resistor 0.0.1 → 0.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: 1f85a9cbd6fc0b7f2738f17d027c1e93cc8ba90b
4
- data.tar.gz: 4ef9a37aa0a495de046ed8015c01e1e1e7c08451
3
+ metadata.gz: b380cd33929b8b61653a914b7979f718b6b28d12
4
+ data.tar.gz: 00071b06604e805d3abb8e061e1ad401ccab1413
5
5
  SHA512:
6
- metadata.gz: 3e6750a86135375473e8f40f2bef63423eede4a457a0e917f0f7eb7dcab9440e51b54501ca2c16974b514bb1ea79703b526ad8a2e361d16dc9f838d4db8eedfc
7
- data.tar.gz: deb1bc335db3774716bf8e03bf1322bd7ed1a3d95d5ab9a59493e372d92b37b18090d35222583dbf8b5ac88fcb40d4f653b38aa996f5ade28c5101a49764d611
6
+ metadata.gz: 7065ee63b363a8532a62118d77e0141c8cc3ad9d6ecd9521971a1465e3cd0895c31b6e3e717cbf91496147e81e6361a2038cd870b237a344fc0a747ec4931167
7
+ data.tar.gz: 07fd9e498c2d20579330312d566af7dd8e92cc7fee67d7f6d34b96746db148e4acf89abb194844ac9225e641148779a4e73ae2b93673a366e24a9e40f884461e
data/.gitignore CHANGED
@@ -7,6 +7,7 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /test
10
11
  *.bundle
11
12
  *.so
12
13
  *.o
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format doc
data/README.md CHANGED
@@ -2,26 +2,39 @@
2
2
 
3
3
  ## Installation
4
4
 
5
- Add this line to your application's Gemfile:
5
+ Add this line to your application's Gemfile:
6
6
 
7
7
  ```ruby
8
8
  gem 'resistor'
9
9
  ```
10
10
 
11
- And then execute:
11
+ And then execute:
12
12
 
13
- $ bundle
13
+ $ bundle install
14
14
 
15
- Or install it yourself as:
15
+ Or install it yourself as:
16
16
 
17
17
  $ gem install resistor
18
18
 
19
19
  ## Usage
20
20
 
21
- ## Contributing
21
+ ```ruby
22
+ require 'resistor'
23
+
24
+ # Resistor Color Code Calculation
25
+ Resistor::ColorCode.encode(4700) # => [:yellow, :purple, :red, :gold]
26
+ Resistor::ColorCode.decode([:yellow, :purple, :red, :gold]) # => 4700.0
27
+
28
+ # Combined Resistance Calculation
29
+ # --[r1]-- --[r4]--
30
+ # --| |--[r3]--| |--
31
+ # --[r2]-- --[r4]--
22
32
 
23
- 1. Fork it ( https://github.com/[my-github-username]/resistor/fork )
24
- 2. Create your feature branch (`git checkout -b my-new-feature`)
25
- 3. Commit your changes (`git commit -am 'Add some feature'`)
26
- 4. Push to the branch (`git push origin my-new-feature`)
27
- 5. Create a new Pull Request
33
+ r1 = Resistor.new(ohm: 20)
34
+ r2 = Resistor.new(ohm: 30)
35
+ r3 = Resistor.new(ohm: 4)
36
+ r4 = Resistor.new(ohm: 8)
37
+
38
+ r5 = (r1 / r2) + r3 + (r4 / r4)
39
+ r5.ohm # => 20.0
40
+ ```
data/lib/resistor.rb CHANGED
@@ -1,5 +1,13 @@
1
1
  require "resistor/version"
2
+ require "resistor/color_code"
3
+ require "resistor/basic_resistor"
4
+ require "resistor/combined_resistor"
2
5
 
3
6
  module Resistor
4
- # Your code goes here...
7
+
8
+ class << self
9
+ def new(opt = {})
10
+ Resistor::BasicResistor.new(opt)
11
+ end
12
+ end
5
13
  end
