resistor 0.2.1 → 1.0.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: 3a576d33899464a13043e8bc89b37d103a0a7fa1
4
- data.tar.gz: cf556ea896a9c2be6273e812036d3c5846a8c12a
3
+ metadata.gz: 4ba5f3aa26c28e62800c94ee3bea8eab62b9295b
4
+ data.tar.gz: 78e020b33d5eaed94e1956a0732626c16189e510
5
5
  SHA512:
6
- metadata.gz: 01501d32e0c5486bb9b5123da1ad26d7f78a2848908198ae35c82c728137e58bef91e37e549278eacc48c93e8a1eb7c6a18053915ec78e2c87ea5fb323eecc25
7
- data.tar.gz: 9c739bd8b568864ddf3689894e3d413204bcf7add187bcca50a1fc59896b72b3aec485041887f0dd7f4c79b9d5e328c61c953dbbb9b92f37f5a0914611c500a5
6
+ metadata.gz: 4d45490081759694cdd282f3bcc59c00777d6c512149efac8ef79806c606ca461008ef541c178133bf527b279a5dedca60a2a3f0203af2047bc49730ed2aa0c4
7
+ data.tar.gz: 8b952ccbbe8da1a138ed560f8a4e0a1719f09e651f39881d9fd86fda3c91ccda5f91c5e85da785bc7a487d319af32095006c8a682c2244df1c6345390d70a890
data/Gemfile CHANGED
@@ -11,6 +11,8 @@ end
11
11
 
12
12
  group :test do
13
13
  gem 'rspec', '>= 3.1'
14
+ gem 'guard-rspec', '4.5.0'
15
+ gem 'libnotify', '0.9.1'
14
16
  end
15
17
 
16
18
  gemspec
@@ -0,0 +1,9 @@
1
+ # coding: utf-8
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch('spec/spec_helper.rb') { 'spec' }
6
+
7
+ watch(/lib\/(.+)\.rb/) { 'spec' }
8
+ watch(/lib\/resistor\/(.+)\.rb/) { 'spec' }
9
+ end
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Resistor
2
+ [![Gem Version](https://badge.fury.io/rb/resistor.svg)](http://badge.fury.io/rb/resistor)
2
3
  Resistor is a gem for the resistor unit.
3
4
 
4
5
  ## Installation
@@ -41,10 +42,10 @@ Resistor::ColorCode.decode([:yellow, :purple, :red, :gold])
41
42
  ```ruby
42
43
  require 'resistor'
43
44
 
44
- r1 = Resistor.new(ohm: 20)
45
- r2 = Resistor.new(ohm: 30)
46
- r3 = Resistor.new(ohm: 4)
47
- r4 = Resistor.new(ohm: 8)
45
+ r1 = Resistor.new(20)
46
+ r2 = Resistor.new(30)
47
+ r3 = Resistor.new(4)
48
+ r4 = Resistor.new(8)
48
49
 
49
50
  # --[r1]-- --[r4]--
50
51
  # --| |--[r3]--| |--
@@ -60,8 +61,8 @@ r5.ohm # => 20.0
60
61
  ```ruby
61
62
  require 'resistor'
62
63
 
63
- r1 = Resistor.new(ohm: 4700)
64
- r2 = Resistor.new(ohm: 62)
64
+ r1 = Resistor.new(4700)
65
+ r2 = Resistor.new(62)
65
66
 
66
67
  r1.e12? # => true
67
68
  r1.e24? # => true
@@ -13,8 +13,8 @@ module Resistor
13
13
  # @example Create a Resistor::BasicResistor object.
14
14
  # r1 = Resistor.new(ohm: 20)
15
15
  # r2 = Resistor.new(code: ['yellow', 'purple', 'red', 'gold'])
16
- def new(options = {})
17
- Resistor::BasicResistor.new(options)
16
+ def new(arg, options = {})
17
+ Resistor::BasicResistor.new(arg, options)
18
18
  end
19
19
  end
20
20
  end
@@ -1,31 +1,33 @@
1
1
  module Resistor
2
2
  class BasicResistor
3
3
 
4
- attr_reader :ohm, :code, :error_range
4
+ attr_reader :ohm, :code, :tolerance
5
5
 
6
6
  # Initializes a new BasicResistor object.
7
7
  # Both the ohm and code parameter are optional,
8
8
  # but at least one of them must be supplied.
9
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)
10
+ # @overload initialize(arg, options = {})
11
+ # @param arg [Integer, Float] A resistance value.
12
+ # @overload initialize(arg, options = {})
13
+ # @param arg [Array<Symbol>, Array<String>] A color code.
14
+ # @option options [Integer, Float] :tolerance(5.0)
13
15
  # @raise [ArgumentError] The ohm or code parameter must be supplied.
14
16
  # Error raised if neither parameter is supplied.
15
17
  # @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
18
+ def initialize(arg, options = {})
19
+ options[:tolerance] ||= 5.0
20
+ opt = {:tolerance => options[:tolerance]}
21
+
22
+ case arg
23
+ when Integer, Float
24
+ @ohm = arg.to_f
22
25
  @code = Resistor::ColorCode.encode(@ohm, opt)
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)
26
+ @tolerance = options[:tolerance]
27
+ when Array
28
+ @code = arg.map(&:to_sym)
27
29
  @ohm = Resistor::ColorCode.decode(@code)
