resistor 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4ba5f3aa26c28e62800c94ee3bea8eab62b9295b
4
- data.tar.gz: 78e020b33d5eaed94e1956a0732626c16189e510
3
+ metadata.gz: dcdef3b21d2be6f436c54df25801ddfbf7d8997a
4
+ data.tar.gz: 97822e8388112ffcecf9dc616f0a2459132c7712
5
5
  SHA512:
6
- metadata.gz: 4d45490081759694cdd282f3bcc59c00777d6c512149efac8ef79806c606ca461008ef541c178133bf527b279a5dedca60a2a3f0203af2047bc49730ed2aa0c4
7
- data.tar.gz: 8b952ccbbe8da1a138ed560f8a4e0a1719f09e651f39881d9fd86fda3c91ccda5f91c5e85da785bc7a487d319af32095006c8a682c2244df1c6345390d70a890
6
+ metadata.gz: c2895a20f52a76fc9ee2f2d9c8a770dca0ddeb9586958d9ad4617990f7073ae0f219e977e78292c34f4544de0f9268132ddd942af56b0bc67103cf29f2740bb0
7
+ data.tar.gz: 1cce63a88b3b75a6a4428433e5e5c0490f292b8817f20733eb74f8c147e8eb9a0356e9ad62d2e96cacaae12d7aea77f0516fb748da5d90f45d5c1c3771d92c2b
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Resistor
2
2
  [![Gem Version](https://badge.fury.io/rb/resistor.svg)](http://badge.fury.io/rb/resistor)
3
3
  Resistor is a gem for the resistor unit.
4
+ For more details, please see [http://qiita.com/seinosuke/items/d527b0e591d6d69c7471](http://qiita.com/seinosuke/items/d527b0e591d6d69c7471).
4
5
 
5
6
  ## Installation
6
7
 
@@ -21,8 +22,9 @@ Or install it yourself as:
21
22
  ## Usage
22
23
 
23
24
  ###Resistor Color Code Converter
24
- `Resistor::ColorCode.encode` **Converts a resistance value to a color code.**
25
- `Resistor::ColorCode.decode` **Converts a color code to a resistance value.**
25
+ You can calculate a resistance value from a color code. The contrary is possible.
26
+ `Resistor::ColorCode.encode` Converts a resistance value to a color code.
27
+ `Resistor::ColorCode.decode` Converts a color code to a resistance value.
26
28
 
27
29
  ```ruby
28
30
  require 'resistor'
@@ -34,10 +36,11 @@ Resistor::ColorCode.decode([:yellow, :purple, :red, :gold])
34
36
  ```
35
37
 
36
38
  ### Combined Resistor Calculator
37
- `Resistor::BasicReisistor#+` **Calculates a series combined resistance value.**
38
- `Resistor::BasicReisistor#/` **Calculates a parallel combined resistance value.**
39
+ You can calculate a combined resistance value.
40
+ `Resistor::BasicReisistor#+` Calculates a series combined resistance value.
41
+ `Resistor::BasicReisistor#/` Calculates a parallel combined resistance value.
39
42
 
40
- `Resistor.new` is an alias for `Resistor::BasicResistor.new`
43
+ `Resistor.new` is an alias for `Resistor::BasicResistor.new`
41
44
 
42
45
  ```ruby
43
46
  require 'resistor'
@@ -54,9 +57,33 @@ r5 = (r1 / r2) + r3 + (r4 / r4)
54
57
  r5.ohm # => 20.0
55
58
  ```
56
59
 
60
+ ### 5-Band Color Code
61
+ Selects a number of color bands. By dafault, the number is 4.
62
+ `Resistor::Options.set_band_number` Sets a combination of options that is usually used.
63
+
64
+ ```ruby
65
+ require 'resistor'
66
+
67
+ Resistor::Options.set_band_number 5
68
+ Resistor.new(976)
69
+ # => #<Resistor::BasicResistor:0x0000000295fb58 @ohm=976.0, @code=[:white, :purple, :blue, :black, :brown], @tolerance=1.0>
70
+ Resistor::ColorCode.encode(100)
71
+ # => [:brown, :black, :black, :black, :brown]
72
+
73
+ Resistor::Options.set_band_number 4
74
+ Resistor.new(4700)
75
+ # => #<Resistor::BasicResistor:0x0000000295ee10 @ohm=4700.0, @code=[:yellow, :purple, :red, :gold], @tolerance=5.0>
76
+ Resistor::ColorCode.encode(100)
77
+ # => [:brown, :black, :brown, :gold]
78
+ ```
79
+
80
+
57
81
  ### E Series
58
- `Resistor::BasicReisistor#e12?` **Whether or not the resistance value is the E12 series.**
59
- `Resistor::BasicReisistor#e24?` **Whether or not the resistance value is the E24 series.**
82
+ `Resistor::BasicReisistor#e12?` Whether or not the resistance value is the E12 series.
83
+ `Resistor::BasicReisistor#e24?` Whether or not the resistance value is the E24 series.
84
+ `Resistor::BasicReisistor#e48?` Whether or not the resistance value is the E48 series.
85
+ `Resistor::BasicReisistor#e96?` Whether or not the resistance value is the E96 series.
86
+ `#48?` and `#96?` always return false if the number of bands is 4.
60
87
 
61
88
  ```ruby
62
89
  require 'resistor'
@@ -68,4 +95,14 @@ r1.e12? # => true
68
95
  r1.e24? # => true
69
96
  r2.e12? # => false
70
97
  r2.e24? # => true
98
+
99
+ Resistor::Options.set_band_number 5
100
+
101
+ r3 = Resistor.new(105)
102
+ r4 = Resistor.new(4.99)
103
+
104
+ r3.e48? # => true
105
+ r3.e96? # => true
106
+ r4.e48? # => false
107
+ r4.e96? # => true
71
108
  ```
data/lib/resistor.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "resistor/version"
2
+ require "resistor/options"
2
3
  require "resistor/color_code"
3
4
  require "resistor/basic_resistor"
4
5
  require "resistor/combined_resistor"
@@ -11,8 +12,8 @@ module Resistor
11
12
  # @see Resistor::BasicResistor
12
13
  # @return [Resistor::BasicResistor]
13
14
  # @example Create a Resistor::BasicResistor object.
14
- # r1 = Resistor.new(ohm: 20)
15
- # r2 = Resistor.new(code: ['yellow', 'purple', 'red', 'gold'])
15
+ # r1 = Resistor.new(20)
16
+ # r2 = Resistor.new(['yellow', 'purple', 'red', 'gold'])
16
17
  def new(arg, options = {})
17
18
  Resistor::BasicResistor.new(arg, options)
18
19
  end
@@ -8,26 +8,28 @@ module Resistor
8
8
  # but at least one of them must be supplied.
9
9
  #
10
10
  # @overload initialize(arg, options = {})
11
- # @param arg [Integer, Float] A resistance value.
11
+ # @param arg [Integer, Float] A resistance value
12
12
  # @overload initialize(arg, options = {})
13
- # @param arg [Array<Symbol>, Array<String>] A color code.
13
+ # @param arg [Array<Symbol>, Array<String>] A color code
14
14
  # @option options [Integer, Float] :tolerance(5.0)
15
+ # @option options [Integer] :band_number(4)
15
16
  # @raise [ArgumentError] The ohm or code parameter must be supplied.
16
17
  # Error raised if neither parameter is supplied.
17
18
  # @return [Resistor::BasicResistor]
18
19
  def initialize(arg, options = {})
19
- options[:tolerance] ||= 5.0
20
- opt = {:tolerance => options[:tolerance]}
20
+ default = Resistor::Options.new
21
+ options[:tolerance] ||= default.tolerance
22
+ options[:band_number] ||= default.band_number
21
23
 
22
24
  case arg
23
25
  when Integer, Float
24
26
  @ohm = arg.to_f
25
- @code = Resistor::ColorCode.encode(@ohm, opt)
27
+ @code = Resistor::ColorCode.encode(@ohm, options)
26
28
  @tolerance = options[:tolerance]
27
29
  when Array
28
30
  @code = arg.map(&:to_sym)
29
- @ohm = Resistor::ColorCode.decode(@code)
30
- @tolerance = Resistor::ColorCode::TOLERANCE[@code[3].to_sym]
31
+ @ohm = Resistor::ColorCode.decode(@code, options)
32
+ @tolerance = Resistor::ColorCode::TOLERANCE[@code[-1].to_sym]
31
33
  else
32
34
  raise ArgumentError
33
35
  end
@@ -71,7 +73,9 @@ module Resistor
71
73
  end
72
74
  alias_method :|, :/
73
75
 
74
- # @return [Boolean] Whether or not the resistance value is the E12 series.
76
+ # Whether or not the resistance value is the E12 series.
77
+ #
78
+ # @return [Boolean]
75
79
  def e12?
76
80
  num0 = Resistor::ColorCode::DIGIT[@code[0]]
77
81
  num1 = Resistor::ColorCode::DIGIT[@code[1]]
@@ -83,7 +87,9 @@ module Resistor
83
87
  return false
84
88
  end
85
89
 
86
- # @return [Boolean] Whether or not the resistance value is the E24 series.
90
+ # Whether or not the resistance value is the E24 series.
91
+ #
92
+ # @return [Boolean]
87
93
  def e24?
88
94
  num0 = Resistor::ColorCode::DIGIT[@code[0]]
89
95
  num1 = Resistor::ColorCode::DIGIT[@code[1]]
@@ -94,5 +100,33 @@ module Resistor
94
100
  end
95
101
  return false
96
102
  end
103
+
104
+ # Whether or not the resistance value is the E48 series.
105
+ # Always returns false if the number of bands is 4.
106
+ #
107
+ # @return [Boolean]
108
+ def e48?
109
+ return false if @code.size == 4
110
+ num = [
111
+ Resistor::ColorCode::DIGIT[@code[0]],
112
+ Resistor::ColorCode::DIGIT[@code[1]],
113
+ Resistor::ColorCode::DIGIT[@code[2]],
114
+ ].join.to_i
115
+ Resistor::ColorCode::E48_SERIES.any?{|n| n == num }
116
+ end
117
+
118
+ # Whether or not the resistance value is the E96 series.
119
+ # Always returns false if the number of bands is 4.
120
+ #
121
+ # @return [Boolean]
122
+ def e96?
123
+ return false if @code.size == 4
124
+ num = [
125
+ Resistor::ColorCode::DIGIT[@code[0]],
126
+ Resistor::ColorCode::DIGIT[@code[1]],
127
+ Resistor::ColorCode::DIGIT[@code[2]],
128
+ ].join.to_i
129
+ Resistor::ColorCode::E96_SERIES.any?{|n| n == num }
130
+ end
97
131
  end
98
132
  end
@@ -1,6 +1,8 @@
1
1
  module Resistor
2
2
  module ColorCode
3
3
 
4
+ # [4-Band-Code] The 1st and 2nd digit of a resistance value
5
+ # [5-Band-Code] The 1st, 2nd and 3rd digit of a resistance value
4
6
  DIGIT = {
5
7
  :black => 0,
6
8
  :brown => 1,
@@ -14,6 +16,8 @@ module Resistor
14
16
  :white => 9
15
17
  }.freeze
16
18
 
19
+ # [4-Band-Code] The 3rd color band indicates the multiplier.
20
+ # [5-Band-Code] The 4th color band indicates the multiplier.
17
21
  MULTIPLIER = {
18
22
  :black => 0,
19
23
  :brown => 1,
@@ -26,6 +30,8 @@ module Resistor
26
30
  :silver => -2
27
31
  }.freeze
28
32
 
33
+ # [4-Band-Code] The 4th color band indicates the tolerance.
34
+ # [5-Band-Code] The 5th color band indicates the tolerance.
29
35
  TOLERANCE = {
30
36
  :brown => 1.0,
31
37
  :red => 2.0,
@@ -59,51 +65,140 @@ module Resistor
59
65
  9 => [1]
60
66
  }.freeze
61
67
 
68
+ E48_SERIES = [
69
+ 100, 105, 110, 115, 121, 127, 133, 140, 147, 154, 162, 169, 178, 187, 196, 205,
70
+ 215, 226, 237, 249, 261, 274, 287, 301, 316, 332, 348, 365, 383, 402, 422, 442,
71
+ 464, 487, 511, 536, 562, 590, 619, 649, 681, 715, 750, 787, 825, 866, 909, 953
72
+ ].freeze
73
+
74
+ E96_SERIES = [
75
+ 100, 102, 105, 107, 110, 113, 115, 118, 121, 124, 127, 130, 133, 137, 140, 143,
76
+ 147, 150, 154, 158, 162, 165, 169, 174, 178, 182, 187, 191, 196, 200, 205, 210,
77
+ 215, 221, 226, 232, 237, 243, 249, 255, 261, 267, 274, 280, 287, 294, 301, 309,
78
+ 316, 324, 332, 340, 348, 357, 365, 374, 383, 392, 402, 412, 422, 432, 442, 453,
79
+ 464, 475, 487, 499, 511, 523, 536, 549, 562, 576, 590, 604, 619, 634, 649, 665,
80
+ 681, 698, 715, 732, 750, 768, 787, 806, 825, 845, 866, 887, 909, 931, 953, 976
81
+ ].freeze
82
+
83
+ module_function
62
84
 
63
85
  # Converts a resistance value to a color code.
64
86
  # The value must be between 0.1 and 99_000_000.
65
87
  #
66
88
  # @param ohm [Integer, Float] resistance value
67
89
  # @option options [Integer, Float] :tolerance(5.0)
90
+ # @option options [Integer] :band_number(4)
68
91
  # @raise [ArgumentError] Error raised
69
92
  # when the supplied resistance value is less than 0.1.
70
93
  # @return [Array<Symbol>] color code
71
- def self.encode(ohm, options = {:tolerance => 5.0})
94
+ def encode(ohm, options = {})
72
95
  return [DIGIT.key(0)] if ohm == 0
73
96
  raise ArgumentError if ohm < 0.1
74
97
 
98
+ default = Resistor::Options.new
99
+ options[:tolerance] ||= default.tolerance
100
+ options[:band_number] ||= default.band_number
101
+
102
+ case options[:band_number]
103
+ when 4 then four_band_encode(ohm, options)
104
+ when 5 then five_band_encode(ohm, options)
105
+ else raise ArgumentError
106
+ end
107
+ end
108
+
109
+ # This method is used by the `Colorcode.encode` method
110
+ # when `options[:band_number]` is 4.
111
+ def four_band_encode(ohm, options = {})
75
112
  if ohm < 1
76
113
  ohm_str = (ohm*100).to_s.split('')
77
- [DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
78
- MULTIPLIER.key(-2), TOLERANCE.key(options[:tolerance])]
114
+ [DIGIT.key(ohm_str[0].to_i),
115
+ DIGIT.key(ohm_str[1].to_i),
116
+ MULTIPLIER.key(-2),
117
+ TOLERANCE.key(options[:tolerance])]
79
118
 
80
119
  elsif ohm < 10
81
120
  ohm_str = (ohm*10).to_s.split('')
82
- [DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
83
- MULTIPLIER.key(-1), TOLERANCE.key(options[:tolerance])]
121
+ [DIGIT.key(ohm_str[0].to_i),
122
+ DIGIT.key(ohm_str[1].to_i),
123
+ MULTIPLIER.key(-1),
124
+ TOLERANCE.key(options[:tolerance])]
84
125
 
85
126
  else
86
127
  ohm_str = ohm.to_i.to_s.split('')
87
- [DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
88
- MULTIPLIER.key(ohm_str.size - 2), TOLERANCE.key(options[:tolerance])]
128
+ [DIGIT.key(ohm_str[0].to_i),
129
+ DIGIT.key(ohm_str[1].to_i),
130
+ MULTIPLIER.key(ohm_str.size - 2),
131
+ TOLERANCE.key(options[:tolerance])]
132
+ end
133
+ end
134
+
135
+ # This method is used by the `Colorcode.encode` method
136
+ # when `options[:band_number]` is 5.
137
+ def five_band_encode(ohm, options = {})
138
+ if ohm < 10
139
+ ohm_str = (ohm*100).to_s.split('')
140
+ [DIGIT.key(ohm_str[0].to_i),
141
+ DIGIT.key(ohm_str[1].to_i),
142
+ DIGIT.key(ohm_str[2].to_i),
143
+ MULTIPLIER.key(-2),
144
+ TOLERANCE.key(options[:tolerance])]
145
+
146
+ elsif ohm < 100
147
+ ohm_str = (ohm*10).to_s.split('')
148
+ [DIGIT.key(ohm_str[0].to_i),
149
+ DIGIT.key(ohm_str[1].to_i),
150
+ DIGIT.key(ohm_str[2].to_i),
151
+ MULTIPLIER.key(-1),
152
+ TOLERANCE.key(options[:tolerance])]
153
+
154
+ else
155
+ ohm_str = ohm.to_i.to_s.split('')
156
+ [DIGIT.key(ohm_str[0].to_i),
157
+ DIGIT.key(ohm_str[1].to_i),
158
+ DIGIT.key(ohm_str[2].to_i),
159
+ MULTIPLIER.key(ohm_str.size - 3),
160
+ TOLERANCE.key(options[:tolerance])]
89
161
  end
90
162
  end
91
163
 
92
164
  # Converts a color code to a resistance value.
93
165
  #
94
166
  # @param code [Array<Symbol>, Array<String>] color code
167
+ # @option options [Integer] :band_number(4)
95
168
  # @raise [ArgumentError] Error raised
96
169
  # when the supplied color code is not an array,
97
170
  # or when the color of the first band is black.
98
171
  # @return [Float] resistance value
99
- def self.decode(code)
172
+ def decode(code, options = {})
100
173
  raise ArgumentError unless code.is_a? Array
101
174
  code = code.map(&:to_sym)
102
175
  return 0.0 if code == [:black]
103
176
  raise ArgumentError if code[0] == :black
104
177
 
105
- ohm = (DIGIT[code[0]]*10 + DIGIT[code[1]]) * 10**MULTIPLIER[code[2]]
106
- return ohm.to_f
178
+ default = Resistor::Options.new
179
+ options[:band_number] ||= default.band_number
180
+
181
+ case options[:band_number]
182
+ when 4 then four_band_decode(code)
183
+ when 5 then five_band_decode(code)
184
+ else raise ArgumentError
185
+ end
186
+ end
187
+
188
+ # This method is used by the `Colorcode.decode` method
189
+ # when `options[:band_number]` is 4.
190
+ def four_band_decode(code)
191
+ (DIGIT[code[0]]*10 + DIGIT[code[1]]) *
192
+ 10**MULTIPLIER[code[2]]
193
+ .to_f
194
+ end
195
+
196
+ # This method is used by the `Colorcode.decode` method
197
+ # when `options[:band_number]` is 5.
198
+ def five_band_decode(code)
199
+ (DIGIT[code[0]]*100 + DIGIT[code[1]]*10 + DIGIT[code[2]]) *
200
+ 10**MULTIPLIER[code[3]]
201
+ .to_f
107
202
  end
108
203
  end
109
204
  end
@@ -0,0 +1,52 @@
1
+ module Resistor
2
+ class Options
3
+
4
+ @defaults = {
5
+ :band_number => 4,
6
+ :tolerance => 5.0
7
+ }
8
+
9
+ attr_reader :band_number, :tolerance
10
+
11
+ class << self
12
+ # The default option values.
13
+ # `@defaults` is a hash of the default options.
14
+ #
15
+ # @yield You can set the options in a block too.
16
+ # @return [Hash]
17
+ # @example Sets the options in a block.
18
+ # Resistor::Options.defaults do |d|
19
+ # d[:tolerance] = 0.5
20
+ # d[:band_number] = 5
21
+ # end
22
+ def defaults(&block)
23
+ yield @defaults if block_given?
24
+ @defaults
25
+ end
26
+
27
+ # Sets a combination of options that is usually used.
28
+ #
29
+ # @param num [Integer] The number of the color bands
30
+ def set_band_number(num)
31
+ case num
32
+ when 4
33
+ @defaults[:band_number] = 4
34
+ @defaults[:tolerance] = 5.0
35
+ when 5
36
+ @defaults[:band_number] = 5
37
+ @defaults[:tolerance] = 1.0
38
+ end
39
+ end
40
+ end
41
+
42
+ # Initializes a new BasicResistor object.
43
+ # The default option values is stored in the instance variables.
44
+ #
45
+ # @return [Resistor::BasicResistor]
46
+ def initialize
47
+ self.class.defaults.each do |key, val|
48
+ instance_variable_set("@#{key}", val)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module Resistor
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
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{Gem for resistor}
12
+ spec.description = %q{Resistor is a gem for the resistor unit.}
13
13
  spec.homepage = "https://github.com/seinosuke/resistor"
14
14
  spec.license = "MIT"
15
15
 
@@ -9,7 +9,6 @@ describe Resistor::BasicResistor do
9
9
 
10
10
  context '抵抗値を指定した場合' do
11
11
  let(:arg) { 4700 }
12
-
13
12
  it { expect(resistor.ohm).to eq 4700.0 }
14
13
  it { expect(resistor.code).to eq [:yellow, :purple, :red, :gold] }
15
14
  it { expect(resistor.tolerance).to eq 5.0 }
@@ -17,7 +16,6 @@ describe Resistor::BasicResistor do
17
16
 
18
17
  context '抵抗値を指定した場合' do
19
18
  let(:arg) { ['brown', 'black', 'blue', 'silver'] }
20
-
21
19
  it { expect(resistor.ohm).to eq 10_000_000 }
22
20
  it { expect(resistor.code).to eq [:brown, :black, :blue, :silver] }
23
21
  it { expect(resistor.tolerance).to eq 10.0 }
@@ -25,7 +23,6 @@ describe Resistor::BasicResistor do
25
23
 
26
24
  context 'どちらも指定しなかった場合' do
27
25
  let(:arg) { nil }
28
-
29
26
  it { expect { is_expected }.to raise_error(ArgumentError) }
30
27
  end
31
28
 
@@ -34,6 +31,46 @@ describe Resistor::BasicResistor do
34
31
  it { expect(resistor.code[3]).to eq :purple }
35
32
  it { expect(resistor.tolerance).to eq 0.1 }
36
33
  end
34
+
35
+ context 'オプションで5本帯を指定した場合' do
36
+ # 許容差は変更されない
37
+ resistor = Resistor.new(6.04, band_number: 5)
38
+ it { expect(resistor.code).to eq [:blue, :black, :yellow, :silver, :gold] }
39
+ it { expect(resistor.tolerance).to eq 5.0 }
40
+ end
41
+
42
+ context 'メソッドで5本帯を指定した場合' do
43
+ # 許容差まで変更される
44
+ Resistor::Options.set_band_number 5
45
+ resistor = Resistor.new(123)
46
+ it { expect(resistor.code).to eq [:brown, :red, :orange, :black, :brown] }
47
+ it { expect(resistor.tolerance).to eq 1.0 }
48
+ Resistor::Options.set_band_number 4
49
+ end
50
+
51
+ context 'メソッドで5本帯と許容差を指定した場合' do
52
+ # 許容差まで指定できる
53
+ Resistor::Options.defaults do |d|
54
+ d[:band_number] = 5
55
+ d[:tolerance] = 0.5
56
+ end
57
+ resistor = Resistor.new(49900)
58
+ it { expect(resistor.code).to eq [:yellow, :white, :white, :red, :green] }
59
+ it { expect(resistor.tolerance).to eq 0.5 }
60
+ Resistor::Options.set_band_number 4
61
+ end
62
+
63
+ context 'メソッドで5本帯を指定してさらにオプションを渡した場合' do
64
+ # オプションで渡されたものに上書きされる
65
+ Resistor::Options.defaults do |d|
66
+ d[:band_number] = 5
67
+ d[:tolerance] = 0.5
68
+ end
69
+ resistor = Resistor.new(84.5, tolerance:0.1)
70
+ it { expect(resistor.code).to eq [:gray, :yellow, :green, :gold, :purple] }
71
+ it { expect(resistor.tolerance).to eq 0.1 }
72
+ Resistor::Options.set_band_number 4
73
+ end
37
74
  end
38
75
 
39
76
  describe '#+, #/' do
@@ -41,7 +78,6 @@ describe Resistor::BasicResistor do
41
78
  r1 = Resistor.new(100)
42
79
  r2 = Resistor.new(200)
43
80
  r3 = Resistor.new(300)
44
-
45
81
  it { expect((r1 + r2 + r3).ohm).to eq 600.0 }
46
82
  end
47
83
 
@@ -49,7 +85,6 @@ describe Resistor::BasicResistor do
49
85
  r1 = Resistor.new(30)
50
86
  r2 = Resistor.new(15)
51
87
  r3 = Resistor.new(10)
52
-
53
88
  it { expect((r1 / r2 / r3).ohm).to eq 5.0 }
54
89
  end
55
90
 
@@ -58,7 +93,6 @@ describe Resistor::BasicResistor do
58
93
  r2 = Resistor.new(30)
59
94
  r3 = Resistor.new(4)
60
95
  r4 = Resistor.new(8)
61
-
62
96
  it { expect(((r1 / r2) + r3 + (r4 / r4)).ohm).to eq 20.0 }
63
97
  end
64
98
 
@@ -66,7 +100,6 @@ describe Resistor::BasicResistor do
66
100
  r1 = Resistor.new(100)
67
101
  original_name = (r1 + r1 + r1).ohm
68
102
  new_name = (r1 - r1 - r1).ohm
69
-
70
103
  it { expect(original_name == new_name).to be_truthy }
71
104
  end
72
105
 
@@ -74,7 +107,6 @@ describe Resistor::BasicResistor do
74
107
  r1 = Resistor.new(100)
75
108
  original_name = (r1 / r1 / r1).ohm
76
109
  new_name = (r1 | r1 | r1).ohm
77
-
78
110
  it { expect(original_name == new_name).to be_truthy }
79
111
  end
80
112
  end
@@ -83,7 +115,6 @@ describe Resistor::BasicResistor do
83
115
  context '抵抗値を変更する場合' do
84
116
  resistor = Resistor.new(100)
85
117
  resistor.ohm = 4.7
86
-
87
118
  it { expect(resistor.ohm).to eq 4.7 }
88
119
  it { expect(resistor.code).to eq [:yellow, :purple, :gold, :gold] }
89
120
  end
@@ -91,7 +122,6 @@ describe Resistor::BasicResistor do
91
122
  context 'カラーコードを変更する場合' do
92
123
  resistor = Resistor.new(100)
93
124
  resistor.code = ['green', 'brown', 'silver', 'brown']
94
-
95
125
  it { expect(resistor.ohm).to eq 0.51 }
96
126
  it { expect(resistor.code).to eq [:green, :brown, :silver, :brown] }
97
127
  it { expect(resistor.tolerance).to eq 1.0 }
@@ -129,4 +159,50 @@ describe Resistor::BasicResistor do
129
159
  it { expect(Resistor.new(52_000_000).e24?).to eq false }
130
160
  end
131
161
  end
162
+
163
+ describe '#e48?' do
164
+ before do
165
+ Resistor::Options.set_band_number 5
166
+ end
167
+ after do
168
+ Resistor::Options.set_band_number 4
169
+ end
170
+
171
+ context 'E48系列である場合' do
172
+ it { expect(Resistor.new(1.69).e48?).to eq true }
173
+ it { expect(Resistor.new(28.7).e48?).to eq true }
174
+ it { expect(Resistor.new(402).e48?).to eq true }
175
+ it { expect(Resistor.new(90_900_000).e48?).to eq true }
176
+ end
177
+
178
+ context 'E48系列でない場合' do
179
+ it { expect(Resistor.new(1.68).e48?).to eq false }
180
+ it { expect(Resistor.new(28.6).e48?).to eq false }
181
+ it { expect(Resistor.new(403).e48?).to eq false }
182
+ it { expect(Resistor.new(91_900_000).e48?).to eq false }
183
+ end
184
+ end
185
+
186
+ describe '#e96?' do
187
+ before do
188
+ Resistor::Options.set_band_number 5
189
+ end
190
+ after do
191
+ Resistor::Options.set_band_number 4
192
+ end
193
+
194
+ context 'E96系列である場合' do
195
+ it { expect(Resistor.new(1.91).e96?).to eq true }
196
+ it { expect(Resistor.new(45.3).e96?).to eq true }
197
+ it { expect(Resistor.new(715).e96?).to eq true }
198
+ it { expect(Resistor.new(10_000_000).e96?).to eq true }
199
+ end
200
+
201
+ context 'E96系列でない場合' do
202
+ it { expect(Resistor.new(1.92).e96?).to eq false }
203
+ it { expect(Resistor.new(45.4).e96?).to eq false }
204
+ it { expect(Resistor.new(716).e96?).to eq false }
205
+ it { expect(Resistor.new(11_100_000).e96?).to eq false }
206
+ end
207
+ end
132
208
  end
@@ -5,65 +5,128 @@ require_relative "spec_helper"
5
5
  describe Resistor::ColorCode do
6
6
 
7
7
  describe '::encode' do
8
- subject { Resistor::ColorCode.encode(ohm) }
8
+ subject { Resistor::ColorCode.encode(ohm, options) }
9
9
 
10
10
  context '0Ωの場合' do
11
11
  let(:ohm) { 0 }
12
+ let(:options) { {} }
12
13
  it { is_expected.to eq [:black] }
13
14
  end
14
15
 
15
16
  context '0.1Ω以上1Ωより小さい場合' do
16
17
  let(:ohm) { 0.24 }
18
+ let(:options) { {} }
17
19
  it { is_expected.to eq [:red, :yellow, :silver, :gold] }
18
20
  end
19
21
 
20
22
  context '1Ω以上10Ωより小さい場合' do
21
23
  let(:ohm) { 2 }
24
+ let(:options) { {} }
22
25
  it { is_expected.to eq [:red, :black, :gold, :gold] }
23
26
  end
24
27
 
25
28
  context '10Ω以上の場合' do
26
29
  let(:ohm) { 3300 }
30
+ let(:options) { {} }
27
31
  it { is_expected.to eq [:orange, :orange, :red, :gold] }
28
32
  end
29
33
 
30
34
  context '0Ωより小さい場合' do
31
35
  let(:ohm) { -10 }
36
+ let(:options) { {} }
32
37
  it { expect { is_expected }.to raise_error(ArgumentError) }
33
38
  end
34
39
 
35
40
  context '0.1Ωより小さい場合' do
36
41
  let(:ohm) { 0.01 }
42
+ let(:options) { {} }
37
43
  it { expect { is_expected }.to raise_error(ArgumentError) }
38
44
  end
45
+
46
+ context 'オプションで5本帯を指定した場合' do
47
+ # 許容差は変更されない
48
+ let(:ohm) { 4320 }
49
+ let(:options) { {:band_number => 5} }
50
+ it { is_expected.to eq [:yellow, :orange, :red, :brown, :gold] }
51
+ end
52
+
53
+ context 'メソッドで5本帯を指定した場合' do
54
+ # 許容差まで変更される
55
+ Resistor::Options.set_band_number 5
56
+ code = Resistor::ColorCode.encode(511)
57
+ it { expect(code).to eq [:green, :brown, :brown, :black, :brown] }
58
+ Resistor::Options.set_band_number 4
59
+ end
60
+
61
+ context 'メソッドで5本帯と許容差を指定した場合' do
62
+ # 許容差まで指定できる
63
+ Resistor::Options.defaults do |d|
64
+ d[:band_number] = 5
65
+ d[:tolerance] = 0.5
66
+ end
67
+ code = Resistor::ColorCode.encode(3.92)
68
+ it { expect(code).to eq [:orange, :white, :red, :silver, :green] }
69
+ Resistor::Options.set_band_number 4
70
+ end
71
+
72
+ context 'メソッドで5本帯を指定してさらにオプションを渡した場合' do
73
+ # オプションで渡されたものに上書きされる
74
+ Resistor::Options.defaults do |d|
75
+ d[:band_number] = 5
76
+ d[:tolerance] = 0.5
77
+ end
78
+ code = Resistor::ColorCode.encode(20.5, tolerance:0.1)
79
+ it { expect(code).to eq [:red, :black, :green, :gold, :purple] }
80
+ Resistor::Options.set_band_number 4
81
+ end
39
82
  end
40
83
 
41
84
  describe '::decode' do
42
- subject { Resistor::ColorCode.decode(code) }
85
+ subject { Resistor::ColorCode.decode(code, options) }
43
86
 
44
87
  context '0Ωの場合' do
45
88
  let(:code) { [:black] }
89
+ let(:options) { {} }
46
90
  it { is_expected.to eq 0 }
47
91
  end
48
92
 
49
93
  context '0.1Ω以上1Ωより小さい場合' do
50
94
  let(:code) { [:blue, :red, :silver, :gold] }
95
+ let(:options) { {} }
51
96
  it { is_expected.to eq 0.62 }
52
97
  end
53
98
 
54
99
  context '1Ω以上10Ωより小さい場合' do
55
100
  let(:code) { [:white, :brown, :gold, :gold] }
101
+ let(:options) { {} }
56
102
  it { is_expected.to eq 9.1 }
57
103
  end
58
104
 
59
105
  context '10Ω以上の場合' do
60
106
  let(:code) { [:brown, :black, :blue, :gold] }
107
+ let(:options) { {} }
61
108
  it { is_expected.to eq 10_000_000 }
62
109
  end
63
110
 
64
111
  context '1本目に黒が指定された場合' do
65
112
  let(:code) { [:black, :blue, :blue, :gold] }
113
+ let(:options) { {} }
66
114
  it { expect { is_expected }.to raise_error(ArgumentError) }
67
115
  end
116
+
117
+ context 'オプションで5本帯を指定した場合' do
118
+ # 許容差は変更されない
119
+ let(:code) { [:yellow, :orange, :red, :brown, :gold] }
120
+ let(:options) { {:band_number => 5} }
121
+ it { is_expected.to eq 4320.0 }
122
+ end
123
+
124
+ context 'メソッドで5本帯を指定した場合' do
125
+ # 許容差まで変更される
126
+ Resistor::Options.set_band_number 5
127
+ ohm = Resistor::ColorCode.decode([:green, :brown, :brown, :black, :brown])
128
+ it { expect(ohm).to eq 511.0 }
129
+ Resistor::Options.set_band_number 4
130
+ end
68
131
  end
69
132
  end
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: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - seinosuke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-11 00:00:00.000000000 Z
11
+ date: 2015-02-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.7'
27
- description: Gem for resistor
27
+ description: Resistor is a gem for the resistor unit.
28
28
  email:
29
29
  - seinosuke.3606@gmail.com
30
30
  executables: []
@@ -42,6 +42,7 @@ files:
42
42
  - lib/resistor/basic_resistor.rb
43
43
  - lib/resistor/color_code.rb
44
44
  - lib/resistor/combined_resistor.rb
45
+ - lib/resistor/options.rb
45
46
  - lib/resistor/version.rb
46
47
  - resistor.gemspec
47
48
  - spec/basic_resistor_spec.rb
@@ -67,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
68
  version: '0'
68
69
  requirements: []
69
70
  rubyforge_project:
70
- rubygems_version: 2.2.2
71
+ rubygems_version: 2.4.5
71
72
  signing_key:
72
73
  specification_version: 4
73
74
  summary: Resistor gem