numeric_with_unit 0.0.2 → 0.0.3

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: f33f4241debd10d0f422dffd58afaa2ea4ff81bb
4
- data.tar.gz: d75d05e2524f9f840a9019ed7cb17b2f0acd7071
3
+ metadata.gz: 5a9e3bf253a2b7b59618f970d6c3dbdd22ff33b7
4
+ data.tar.gz: 13adf31a8364d7ffa4e88c283ba0b67738c114e9
5
5
  SHA512:
6
- metadata.gz: 7edf5db65fff4bcd7dffbc9e035da919f60a079ca2583a95fd6acb47019ec3f7b11738ad728ff6117d33ca451b9d97971f2e246ba6772625bcc89274bb66d97e
7
- data.tar.gz: f23d463f342e2d2ed4b1d0dc8b674b3e71030041e3500ff7740299ea0458856d6baec99a9a9d3fa5a2cee44549eed882f14a56aa862baa24e69220748b3e1a85
6
+ metadata.gz: 5938fcabeb8013050901f66e98888a78a02ab65fb175b3341747e91913044b570efe5c3d18a36683a388ab341b05877759200824e269b26bbe78e744e932434f
7
+ data.tar.gz: 1154f4a257116d39df130e411e9c3021bbb4519ec851d76d345f43405cf3a5345d1ad7680e4d0d4b9d9e4123a2e9f4f06e1c36e5e1a04c7fa1cc1fd2e4be1f04
@@ -1,20 +1,17 @@
1
1
  # coding: utf-8
2
2
 
3
-
4
3
  class NumericWithUnit
5
4
  class Unit
6
5
  class Config
7
- attr_reader :symbol, :dimension, :derivation
6
+ attr_reader :symbol, :dimension
8
7
  attr_reader :si
9
8
 
10
- def initialize(parent=nil)
9
+ def initialize()
11
10
  @symbol = nil
12
11
  @dimension = Hash.new(0)
13
12
  @from_si = nil
14
13
  @to_si = nil
15
14
  @si = false
16
-
17
- @parent = parent
18
15
  end
19
16
 
20
17
  def compile
@@ -23,9 +20,6 @@ class NumericWithUnit
23
20
  @from_si ||= ->(x){x}
24
21
  @to_si ||= ->(x){x}
25
22
 
26
- @derivation = Hash.new(0)
27
- @derivation[@parent] += 1 unless @parent.nil?
28
-
29
23
  self
30
24
  end
31
25
 
@@ -122,9 +116,10 @@ class NumericWithUnit
122
116
 
123
117
  h = derivation.sort_by{|u,v| u.symbol}.sort_by{|u,v| v} # ←どうしよう
124
118
 
125
- s1 = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
126
- s2 = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}.join('.')
127
- symbol = s1 + (s2.empty? ? '' : "/(#{s2})")
119
+ syms_pos = h.select{|u,v| v > 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
120
+ syms_neg = h.select{|u,v| v < 0}.map{|u,v| u.symbol + ((v.abs>1) ? v.abs.to_s : '')}
121
+ symbol = syms_pos.join('.')
122
+ symbol += '/' + (syms_neg.size==1 ? "#{syms_neg.first}" : "(#{syms_neg.join('.')})") unless syms_neg.empty?
128
123
 
129
124
  derivation.each do |u,v|
130
125
  u.dimension.each do |d,i|
@@ -158,11 +153,11 @@ class NumericWithUnit
158
153
  ->(x){memo[prc[x]]}
159
154
  }
160
155
 
161
- self.new{|conf|
162
- conf.symbol = symbol
163
- conf.dimension = dimension
164
- conf.from_si = from_si
165
- conf.to_si = to_si
156
+ self.new(derivation){|config|
157
+ config.symbol = symbol
158
+ config.dimension = dimension
159
+ config.from_si = from_si
160
+ config.to_si = to_si
166
161
  }
167
162
  end
168
163
 
@@ -327,9 +322,9 @@ class NumericWithUnit
327
322
  attr_reader :symbol
328
323
  attr_reader :dimension, :derivation
329
324
 
330
- def initialize
325
+ def initialize(derivation=nil)
331
326
  # Unit::Configとinitializeの役割が分離できていないので見なおせ
332
- config = Config.new(self)
327
+ config = Config.new
333
328
  yield(config) if block_given?
334
329
  config.compile
335
330
 
@@ -338,17 +333,21 @@ class NumericWithUnit
338
333
  @from_si = config.from_si
339
334
  @to_si = config.to_si
340
335
 
341
- @derivation = config.derivation
336
+ unless derivation
337
+ derivation = Hash.new(0)
338
+ derivation[self] += 1
339
+ end
340
+ @derivation = derivation
342
341
  end
343
342
 
344
343
  # create new unit with new symbol and factor from self.
345
344
  # use for converting [in] = 25.4[mm] .
346
345
  def cast(new_symbol, factor = 1)
347
- self.class.new do |conf|
348
- conf.symbol = new_symbol
349
- conf.dimension = @dimension
350
- conf.from_si = ->(x){from_si(x.quo(factor))}
351
- conf.to_si = ->(x){to_si(x * factor)}
346
+ self.class.new do |config|
347
+ config.symbol = new_symbol
348
+ config.dimension = @dimension
349
+ config.from_si = ->(x){from_si(x.quo(factor))}
350
+ config.to_si = ->(x){to_si(x * factor)}
352
351
  end
353
352
  end
354
353
 
@@ -409,6 +408,7 @@ class NumericWithUnit
409
408
  end
410
409
  end
411
410
  end
411
+
412
412
  end
413
413
 
414
414
  class Unit
@@ -187,8 +187,11 @@ end
187
187
 
188
188
  class String
189
189
  def to_nwu(mthd=:to_r)
190
- m = self.match /(?<value>.+) (?<unit>.+)/ # 適当
191
- NumericWithUnit.new(m[:value].__send__(mthd), m[:unit])
190
+ # 適当
191
+ m = self.match /.*?(?=[\s\(\[])/
192
+ value = m.to_s
193
+ unit = m.post_match.strip.gsub(/^\[|\]$/, '')
194
+ NumericWithUnit.new(value.__send__(mthd), unit)
192
195
  end
193
196
  end
194
197
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numeric_with_unit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - diaphragm
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-31 00:00:00.000000000 Z
11
+ date: 2015-02-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This gem provide NumericWithUnit class to calculate numeric with unit
14
14
  of measurement.