@@ -0,0 +1,41 @@
1
+ module Resistor
2
+ class BasicResistor
3
+
4
+ attr_reader :ohm, :code, :error_range
5
+
6
+ def initialize(ohm:nil, code:nil, error_range: 5.0)
7
+ opt = {error_range: error_range}
8
+ if ohm
9
+ @ohm = ohm.to_f
10
+ @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)
15
+ @ohm = Resistor::ColorCode.decode(@code)
16
+ @error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
17
+ else
18
+ raise ArgumentError
19
+ end
20
+ end
21
+
22
+ def ohm=(ohm)
23
+ @ohm = ohm.to_f
24
+ @code = Resistor::ColorCode.encode(@ohm)
25
+ end
26
+
27
+ def code=(code)
28
+ @code = code.map(&:to_sym)
29
+ @ohm = Resistor::ColorCode.decode(@code)
30
+ @error_range = Resistor::ColorCode::ERROR_RANGE[@code[3].to_sym]
31
+ end
32
+
33
+ def +(other)
34
+ Resistor::CombinedResistor.new(@ohm + other.ohm)
35
+ end
36
+
37
+ def /(other)
38
+ Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,79 @@
1
+ module Resistor
2
+ module ColorCode
3
+
4
+ # 1桁目、2桁目の数値
5
+ NUM = {
6
+ :black => 0,
7
+ :brown => 1,
8
+ :red => 2,
9
+ :orange => 3,
10
+ :yellow => 4,
11
+ :green => 5,
12
+ :blue => 6,
13
+ :purple => 7,
14
+ :gray => 8,
15
+ :white => 9
16
+ }.freeze
17
+
18
+ # 3桁目で指定される乗数
19
+ MULT = {
20
+ :black => 0,
21
+ :brown => 1,
22
+ :red => 2,
23
+ :orange => 3,
24
+ :yellow => 4,
25
+ :green => 5,
26
+ :blue => 6,
27
+ :gold => -1,
28
+ :silver => -2
29
+ }.freeze
30
+
31
+ # 4桁目で指定される誤差範囲
32
+ ERROR_RANGE = {
33
+ :brown => 1.0,
34
+ :red => 2.0,
35
+ :orange => 0.05,
36
+ :green => 0.5,
37
+ :blue => 0.25,
38
+ :purple => 0.1,
39
+ :gold => 5.0,
40
+ :silver => 10.0
41
+ }.freeze
42
+
43
+ # 抵抗値 -> カラーコード
44
+ def self.encode(ohm, error_range: 5.0)
45
+ return [NUM.key(0)] if ohm == 0
46
+ raise ArgumentError if ohm < 0.1
47
+
48
+ # 0.1以上で1より小さい
49
+ if ohm < 1
50
+ ohm_str = (ohm*100).to_s.split('')
51
+ [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
52
+ MULT.key(-2), ERROR_RANGE.key(error_range)]
53
+
54
+ # 1以上で10より小さい(1桁)
55
+ elsif ohm < 10
56
+ ohm_str = (ohm*10).to_s.split('')
57
+ [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
58
+ MULT.key(-1), ERROR_RANGE.key(error_range)]
59
+
60
+ # 2桁以上
61
+ else
62
+ ohm_str = ohm.to_i.to_s.split('')
63
+ [NUM.key(ohm_str[0].to_i), NUM.key(ohm_str[1].to_i),
64
+ MULT.key(ohm_str.size - 2), ERROR_RANGE.key(error_range)]
65
+ end
66
+ end
67
+
68
+ # カラーコード -> 抵抗値
69
+ def self.decode(code)
70
+ raise ArgumentError unless code.is_a? Array
71
+ code = code.map(&:to_sym)
72
+ return 0.0 if code == [:black]
73
+ raise ArgumentError if code[0] == :black
74
+
75
+ ohm = (NUM[code[0]]*10 + NUM[code[1]]) * 10**MULT[code[2]]
76
+ return ohm.to_f
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,18 @@
1
+ module Resistor
2
+ class CombinedResistor
3
+
4
+ attr_reader :ohm
5
+
6
+ def initialize(ohm)
7
+ @ohm = ohm.to_f
8
+ end
9
+
10
+ def +(other)
11
+ Resistor::CombinedResistor.new(@ohm + other.ohm)
12
+ end
13
+
14
+ def /(other)
15
+ Resistor::CombinedResistor.new(1 / (1 / @ohm + 1 / other.ohm))
16
+ end
17
+ end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module Resistor
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/resistor.gemspec CHANGED
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.1"
23
24
  end
