general_units 0.0.3 → 0.0.4

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: ba43bb6e8b19eba9b18fccc0c5d40fdebbc26c31
4
- data.tar.gz: dee3ce3ab9873f719d6e4fc09707ef3a89d99562
3
+ metadata.gz: 0b587736e5f764d47a5633f38ae9e871078a9d2d
4
+ data.tar.gz: 91594f7393deb805d8a15ffbdeddaeaded832e18
5
5
  SHA512:
6
- metadata.gz: 6229d7a83ac3f0f58fc7177d3461323849ae609e4178121c1cadd9df72a86511e81c22a8d41cb85b0725cc5559cc311d81e66935776c0d4773e781b9c08c6e26
7
- data.tar.gz: c8d68aec598958c1633f5b64b56124050c889d72ad80deaed48453e2c6dd21300907ba512bd0eabfa58862a0843a8ceb2d0349185feecdc757ffdc28e96eb842
6
+ metadata.gz: 4ec7e49bc0fb3bf97304216b650fbb1f23db262ca31475db84be92448b116e2d9e033be3a3002e3ab176dc9d715d5cdf30a79cb112614c321bad755de23eb98c
7
+ data.tar.gz: f75851118b3fcbbeda331e3999d2a6d205984ffd6fac174ea65512f4ce84eb552078055c9f729323c7399e6213ca6f0b9bf060085437096a2537417f98e708dc
@@ -8,7 +8,7 @@ module GeneralUnits
8
8
 
9
9
  attr_reader *VALUES, :unit
10
10
 
11
- delegate :to_f, :to => :amount
11
+ delegate :to_f, :to => :volume
12
12
  delegate :hash, :to => :attributes
13
13
 
14
14
  def initialize(length = 0, width = 0, height = 0, unit)
@@ -23,10 +23,18 @@ module GeneralUnits
23
23
  def values
24
24
  VALUES.map {|v| self.send(v)}
25
25
  end
26
-
26
+
27
27
  def amount
28
+ values.sum
29
+ end
30
+
31
+ def volume
28
32
  length * width * height
29
33
  end
34
+
35
+ def has_space?
36
+ length > 0 && width > 0 && height > 0
37
+ end
30
38
 
31
39
  def convert_to(unit)
32
40
  Box.new(*values.map {|v| v.convert_to(unit).amount}, unit)
@@ -42,7 +50,7 @@ module GeneralUnits
42
50
 
43
51
  def accommodates?(*boxes)
44
52
  boxes.map! {|box| validate_capacity(box)}
45
- boxes.sum(&:amount) < amount && includes?(Box.new(boxes.map(&:length).max, boxes.map(&:width).max, boxes.map(&:height).max, unit))
53
+ boxes.sum(&:volume) < volume && includes?(Box.new(boxes.map(&:length).max, boxes.map(&:width).max, boxes.map(&:height).max, unit))
46
54
  end
47
55
 
48
56
  def same_size?(other_object)
@@ -64,6 +72,8 @@ module GeneralUnits
64
72
  def ==(other_object)
65
73
  other_object = validate_capacity(other_object)
66
74
  length == other_object.length && width == other_object.width && height == other_object.height
75
+ rescue
76
+ false
67
77
  end
68
78
 
69
79
  def eql?(other_object)
@@ -72,45 +82,45 @@ module GeneralUnits
72
82
 
73
83
  def <=>(other_object)
74
84
  other_object = validate_capacity_or_length(other_object)
75
- amount <=> case other_object
85
+ volume <=> case other_object
76
86
  when Length then other_object
77
- when Box then other_object.amount
87
+ when Box then other_object.volume
78
88
  end
79
89
  end
80
90
 
81
91
  def >(other_object)
82
92
  other_object = validate_capacity_or_length(other_object)
83
- amount > case other_object
93
+ volume > case other_object
84
94
  when Length then other_object
85
- when Box then other_object.amount
95
+ when Box then other_object.volume
86
96
  end
87
97
  end
88
98
 
89
99
  def <(other_object)
90
100
  other_object = validate_capacity_or_length(other_object)
91
- amount < case other_object
101
+ volume < case other_object
92
102
  when Length then other_object
93
- when Box then other_object.amount
103
+ when Box then other_object.volume
94
104
  end
95
105
  end
96
106
 
97
107
  def >=(other_object)
98
108
  other_object = validate_capacity_or_length(other_object)
99
- amount >= case other_object
109
+ volume >= case other_object
100
110
  when Length then other_object
101
- when Box then other_object.amount
111
+ when Box then other_object.volume
102
112
  end
103
113
  end
104
114
 
105
115
  def <=(other_object)
106
116
  other_object = validate_capacity_or_length(other_object)
107
- amount <= case other_object
117
+ volume <= case other_object
108
118
  when Length then other_object
109
- when Box then other_object.amount
119
+ when Box then other_object.volume
110
120
  end
111
121
  end
112
122
 
113
- delegate :positive?, :negative?, :to => :amount
123
+ delegate :positive?, :negative?, :to => :volume
114
124
 
115
125
  def +(other_object)
116
126
  other_object = validate_capacity_or_length(other_object)
@@ -144,7 +154,7 @@ module GeneralUnits
144
154
  end
145
155
  end
146
156
 
147
- delegate :div, :divmod, :modulo, :%, :remainder, :abs, :zero?, :nonzero?, :coerce, :to => :amount
157
+ delegate :div, :divmod, :modulo, :%, :remainder, :abs, :zero?, :nonzero?, :coerce, :to => :volume
148
158
 
149
159
  ### ARITHMETICS END ###
150
160
 
@@ -56,7 +56,7 @@ module GeneralUnits
56
56
  end
57
57
 
58
58
  def to_s(round = nil)
59
- "#{to_f.round(round||2)}"
59
+ "#{to_f.divmod(1).last == 0 ? to_f.round(0) : to_f.round(round||2)}"
60
60
  end
61
61
 
62
62
  def formatted(round = nil)
@@ -55,7 +55,7 @@ module GeneralUnits
55
55
  end
56
56
 
57
57
  def to_s(round = nil)
58
- "#{to_f.round(round||2)}"
58
+ "#{to_f.divmod(1).last == 0 ? to_f.round(0) : to_f.round(round||2)}"
59
59
  end
60
60
 
61
61
  def formatted(round = nil)
@@ -1,3 +1,3 @@
1
1
  module GeneralUnits
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: general_units
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valery Kvon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-22 00:00:00.000000000 Z
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n