28
- @error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
30
+ @tolerance = Resistor::ColorCode::TOLERANCE[@code[3].to_sym]
29
31
  else
30
32
  raise ArgumentError
31
33
  end
@@ -42,13 +44,13 @@ module Resistor
42
44
 
43
45
  # Set a color code.
44
46
  # When the color code is changed, the resistance value is also changed,
45
- # and the error range is also changed.
47
+ # and the tolerance is also changed.
46
48
  #
47
49
  # @param code [Array<Symbol>, Array<String>] color code
48
50
  def code=(code)
49
51
  @code = code.map(&:to_sym)
50
52
  @ohm = Resistor::ColorCode.decode(@code)
51
- @error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
53
+ @tolerance = Resistor::ColorCode::TOLERANCE[@code[3].to_sym]
52
54
  end
53
55
 
54
56
  # Calculates a series combined resistance value.
@@ -58,6 +60,7 @@ module Resistor
58
60
  def +(other)
59
61
  Resistor::CombinedResistor.new(@ohm + other.ohm)
60
62
  end
63
+ alias_method :-, :+
61
64
 
62
65
  # Calculates a parallel combined resistance value.
63
66
  #
@@ -66,11 +69,12 @@ module Resistor
66
69
  def /(other)
67
70
  Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
68
71
  end
72
+ alias_method :|, :/
69
73
 
70
74
  # @return [Boolean] Whether or not the resistance value is the E12 series.
71
75
  def e12?
72
- num0 = Resistor::ColorCode::NUM[@code[0]]
73
- num1 = Resistor::ColorCode::NUM[@code[1]]
76
+ num0 = Resistor::ColorCode::DIGIT[@code[0]]
77
+ num1 = Resistor::ColorCode::DIGIT[@code[1]]
74
78
  Resistor::ColorCode::E12_SERIES.each do |key, val|
75
79
  if num0 == key
76
80
  return true if val.any? { |e| e == num1 }
@@ -81,8 +85,8 @@ module Resistor
81
85
 
82
86
  # @return [Boolean] Whether or not the resistance value is the E24 series.
83
87
  def e24?
84
- num0 = Resistor::ColorCode::NUM[@code[0]]
85
- num1 = Resistor::ColorCode::NUM[@code[1]]
88
+ num0 = Resistor::ColorCode::DIGIT[@code[0]]
89
+ num1 = Resistor::ColorCode::DIGIT[@code[1]]
86
90
  Resistor::ColorCode::E24_SERIES.each do |key, val|
87
91
  if num0 == key
88
92
  return true if val.any? { |e| e == num1 }
@@ -1,8 +1,7 @@
1
1
  module Resistor
2
2
  module ColorCode
3
3
 