@@ -0,0 +1,81 @@
1
+ # coding: utf-8
2
+
3
+ require_relative "spec_helper"
4
+
5
+ describe Resistor::BasicResistor do
6
+
7
+ describe '::new' do
8
+ let(:resistor) { Resistor.new(ohm: ohm, code: code) }
9
+
10
+ context '抵抗値を指定した場合' do
11
+ let(:ohm) { 4700 }
12
+ let(:code) { nil }
13
+
14
+ it { expect(resistor.ohm).to eq 4700.0 }
15
+ it { expect(resistor.code).to eq [:yellow, :purple, :red, :gold] }
16
+ it { expect(resistor.error_range).to eq 5.0 }
17
+ end
18
+
19
+ context '抵抗値を指定した場合' do
20
+ let(:ohm) { nil }
21
+ let(:code) { ['brown', 'black', 'blue', 'silver'] }
22
+
23
+ it { expect(resistor.ohm).to eq 10_000_000 }
24
+ it { expect(resistor.code).to eq [:brown, :black, :blue, :silver] }
25
+ it { expect(resistor.error_range).to eq 10.0 }
26
+ end
27
+
28
+ context 'どちらも指定しなかった場合' do
29
+ let(:ohm) { nil }
30
+ let(:code) { nil }
31
+
32
+ it { expect { is_expected }.to raise_error(ArgumentError) }
33
+ end
34
+ end
35
+
36
+ describe '#+, #/' do
37
+ context '直列接続の合成抵抗' do
38
+ r1 = Resistor.new(ohm: 100)
39
+ r2 = Resistor.new(ohm: 200)
40
+ r3 = Resistor.new(ohm: 300)
41
+
42
+ it { expect((r1 + r2 + r3).ohm).to eq 600.0 }
43
+ end
44
+
45
+ context '並列接続の合成抵抗' do
46
+ r1 = Resistor.new(ohm: 30)
47
+ r2 = Resistor.new(ohm: 15)
48
+ r3 = Resistor.new(ohm: 10)
49
+
50
+ it { expect((r1 / r2 / r3).ohm).to eq 5.0 }
51
+ end
52
+
53
+ context '並列と直列を合わせた合成抵抗' do
54
+ r1 = Resistor.new(ohm: 20)
55
+ r2 = Resistor.new(ohm: 30)
56
+ r3 = Resistor.new(ohm: 4)
57
+ r4 = Resistor.new(ohm: 8)
58
+
59
+ it { expect(((r1 / r2) + r3 + (r4 / r4)).ohm).to eq 20.0 }
60
+ end
61
+ end
62
+
63
+ describe '#ohm=, #code=' do
64
+ context '抵抗値を変更する場合' do
65
+ resistor = Resistor.new(ohm: 100)
66
+ resistor.ohm = 4.7
67
+
68
+ it { expect(resistor.ohm).to eq 4.7 }
69
+ it { expect(resistor.code).to eq [:yellow, :purple, :gold, :gold] }
70
+ end
71
+
72
+ context 'カラーコードを変更する場合' do
73
+ resistor = Resistor.new(ohm: 100)
74
+ resistor.code = ['green', 'brown', 'silver', 'brown']
75
+
76
+ it { expect(resistor.ohm).to eq 0.51 }
77
+ it { expect(resistor.code).to eq [:green, :brown, :silver, :brown] }
78
+ it { expect(resistor.error_range).to eq 1.0 }
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,69 @@
1
+ # coding: utf-8
2
+
3
+ require_relative "spec_helper"
4
+
5
+ describe Resistor::ColorCode do
6
+
7
+ describe '::encode' do
8
+ subject { Resistor::ColorCode.encode(ohm) }
9
+
10
+ context '0Ωの場合' do
11
+ let(:ohm) { 0 }
12
+ it { is_expected.to eq [:black] }
13
+ end
14
+
15
+ context '0.1Ω以上1Ωより小さい場合' do
16
+ let(:ohm) { 0.24 }
17
+ it { is_expected.to eq [:red, :yellow, :silver, :gold] }
18
+ end
19
+
20
+ context '1Ω以上10Ωより小さい場合' do
21
+ let(:ohm) { 2 }
22
+ it { is_expected.to eq [:red, :black, :gold, :gold] }
23
+ end
24
+
25
+ context '10Ω以上の場合' do
26
+ let(:ohm) { 3300 }
27
+ it { is_expected.to eq [:orange, :orange, :red, :gold] }
28
+ end
29
+
30
+ context '0Ωより小さい場合' do
31
+ let(:ohm) { -10 }
32
+ it { expect { is_expected }.to raise_error(ArgumentError) }
33
+ end
34
+
35
+ context '0.1Ωより小さい場合' do
36
+ let(:ohm) { 0.01 }
37
+ it { expect { is_expected }.to raise_error(ArgumentError) }
38
+ end
39
+ end
40
+
41
+ describe '::decode' do
42
+ subject { Resistor::ColorCode.decode(code) }
43
+
44
+ context '0Ωの場合' do
45
+ let(:code) { [:black] }
46
+ it { is_expected.to eq 0 }
47
+ end
48
+
49
+ context '0.1Ω以上1Ωより小さい場合' do
50
+ let(:code) { [:blue, :red, :silver, :gold] }
51
+ it { is_expected.to eq 0.62 }
52
+ end
53
+
54
+ context '1Ω以上10Ωより小さい場合' do
55
+ let(:code) { [:white, :brown, :gold, :gold] }
56
+ it { is_expected.to eq 9.1 }
57
+ end
58
+
59
+ context '10Ω以上の場合' do
60
+ let(:code) { [:brown, :black, :blue, :gold] }
61
+ it { is_expected.to eq 10_000_000 }
62
+ end
63
+
64
+ context '1本目に黒が指定された場合' do
65
+ let(:code) { [:black, :blue, :blue, :gold] }
66
+ it { expect { is_expected }.to raise_error(ArgumentError) }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ # coding: utf-8
2
+
3
+ $:.unshift File.expand_path '../lib', File.dirname(__FILE__)
4
+ require 'resistor'
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.0.1
4
+ version: 0.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-01-13 00:00:00.000000000 Z
11
+ date: 2015-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
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'
41
55
  description: gem for resistor
42
56
  email:
43
57
  - seinosuke.3606@gmail.com
@@ -46,13 +60,20 @@ extensions: []
46
60
  extra_rdoc_files: []
47
61
  files:
48
62
  - ".gitignore"
63
+ - ".rspec"
49
64
  - Gemfile
50
65
  - LICENSE.txt
51
66
  - README.md
52
67
  - Rakefile
53
68
  - lib/resistor.rb
69
+ - lib/resistor/basic_resistor.rb
70
+ - lib/resistor/color_code.rb
71
+ - lib/resistor/combined_resistor.rb
54
72
  - lib/resistor/version.rb
55
73
  - resistor.gemspec
74
+ - spec/basic_resistor_spec.rb
75
+ - spec/color_code_spec.rb
76
+ - spec/spec_helper.rb
56
77
  homepage: ''
57
78
  licenses:
58
79
  - MIT
@@ -77,4 +98,7 @@ rubygems_version: 2.4.5
77
98
  signing_key:
78
99
  specification_version: 4
79
100
  summary: Resistor gem
80
- test_files: []
101
+ test_files:
102
+ - spec/basic_resistor_spec.rb
103
+ - spec/color_code_spec.rb
104
+ - spec/spec_helper.rb