subunit 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/subunit.rb +26 -5
- metadata +2 -2
data/lib/subunit.rb
CHANGED
@@ -3,15 +3,35 @@
|
|
3
3
|
|
4
4
|
class Subunit
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
attr_reader :to_a, :to_h
|
7
|
+
|
8
|
+
def initialize(raw_units=nil, raw_subunit=0)
|
9
|
+
method((raw_units.class.to_s.downcase + '_units').to_sym).call(raw_units, raw_subunit)
|
9
10
|
end
|
10
11
|
|
11
|
-
def to_a() @a.map{|x| x >= 1 ? x.to_i : 0} end
|
12
|
-
|
13
12
|
private
|
14
13
|
|
14
|
+
def hash_subunit(h)
|
15
|
+
[h.values.first, h.keys.first]
|
16
|
+
end
|
17
|
+
|
18
|
+
def fixnum_subunit(v)
|
19
|
+
[v, :remainder]
|
20
|
+
end
|
21
|
+
|
22
|
+
def hash_units(raw_units={}, u=nil)
|
23
|
+
val, label = method((u.class.to_s.downcase + '_subunit').to_sym).call(u)
|
24
|
+
units = multiply_units(raw_units.values).reverse
|
25
|
+
@to_a = scan_units(units, val).map(&:to_i)
|
26
|
+
@to_h = Hash[(raw_units.keys.reverse + [label]).zip(@to_a)]
|
27
|
+
end
|
28
|
+
|
29
|
+
def array_units(raw_units=[], u=nil)
|
30
|
+
val, label = method((u.class.to_s.downcase + '_subunit').to_sym).call(u)
|
31
|
+
units = multiply_units(raw_units).reverse
|
32
|
+
@to_a = scan_units(units, val).map(&:to_i)
|
33
|
+
end
|
34
|
+
|
15
35
|
def multiply_units(x) x.inject([]){|r,x| r + [x.to_i * (r[-1] || 1)]} end
|
16
36
|
|
17
37
|
def scan_units(unit_list,raw_subunit)
|
@@ -20,3 +40,4 @@ class Subunit
|
|
20
40
|
unit_list.length > 0 ? [unit] + scan_units(unit_list, subunit) : [unit, subunit]
|
21
41
|
end
|
22
42
|
end
|
43
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subunit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors: []
|
7
7
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2011-02-11 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|