4
- # 1st Band, 2nd Band
5
- NUM = {
4
+ DIGIT = {
6
5
  :black => 0,
7
6
  :brown => 1,
8
7
  :red => 2,
@@ -15,8 +14,7 @@ module Resistor
15
14
  :white => 9
16
15
  }.freeze
17
16
 
18
- # Multiplier
19
- MULT = {
17
+ MULTIPLIER = {
20
18
  :black => 0,
21
19
  :brown => 1,
22
20
  :red => 2,
@@ -28,8 +26,7 @@ module Resistor
28
26
  :silver => -2
29
27
  }.freeze
30
28
 
31
- # Tolerance
32
- ERROR_RANGE = {
29
+ TOLERANCE = {
33
30
  :brown => 1.0,
34
31
  :red => 2.0,
35
32
  :orange => 0.05,
@@ -67,28 +64,28 @@ module Resistor
67
64
  # The value must be between 0.1 and 99_000_000.
68
65
  #
69
66
  # @param ohm [Integer, Float] resistance value
70
- # @option options [Integer, Float] :error_range(5.0)
67
+ # @option options [Integer, Float] :tolerance(5.0)
71
68
  # @raise [ArgumentError] Error raised
72
69
  # when the supplied resistance value is less than 0.1.
73
70
  # @return [Array<Symbol>] color code
74
- def self.encode(ohm, options = {:error_range => 5.0})
75
- return [NUM.key(0)] if ohm == 0
71
+ def self.encode(ohm, options = {:tolerance => 5.0})
72
+ return [DIGIT.key(0)] if ohm == 0
76
73
  raise ArgumentError if ohm < 0.1
77
74
 
78
75
  if ohm < 1
79
76
  ohm_str = (ohm*100).to_s.split('')
80
- [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
81
- MULT.key(-2), ERROR_RANGE.key(options[:error_range])]
77
+ [DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
78
+ MULTIPLIER.key(-2), TOLERANCE.key(options[:tolerance])]
82
79
 
83
80
  elsif ohm < 10
84
81
  ohm_str = (ohm*10).to_s.split('')
85
- [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
86
- MULT.key(-1), ERROR_RANGE.key(options[:error_range])]
82
+ [DIGIT.key(ohm_str[0].to_i), DIGIT.key(ohm_str[1].to_i),
83
+ MULTIPLIER.key(-1), TOLERANCE.key(options[:tolerance])]
87
84
 
88
85
  else
89
86
  ohm_str = ohm.to_i.to_s.split('')
90
- [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
91
- MULT.key(ohm_str.size - 2), ERROR_RANGE.key(options[:error_range])]
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])]
92
89
  end
93
90
  end
94
91
 
@@ -105,7 +102,7 @@ module Resistor
105
102
  return 0.0 if code == [:black]
106
103
  raise ArgumentError if code[0] == :black
107
104
 
108
- ohm = (NUM[code[0]]*10 + NUM[code[1]]) * 10**MULT[code[2]]
105
+ ohm = (DIGIT[code[0]]*10 + DIGIT[code[1]]) * 10**MULTIPLIER[code[2]]
109
106
  return ohm.to_f
110
107
  end
111
108
  end
@@ -20,6 +20,7 @@ module Resistor
20
20
  def +(other)
21
21
  Resistor::CombinedResistor.new(@ohm + other.ohm)
22
22
  end
23
+ alias_method :-, :+
23
24
 
24
25
  # Calculates a parallel combined resistance value.
25
26
  #
@@ -28,5 +29,6 @@ module Resistor
28
29
  def /(other)
29
30
  Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
30
31
  end
32
+ alias_method :|, :/
31
33
  end
32
34
  end
@@ -1,3 +1,3 @@
1
1
  module Resistor
2
- VERSION = "0.2.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -5,70 +5,83 @@ require_relative "spec_helper"
5
5
  describe Resistor::BasicResistor do
6
6
 
7
7
  describe '::new' do
8
- let(:resistor) { Resistor.new(ohm: ohm, code: code) }
8
+ let(:resistor) { Resistor.new(arg) }
9
9
 
10
10
  context '抵抗値を指定した場合' do
11
- let(:ohm) { 4700 }
12
- let(:code) { nil }
11
+ let(:arg) { 4700 }
13
12
 
14
13
  it { expect(resistor.ohm).to eq 4700.0 }
15
14
  it { expect(resistor.code).to eq [:yellow, :purple, :red, :gold] }
16
- it { expect(resistor.error_range).to eq 5.0 }
15
+ it { expect(resistor.tolerance).to eq 5.0 }
17
16
  end
18
17
 
19
18
  context '抵抗値を指定した場合' do
20
- let(:ohm) { nil }
21
- let(:code) { ['brown', 'black', 'blue', 'silver'] }
19
+ let(:arg) { ['brown', 'black', 'blue', 'silver'] }
22
20
 
23
21
  it { expect(resistor.ohm).to eq 10_000_000 }
24
22
  it { expect(resistor.code).to eq [:brown, :black, :blue, :silver] }
25
- it { expect(resistor.error_range).to eq 10.0 }
23
+ it { expect(resistor.tolerance).to eq 10.0 }
26
24
  end
27
25
 
28
26
  context 'どちらも指定しなかった場合' do
29
- let(:ohm) { nil }
30
- let(:code) { nil }
27
+ let(:arg) { nil }
31
28
 
32
29
  it { expect { is_expected }.to raise_error(ArgumentError) }
33
30
  end
34
31
 
35
32
  context '許容差を指定した場合' do
36
- resistor = Resistor.new(ohm: 100, error_range: 0.1)
33
+ resistor = Resistor.new(100, tolerance: 0.1)
37
34
  it { expect(resistor.code[3]).to eq :purple }
38
- it { expect(resistor.error_range).to eq 0.1 }
35
+ it { expect(resistor.tolerance).to eq 0.1 }
39
36
  end
40
37
  end
41
38
 
42
39
  describe '#+, #/' do
43
40
  context '直列接続の合成抵抗' do
44
- r1 = Resistor.new(ohm: 100)
45
- r2 = Resistor.new(ohm: 200)
46
- r3 = Resistor.new(ohm: 300)
41
+ r1 = Resistor.new(100)
42
+ r2 = Resistor.new(200)
43
+ r3 = Resistor.new(300)
47
44
 
48
45
  it { expect((r1 + r2 + r3).ohm).to eq 600.0 }
49
46
  end
50
47
 
51
48
  context '並列接続の合成抵抗' do
52
- r1 = Resistor.new(ohm: 30)
53
- r2 = Resistor.new(ohm: 15)
54
- r3 = Resistor.new(ohm: 10)
49
+ r1 = Resistor.new(30)
50
+ r2 = Resistor.new(15)
51
+ r3 = Resistor.new(10)
55
52
 
56
53
  it { expect((r1 / r2 / r3).ohm).to eq 5.0 }
57
54
  end
58
55
 
59
56
  context '並列と直列を合わせた合成抵抗' do
60
- r1 = Resistor.new(ohm: 20)
61
- r2 = Resistor.new(ohm: 30)
62
- r3 = Resistor.new(ohm: 4)
63
- r4 = Resistor.new(ohm: 8)
57
+ r1 = Resistor.new(20)
58
+ r2 = Resistor.new(30)
59
+ r3 = Resistor.new(4)
60
+ r4 = Resistor.new(8)
64
61
 
65
62
  it { expect(((r1 / r2) + r3 + (r4 / r4)).ohm).to eq 20.0 }
66
63
  end
64
+
65
+ context '#+のエイリアスメソッドを使用した場合' do
66
+ r1 = Resistor.new(100)
67
+ original_name = (r1 + r1 + r1).ohm
68
+ new_name = (r1 - r1 - r1).ohm
69
+
70
+ it { expect(original_name == new_name).to be_truthy }
71
+ end
72
+
73
+ context '#/のエイリアスメソッドを使用した場合' do
74
+ r1 = Resistor.new(100)
75
+ original_name = (r1 / r1 / r1).ohm
76
+ new_name = (r1 | r1 | r1).ohm
77
+
78
+ it { expect(original_name == new_name).to be_truthy }
79
+ end
67
80
  end
68
81
 
69
82
  describe '#ohm=, #code=' do
70
83
  context '抵抗値を変更する場合' do
71
- resistor = Resistor.new(ohm: 100)
84
+ resistor = Resistor.new(100)
72
85
  resistor.ohm = 4.7
73
86
 
74
87
  it { expect(resistor.ohm).to eq 4.7 }
@@ -76,44 +89,44 @@ describe Resistor::BasicResistor do
76
89
  end
77
90
 
78
91
  context 'カラーコードを変更する場合' do
79
- resistor = Resistor.new(ohm: 100)
92
+ resistor = Resistor.new(100)
80
93
  resistor.code = ['green', 'brown', 'silver', 'brown']
81
94
 
82
95
  it { expect(resistor.ohm).to eq 0.51 }
83
96
  it { expect(resistor.code).to eq [:green, :brown, :silver, :brown] }
84
- it { expect(resistor.error_range).to eq 1.0 }
97
+ it { expect(resistor.tolerance).to eq 1.0 }
85
98
  end
86
99
  end
87
100
 
88
101
  describe '#e12?' do
89
102
  context 'E12系列である場合' do
90
- it { expect(Resistor.new(ohm: 0.12).e12?).to eq true }
91
- it { expect(Resistor.new(ohm: 3.3).e12?).to eq true }
92
- it { expect(Resistor.new(ohm: 47).e12?).to eq true }
93
- it { expect(Resistor.new(ohm: 82_000_000).e12?).to eq true }
103
+ it { expect(Resistor.new(0.12).e12?).to eq true }
104
+ it { expect(Resistor.new(3.3).e12?).to eq true }
105
+ it { expect(Resistor.new(47).e12?).to eq true }
106
+ it { expect(Resistor.new(82_000_000).e12?).to eq true }
94
107
  end
95
108
 
96
109
  context 'E12系列でない場合' do
97
- it { expect(Resistor.new(ohm: 0.13).e12?).to eq false }
98
- it { expect(Resistor.new(ohm: 3.8).e12?).to eq false }
99
- it { expect(Resistor.new(ohm: 55).e12?).to eq false }
100
- it { expect(Resistor.new(ohm: 70_000_000).e12?).to eq false }
110
+ it { expect(Resistor.new(0.13).e12?).to eq false }
111
+ it { expect(Resistor.new(3.8).e12?).to eq false }
112
+ it { expect(Resistor.new(55).e12?).to eq false }
113
+ it { expect(Resistor.new(70_000_000).e12?).to eq false }
101
114
  end
102
115
  end
103
116
 
104
117
  describe '#e24?' do
105
118
  context 'E24系列である場合' do
106
- it { expect(Resistor.new(ohm: 0.1).e24?).to eq true }
107
- it { expect(Resistor.new(ohm: 3).e24?).to eq true }
108
- it { expect(Resistor.new(ohm: 47_000).e24?).to eq true }
109
- it { expect(Resistor.new(ohm: 10_000_000).e24?).to eq true }
119
+ it { expect(Resistor.new(0.1).e24?).to eq true }
120
+ it { expect(Resistor.new(3).e24?).to eq true }
121
+ it { expect(Resistor.new(47_000).e24?).to eq true }
122
+ it { expect(Resistor.new(10_000_000).e24?).to eq true }
110
123
  end
111
124
 
112
125
  context 'E24系列でない場合' do
113
- it { expect(Resistor.new(ohm: 0.92).e24?).to eq false }
114
- it { expect(Resistor.new(ohm: 8.3).e24?).to eq false }
115
- it { expect(Resistor.new(ohm: 23_000).e24?).to eq false }
116
- it { expect(Resistor.new(ohm: 52_000_000).e24?).to eq false }
126
+ it { expect(Resistor.new(0.92).e24?).to eq false }
127
+ it { expect(Resistor.new(8.3).e24?).to eq false }
128
+ it { expect(Resistor.new(23_000).e24?).to eq false }
129
+ it { expect(Resistor.new(52_000_000).e24?).to eq false }
117
130
  end
118
131
  end
119
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: 0.2.1
4
+ version: 1.0.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-01-25 00:00:00.000000000 Z
11
+ date: 2015-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -34,6 +34,7 @@ files:
34
34
  - ".gitignore"
35
35
  - ".rspec"
36
36
  - Gemfile
37
+ - Guardfile
37
38
  - LICENSE.txt
38
39
  - README.md
39
40
  - Rakefile
@@ -66,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
66
67
  version: '0'
67
68
  requirements: []
68
69
  rubyforge_project:
69
- rubygems_version: 2.4.5
70
+ rubygems_version: 2.2.2
70
71
  signing_key:
71
72
  specification_version: 4
72
73
  summary: Resistor gem