resistor 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +13 -1
- data/README.md +29 -8
- data/Rakefile +7 -0
- data/lib/resistor.rb +9 -2
- data/lib/resistor/basic_resistor.rb +39 -8
- data/lib/resistor/color_code.rb +23 -14
- data/lib/resistor/combined_resistor.rb +14 -0
- data/lib/resistor/version.rb +1 -1
- data/resistor.gemspec +1 -3
- data/spec/basic_resistor_spec.rb +6 -0
- metadata +4 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a576d33899464a13043e8bc89b37d103a0a7fa1
|
4
|
+
data.tar.gz: cf556ea896a9c2be6273e812036d3c5846a8c12a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01501d32e0c5486bb9b5123da1ad26d7f78a2848908198ae35c82c728137e58bef91e37e549278eacc48c93e8a1eb7c6a18053915ec78e2c87ea5fb323eecc25
|
7
|
+
data.tar.gz: 9c739bd8b568864ddf3689894e3d413204bcf7add187bcca50a1fc59896b72b3aec485041887f0dd7f4c79b9d5e328c61c953dbbb9b92f37f5a0914611c500a5
|
data/Gemfile
CHANGED
@@ -1,4 +1,16 @@
|
|
1
1
|
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
|
3
|
+
gem 'rake'
|
4
|
+
gem 'yard'
|
5
|
+
|
6
|
+
group :development do
|
7
|
+
gem 'pry'
|
8
|
+
gem 'pry-doc'
|
9
|
+
gem 'pry-stack_explorer'
|
10
|
+
end
|
11
|
+
|
12
|
+
group :test do
|
13
|
+
gem 'rspec', '>= 3.1'
|
14
|
+
end
|
15
|
+
|
4
16
|
gemspec
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# Resistor
|
2
|
+
Resistor is a gem for the resistor unit.
|
2
3
|
|
3
4
|
## Installation
|
4
5
|
|
@@ -18,27 +19,47 @@ Or install it yourself as:
|
|
18
19
|
|
19
20
|
## Usage
|
20
21
|
|
22
|
+
###Resistor Color Code Converter
|
23
|
+
`Resistor::ColorCode.encode` **Converts a resistance value to a color code.**
|
24
|
+
`Resistor::ColorCode.decode` **Converts a color code to a resistance value.**
|
25
|
+
|
21
26
|
```ruby
|
22
27
|
require 'resistor'
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
Resistor::ColorCode.decode([:yellow, :purple, :red, :gold])
|
29
|
+
Resistor::ColorCode.encode(4700)
|
30
|
+
# => [:yellow, :purple, :red, :gold]
|
31
|
+
Resistor::ColorCode.decode([:yellow, :purple, :red, :gold])
|
32
|
+
# => 4700.0
|
33
|
+
```
|
27
34
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
### Combined Resistor Calculator
|
36
|
+
`Resistor::BasicReisistor#+` **Calculates a series combined resistance value.**
|
37
|
+
`Resistor::BasicReisistor#/` **Calculates a parallel combined resistance value.**
|
38
|
+
|
39
|
+
`Resistor.new` is an alias for `Resistor::BasicResistor.new`
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'resistor'
|
32
43
|
|
33
44
|
r1 = Resistor.new(ohm: 20)
|
34
45
|
r2 = Resistor.new(ohm: 30)
|
35
46
|
r3 = Resistor.new(ohm: 4)
|
36
47
|
r4 = Resistor.new(ohm: 8)
|
37
48
|
|
49
|
+
# --[r1]-- --[r4]--
|
50
|
+
# --| |--[r3]--| |--
|
51
|
+
# --[r2]-- --[r4]--
|
38
52
|
r5 = (r1 / r2) + r3 + (r4 / r4)
|
39
53
|
r5.ohm # => 20.0
|
54
|
+
```
|
55
|
+
|
56
|
+
### E Series
|
57
|
+
`Resistor::BasicReisistor#e12?` **Whether or not the resistance value is the E12 series.**
|
58
|
+
`Resistor::BasicReisistor#e24?` **Whether or not the resistance value is the E24 series.**
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'resistor'
|
40
62
|
|
41
|
-
# E Series Checker
|
42
63
|
r1 = Resistor.new(ohm: 4700)
|
43
64
|
r2 = Resistor.new(ohm: 62)
|
44
65
|
|
data/Rakefile
CHANGED
data/lib/resistor.rb
CHANGED
@@ -6,8 +6,15 @@ require "resistor/combined_resistor"
|
|
6
6
|
module Resistor
|
7
7
|
|
8
8
|
class << self
|
9
|
-
|
10
|
-
|
9
|
+
# Alias for Resistor::BasicResistor.new
|
10
|
+
#
|
11
|
+
# @see Resistor::BasicResistor
|
12
|
+
# @return [Resistor::BasicResistor]
|
13
|
+
# @example Create a Resistor::BasicResistor object.
|
14
|
+
# r1 = Resistor.new(ohm: 20)
|
15
|
+
# r2 = Resistor.new(code: ['yellow', 'purple', 'red', 'gold'])
|
16
|
+
def new(options = {})
|
17
|
+
Resistor::BasicResistor.new(options)
|
11
18
|
end
|
12
19
|
end
|
13
20
|
end
|
@@ -3,15 +3,27 @@ module Resistor
|
|
3
3
|
|
4
4
|
attr_reader :ohm, :code, :error_range
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
# Initializes a new BasicResistor object.
|
7
|
+
# Both the ohm and code parameter are optional,
|
8
|
+
# but at least one of them must be supplied.
|
9
|
+
#
|
10
|
+
# @option options [Integer, Float] :ohm(nil) resistance value
|
11
|
+
# @option options [Array<Symbol>, Array<String>] :code(nil) color code
|
12
|
+
# @option options [Integer, Float] :error_range(5.0)
|
13
|
+
# @raise [ArgumentError] The ohm or code parameter must be supplied.
|
14
|
+
# Error raised if neither parameter is supplied.
|
15
|
+
# @return [Resistor::BasicResistor]
|
16
|
+
# def initialize(ohm:nil, code:nil, error_range: 5.0)
|
17
|
+
def initialize(options = {})
|
18
|
+
options[:error_range] ||= 5.0
|
19
|
+
opt = {:error_range => options[:error_range]}
|
20
|
+
if options[:ohm]
|
21
|
+
@ohm = options[:ohm].to_f
|
10
22
|
@code = Resistor::ColorCode.encode(@ohm, opt)
|
11
|
-
@error_range = error_range
|
12
|
-
elsif code
|
13
|
-
raise ArgumentError unless code.is_a? Array
|
14
|
-
@code = code.map(&:to_sym)
|
23
|
+
@error_range = options[:error_range]
|
24
|
+
elsif options[:code]
|
25
|
+
raise ArgumentError unless options[:code].is_a? Array
|
26
|
+
@code = options[:code].map(&:to_sym)
|
15
27
|
@ohm = Resistor::ColorCode.decode(@code)
|
16
28
|
@error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
|
17
29
|
else
|
@@ -19,25 +31,43 @@ module Resistor
|
|
19
31
|
end
|
20
32
|
end
|
21
33
|
|
34
|
+
# Set a resistance value.
|
35
|
+
# When the resistance value is changed, the color code is also changed.
|
36
|
+
#
|
37
|
+
# @param ohm [Integer, Float] resistance value
|
22
38
|
def ohm=(ohm)
|
23
39
|
@ohm = ohm.to_f
|
24
40
|
@code = Resistor::ColorCode.encode(@ohm)
|
25
41
|
end
|
26
42
|
|
43
|
+
# Set a color code.
|
44
|
+
# When the color code is changed, the resistance value is also changed,
|
45
|
+
# and the error range is also changed.
|
46
|
+
#
|
47
|
+
# @param code [Array<Symbol>, Array<String>] color code
|
27
48
|
def code=(code)
|
28
49
|
@code = code.map(&:to_sym)
|
29
50
|
@ohm = Resistor::ColorCode.decode(@code)
|
30
51
|
@error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
|
31
52
|
end
|
32
53
|
|
54
|
+
# Calculates a series combined resistance value.
|
55
|
+
#
|
56
|
+
# @param other [Resistor::BasicResistor, Resistor::CombinedResistor]
|
57
|
+
# @return [Resistor::CombinedResistor]
|
33
58
|
def +(other)
|
34
59
|
Resistor::CombinedResistor.new(@ohm + other.ohm)
|
35
60
|
end
|
36
61
|
|
62
|
+
# Calculates a parallel combined resistance value.
|
63
|
+
#
|
64
|
+
# @param other [Resistor::BasicResistor, Resistor::CombinedResistor]
|
65
|
+
# @return [Resistor::CombinedResistor]
|
37
66
|
def /(other)
|
38
67
|
Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
|
39
68
|
end
|
40
69
|
|
70
|
+
# @return [Boolean] Whether or not the resistance value is the E12 series.
|
41
71
|
def e12?
|
42
72
|
num0 = Resistor::ColorCode::NUM[@code[0]]
|
43
73
|
num1 = Resistor::ColorCode::NUM[@code[1]]
|
@@ -49,6 +79,7 @@ module Resistor
|
|
49
79
|
return false
|
50
80
|
end
|
51
81
|
|
82
|
+
# @return [Boolean] Whether or not the resistance value is the E24 series.
|
52
83
|
def e24?
|
53
84
|
num0 = Resistor::ColorCode::NUM[@code[0]]
|
54
85
|
num1 = Resistor::ColorCode::NUM[@code[1]]
|
data/lib/resistor/color_code.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Resistor
|
2
2
|
module ColorCode
|
3
3
|
|
4
|
-
#
|
4
|
+
# 1st Band, 2nd Band
|
5
5
|
NUM = {
|
6
6
|
:black => 0,
|
7
7
|
:brown => 1,
|
@@ -15,7 +15,7 @@ module Resistor
|
|
15
15
|
:white => 9
|
16
16
|
}.freeze
|
17
17
|
|
18
|
-
#
|
18
|
+
# Multiplier
|
19
19
|
MULT = {
|
20
20
|
:black => 0,
|
21
21
|
:brown => 1,
|
@@ -28,7 +28,7 @@ module Resistor
|
|
28
28
|
:silver => -2
|
29
29
|
}.freeze
|
30
30
|
|
31
|
-
#
|
31
|
+
# Tolerance
|
32
32
|
ERROR_RANGE = {
|
33
33
|
:brown => 1.0,
|
34
34
|
:red => 2.0,
|
@@ -40,7 +40,6 @@ module Resistor
|
|
40
40
|
:silver => 10.0
|
41
41
|
}.freeze
|
42
42
|
|
43
|
-
# E12系列
|
44
43
|
E12_SERIES = {
|
45
44
|
1 => [0, 2, 5, 8],
|
46
45
|
2 => [2, 7],
|
@@ -51,7 +50,6 @@ module Resistor
|
|
51
50
|
8 => [2],
|
52
51
|
}.freeze
|
53
52
|
|
54
|
-
# E24系列
|
55
53
|
E24_SERIES = {
|
56
54
|
1 => [0, 1, 2, 3, 5, 6, 8],
|
57
55
|
2 => [0, 2, 4, 7],
|
@@ -64,32 +62,43 @@ module Resistor
|
|
64
62
|
9 => [1]
|
65
63
|
}.freeze
|
66
64
|
|
67
|
-
|
68
|
-
|
65
|
+
|
66
|
+
# Converts a resistance value to a color code.
|
67
|
+
# The value must be between 0.1 and 99_000_000.
|
68
|
+
#
|
69
|
+
# @param ohm [Integer, Float] resistance value
|
70
|
+
# @option options [Integer, Float] :error_range(5.0)
|
71
|
+
# @raise [ArgumentError] Error raised
|
72
|
+
# when the supplied resistance value is less than 0.1.
|
73
|
+
# @return [Array<Symbol>] color code
|
74
|
+
def self.encode(ohm, options = {:error_range => 5.0})
|
69
75
|
return [NUM.key(0)] if ohm == 0
|
70
76
|
raise ArgumentError if ohm < 0.1
|
71
77
|
|
72
|
-
# 0.1以上で1より小さい
|
73
78
|
if ohm < 1
|
74
79
|
ohm_str = (ohm*100).to_s.split('')
|
75
80
|
[NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
|
76
|
-
MULT.key(-2), ERROR_RANGE.key(error_range)]
|
81
|
+
MULT.key(-2), ERROR_RANGE.key(options[:error_range])]
|
77
82
|
|
78
|
-
# 1以上で10より小さい(1桁)
|
79
83
|
elsif ohm < 10
|
80
84
|
ohm_str = (ohm*10).to_s.split('')
|
81
85
|
[NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
|
82
|
-
MULT.key(-1), ERROR_RANGE.key(error_range)]
|
86
|
+
MULT.key(-1), ERROR_RANGE.key(options[:error_range])]
|
83
87
|
|
84
|
-
# 2桁以上
|
85
88
|
else
|
86
89
|
ohm_str = ohm.to_i.to_s.split('')
|
87
90
|
[NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
|
88
|
-
MULT.key(ohm_str.size - 2), ERROR_RANGE.key(error_range)]
|
91
|
+
MULT.key(ohm_str.size - 2), ERROR_RANGE.key(options[:error_range])]
|
89
92
|
end
|
90
93
|
end
|
91
94
|
|
92
|
-
#
|
95
|
+
# Converts a color code to a resistance value.
|
96
|
+
#
|
97
|
+
# @param code [Array<Symbol>, Array<String>] color code
|
98
|
+
# @raise [ArgumentError] Error raised
|
99
|
+
# when the supplied color code is not an array,
|
100
|
+
# or when the color of the first band is black.
|
101
|
+
# @return [Float] resistance value
|
93
102
|
def self.decode(code)
|
94
103
|
raise ArgumentError unless code.is_a? Array
|
95
104
|
code = code.map(&:to_sym)
|
@@ -3,14 +3,28 @@ module Resistor
|
|
3
3
|
|
4
4
|
attr_reader :ohm
|
5
5
|
|
6
|
+
# Initializes a new CombinedResistor object.
|
7
|
+
#
|
8
|
+
# @see BasicResistor#+
|
9
|
+
# @see BasicResistor#/
|
10
|
+
# @param ohm [Float] resistance value
|
11
|
+
# @return [Resistor::CombinedResistor]
|
6
12
|
def initialize(ohm)
|
7
13
|
@ohm = ohm.to_f
|
8
14
|
end
|
9
15
|
|
16
|
+
# Calculates a series combined resistance value.
|
17
|
+
#
|
18
|
+
# @param other [Resistor::BasicResistor, Resistor::CombinedResistor]
|
19
|
+
# @return [Resistor::CombinedResistor]
|
10
20
|
def +(other)
|
11
21
|
Resistor::CombinedResistor.new(@ohm + other.ohm)
|
12
22
|
end
|
13
23
|
|
24
|
+
# Calculates a parallel combined resistance value.
|
25
|
+
#
|
26
|
+
# @param other [Resistor::BasicResistor, Resistor::CombinedResistor]
|
27
|
+
# @return [Resistor::CombinedResistor]
|
14
28
|
def /(other)
|
15
29
|
Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
|
16
30
|
end
|
data/lib/resistor/version.rb
CHANGED
data/resistor.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["seinosuke"]
|
10
10
|
spec.email = ["seinosuke.3606@gmail.com"]
|
11
11
|
spec.summary = %q{Resistor gem}
|
12
|
-
spec.description = %q{
|
12
|
+
spec.description = %q{Gem for resistor}
|
13
13
|
spec.homepage = "https://github.com/seinosuke/resistor"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
@@ -19,6 +19,4 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
-
spec.add_development_dependency "rspec", "~> 3.1"
|
24
22
|
end
|
data/spec/basic_resistor_spec.rb
CHANGED
@@ -31,6 +31,12 @@ describe Resistor::BasicResistor do
|
|
31
31
|
|
32
32
|
it { expect { is_expected }.to raise_error(ArgumentError) }
|
33
33
|
end
|
34
|
+
|
35
|
+
context '許容差を指定した場合' do
|
36
|
+
resistor = Resistor.new(ohm: 100, error_range: 0.1)
|
37
|
+
it { expect(resistor.code[3]).to eq :purple }
|
38
|
+
it { expect(resistor.error_range).to eq 0.1 }
|
39
|
+
end
|
34
40
|
end
|
35
41
|
|
36
42
|
describe '#+, #/' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resistor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- seinosuke
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-01-
|
11
|
+
date: 2015-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,35 +24,7 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.7'
|
27
|
-
|
28
|
-
name: rake
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '10.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '10.0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '3.1'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '3.1'
|
55
|
-
description: gem for resistor
|
27
|
+
description: Gem for resistor
|
56
28
|
email:
|
57
29
|
- seinosuke.3606@gmail.com
|
58
30
|
executables: []
|
@@ -102,3 +74,4 @@ test_files:
|
|
102
74
|
- spec/basic_resistor_spec.rb
|
103
75
|
- spec/color_code_spec.rb
|
104
76
|
- spec/spec_helper.rb
|
77
|
+
has_rdoc:
|