general_units 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: ee405e53d489845a7239d7301ff0d582caac6565
4
- data.tar.gz: 38c046bb71f0908432f04d0be231c3c2214793a4
3
+ metadata.gz: ba43bb6e8b19eba9b18fccc0c5d40fdebbc26c31
4
+ data.tar.gz: dee3ce3ab9873f719d6e4fc09707ef3a89d99562
5
5
  SHA512:
6
- metadata.gz: 36399caf5fef89a3d11771717d96d91031e986b5fc729f0443b34635fd17dd358990cb9d5bf7877ec38e3fad50976c8c934426c0634a34c586397e34909c58f7
7
- data.tar.gz: bee9e5c75aa9f697e56f488d64a1173c58a9f2dff345ee0a8c1f2e7fb9cc537fac1df4fafa30ca054ba8239525380bfb5370745aaa7b3cee0754d5ad7774afc8
6
+ metadata.gz: 6229d7a83ac3f0f58fc7177d3461323849ae609e4178121c1cadd9df72a86511e81c22a8d41cb85b0725cc5559cc311d81e66935776c0d4773e781b9c08c6e26
7
+ data.tar.gz: c8d68aec598958c1633f5b64b56124050c889d72ad80deaed48453e2c6dd21300907ba512bd0eabfa58862a0843a8ceb2d0349185feecdc757ffdc28e96eb842
@@ -8,11 +8,18 @@ module GeneralUnits
8
8
 
9
9
  attr_reader *VALUES, :unit
10
10
 
11
+ delegate :to_f, :to => :amount
12
+ delegate :hash, :to => :attributes
13
+
11
14
  def initialize(length = 0, width = 0, height = 0, unit)
12
15
  VALUES.each {|v| instance_variable_set(:"@#{v}", validate_value(eval(v), unit))}
13
16
  @unit = @height.unit
14
17
  end
15
18
 
19
+ def attributes
20
+ {:length => length, :width => width, :height => height, :unit => unit}
21
+ end
22
+
16
23
  def values
17
24
  VALUES.map {|v| self.send(v)}
18
25
  end
@@ -25,8 +32,12 @@ module GeneralUnits
25
32
  Box.new(*values.map {|v| v.convert_to(unit).amount}, unit)
26
33
  end
27
34
 
28
- def to_s(round = 2)
29
- "#{values.map(&:to_f).map {|f| f.round(round)}.join("x")} #{unit.short}"
35
+ def to_s(round = nil)
36
+ values.map {|d| d.to_s(round)}.join("x")
37
+ end
38
+
39
+ def formatted(round = nil)
40
+ "#{to_s(round)} #{unit.short}"
30
41
  end
31
42
 
32
43
  def accommodates?(*boxes)
@@ -59,7 +70,7 @@ module GeneralUnits
59
70
  self == other_object
60
71
  end
61
72
 
62
- def <=>(val)
73
+ def <=>(other_object)
63
74
  other_object = validate_capacity_or_length(other_object)
64
75
  amount <=> case other_object
65
76
  when Length then other_object
@@ -6,6 +6,14 @@ module GeneralUnits
6
6
  included do
7
7
  end
8
8
 
9
+ def weight_units_for_select
10
+ Weight::UNITS.map {|u| [u.name, u.code]}
11
+ end
12
+
13
+ def length_units_for_select
14
+ Length::UNITS.map {|u| [u.name, u.code]}
15
+ end
16
+
9
17
  end
10
18
 
11
19
  end
@@ -4,6 +4,200 @@ module GeneralUnits
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
+
8
+ def has_weight(*args)
9
+ options = args.extract_options!
10
+ prefix = args.first||:weight
11
+
12
+ class_attribute :_has_weight unless defined?(_has_weight)
13
+ self._has_weight = {prefix => {}}
14
+
15
+ self._has_weight[prefix][:amount_field] = amount_field = options[:amount]||:"#{prefix}_amount"
16
+ self._has_weight[prefix][:unit_field] = unit_field = options[:unit]||:"#{prefix}_unit"
17
+ self._has_weight[prefix][:default_unit] = default_unit = options[:default_unit]||:kilogram
18
+ self._has_weight[prefix][:default_unit_method] = default_unit_method = options[:default_unit_method]||:"deafult_#{prefix}_unit"
19
+
20
+ validates_inclusion_of unit_field, :in => Weight::UNITS.map {|u| u.code.to_s}, :if => Proc.new {|o| o.send(amount_field).present?}
21
+
22
+ class_eval <<-EOV
23
+
24
+ def #{unit_field}=(value)
25
+ if value.to_sym.in?(GeneralUnits::Weight::UNITS.map(&:code))
26
+ super(value.to_s)
27
+ else
28
+ raise ArgumentError, "Unprocessable unit: \#{value.inspect\}"
29
+ end
30
+ end
31
+
32
+ def #{prefix}
33
+ begin
34
+ if @_#{prefix}.nil? || #{amount_field}_changed? || #{unit_field}_changed?
35
+ @_#{prefix} = GeneralUnits::Weight.new(#{amount_field}, #{unit_field})
36
+ else
37
+ @_#{prefix}
38
+ end
39
+ rescue ArgumentError, TypeError
40
+ nil
41
+ end
42
+ end
43
+
44
+ def #{prefix}=(value)
45
+ case value
46
+ when GeneralUnits::Weight
47
+ self.#{amount_field} = value.amount
48
+ self.#{unit_field} = value.unit.code
49
+ when Array then
50
+ self.#{amount_field} = value[0]
51
+ self.#{unit_field} = value[1]||try(:#{default_unit_method})||:#{default_unit}
52
+ when Hash then
53
+ value = value.with_indifferent_access
54
+ self.#{amount_field} = value[:amount]
55
+ self.#{unit_field} = value[:unit]||try(:#{default_unit_method})||:#{default_unit}
56
+ when Numeric then
57
+ self.#{amount_field} = value
58
+ self.#{unit_field} = try(:#{default_unit_method})||:#{default_unit}
59
+ when nil then
60
+ self.#{amount_field} = nil
61
+ self.#{unit_field} = nil
62
+ end
63
+ #{prefix}
64
+ end
65
+ EOV
66
+ end
67
+
68
+ def has_length(*args)
69
+ options = args.extract_options!
70
+ prefix = args.first||:length
71
+
72
+ class_attribute :_has_length unless defined?(_has_length)
73
+ self._has_length = {prefix => {}}
74
+
75
+ self._has_length[prefix][:amount_field] = amount_field = options[:amount]||:"#{prefix}_amount"
76
+ self._has_length[prefix][:unit_field] = unit_field = options[:unit]||:"#{prefix}_unit"
77
+ self._has_length[prefix][:default_unit] = default_unit = options[:default_unit]||:centimeter
78
+ self._has_length[prefix][:default_unit_method] = default_unit_method = options[:default_unit_method]||:"deafult_#{prefix}_unit"
79
+
80
+ validates_inclusion_of unit_field, :in => Length::UNITS.map {|u| u.code.to_s}, :if => Proc.new {|o| o.send(amount_field).present?}
81
+
82
+ class_eval <<-EOV
83
+
84
+ def #{unit_field}=(value)
85
+ if value.to_sym.in?(GeneralUnits::Length::UNITS.map(&:code))
86
+ super(value.to_s)
87
+ else
88
+ raise ArgumentError, "Unprocessable unit: \#{value.inspect\}"
89
+ end
90
+ end
91
+
92
+ def #{prefix}
93
+ begin
94
+ if @_#{prefix}.nil? || #{amount_field}_changed? || #{unit_field}_changed?
95
+ @_#{prefix} = GeneralUnits::Length.new(#{amount_field}, #{unit_field})
96
+ else
97
+ @_#{prefix}
98
+ end
99
+ rescue ArgumentError, TypeError
100
+ nil
101
+ end
102
+ end
103
+
104
+ def #{prefix}=(value)
105
+ case value
106
+ when GeneralUnits::Length
107
+ self.#{amount_field} = value.amount
108
+ self.#{unit_field} = value.unit.code
109
+ when Array then
110
+ self.#{amount_field} = value[0]
111
+ self.#{unit_field} = value[1]||try(:#{default_unit_method})||:#{default_unit}
112
+ when Hash then
113
+ value = value.with_indifferent_access
114
+ self.#{amount_field} = value[:amount]
115
+ self.#{unit_field} = value[:unit]||try(:#{default_unit_method})||:#{default_unit}
116
+ when Numeric then
117
+ self.#{amount_field} = value
118
+ self.#{unit_field} = try(:#{default_unit_method})||:#{default_unit}
119
+ when nil then
120
+ self.#{amount_field} = nil
121
+ self.#{unit_field} = nil
122
+ end
123
+ #{prefix}
124
+ end
125
+ EOV
126
+ end
127
+
128
+ def has_box(*args)
129
+ options = args.extract_options!
130
+ prefix = args.first||:box
131
+
132
+ class_attribute :_has_box unless defined?(_has_box)
133
+ self._has_box = {prefix => {}}
134
+
135
+ self._has_box[prefix][:length_field] = length_field = options[:length]||:"#{prefix}_length"
136
+ self._has_box[prefix][:width_field] = width_field = options[:width]||:"#{prefix}_width"
137
+ self._has_box[prefix][:height_field] = height_field = options[:height]||:"#{prefix}_height"
138
+
139
+ self._has_box[prefix][:unit_field] = unit_field = options[:unit]||:"#{prefix}_unit"
140
+ self._has_box[prefix][:default_unit] = default_unit = options[:default_unit]||:centimeter
141
+ self._has_box[prefix][:default_unit_method] = default_unit_method = options[:default_unit_method]||:"deafult_#{prefix}_unit"
142
+
143
+ validates_inclusion_of unit_field, :in => Length::UNITS.map {|u| u.code.to_s}, :if => Proc.new {|o| o.send(length_field).present? || o.send(width_field).present? || o.send(height_field).present?}
144
+
145
+ class_eval <<-EOV
146
+
147
+ def #{unit_field}=(value)
148
+ if value.to_sym.in?(GeneralUnits::Length::UNITS.map(&:code))
149
+ super(value.to_s)
150
+ else
151
+ raise ArgumentError, "Unprocessable unit: \#{value.inspect\}"
152
+ end
153
+ end
154
+
155
+ def #{prefix}
156
+ begin
157
+ if @_#{prefix}.nil? || #{length_field}_changed? || #{width_field}_changed? || #{height_field}_changed? || #{unit_field}_changed?
158
+ @_#{prefix} = GeneralUnits::Box.new(#{length_field}, #{width_field}, #{height_field}, #{unit_field})
159
+ else
160
+ @_#{prefix}
161
+ end
162
+ rescue ArgumentError, TypeError
163
+ nil
164
+ end
165
+ end
166
+
167
+ def #{prefix}=(value)
168
+ case value
169
+ when GeneralUnits::Box then
170
+ self.#{length_field} = value.length
171
+ self.#{width_field} = value.width
172
+ self.#{height_field} = value.height
173
+ self.#{unit_field} = value.unit.code
174
+ when Array then
175
+ self.#{length_field} = value[0]
176
+ self.#{width_field} = value[1]
177
+ self.#{height_field} = value[2]
178
+ self.#{unit_field} = value[3]||try(:#{default_unit_method})||:#{default_unit}
179
+ when Hash then
180
+ value = value.with_indifferent_access
181
+ self.#{length_field} = value[:length]
182
+ self.#{width_field} = value[:width]
183
+ self.#{height_field} = value[:height]
184
+ self.#{unit_field} = value[:unit]||try(:#{default_unit_method})||:#{default_unit}
185
+ when Numeric then
186
+ self.#{length_field} = value
187
+ self.#{width_field} = value
188
+ self.#{height_field} = value
189
+ self.#{unit_field} = try(:#{default_unit_method})||:#{default_unit}
190
+ when nil then
191
+ self.#{length_field} = nil
192
+ self.#{width_field} = nil
193
+ self.#{height_field} = nil
194
+ self.#{unit_field} = nil
195
+ end
196
+ #{prefix}
197
+ end
198
+ EOV
199
+ end
200
+
7
201
  end
8
202
 
9
203
  end
@@ -5,9 +5,10 @@ module GeneralUnits
5
5
  ::GeneralUnits::Arithmetics.extend_class(self)
6
6
 
7
7
  class Unit
8
- attr_reader :name, :short, :millimeters
8
+ attr_reader :code, :name, :short, :millimeters
9
9
 
10
- def initialize(name, short, millimeters)
10
+ def initialize(code, name, short, millimeters)
11
+ @code = code
11
12
  @name = name.to_s
12
13
  @short = short.to_s
13
14
  @millimeters = millimeters.to_d
@@ -18,22 +19,23 @@ module GeneralUnits
18
19
  end
19
20
 
20
21
  def inspect
21
- "\"#{name}\""
22
+ code
22
23
  end
23
24
  end
24
25
 
25
- UNITS = {:mile_nautical => Unit.new("Mile (nautical)", "mln.", 1852000.0),
26
- :mile => Unit.new("Mile", "ml.", 1609344.0),
27
- :yard => Unit.new("Yard", "yrd.", 914.4),
28
- :foot => Unit.new("Foot", "ft.", 304.8),
29
- :inch => Unit.new("Inch", "in.", 25.4),
30
- :kilometer => Unit.new("Kilometer", "km.", 1000000.0),
31
- :meter => Unit.new("Meter", "m.", 1000.0),
32
- :centimeter => Unit.new("Centimeter", "cm.", 10.0),
33
- :millimeter => Unit.new("Millimeter", "mm.", 1.0)}
26
+ UNITS = [Unit.new(:mile_nautical, "Mile (nautical)", "mln.", 1852000.0),
27
+ Unit.new(:mile, "Mile", "ml.", 1609344.0),
28
+ Unit.new(:yard, "Yard", "yrd.", 914.4),
29
+ Unit.new(:foot, "Foot", "ft.", 304.8),
30
+ Unit.new(:inch, "Inch", "in.", 25.4),
31
+ Unit.new(:kilometer, "Kilometer", "km.", 1000000.0),
32
+ Unit.new(:meter, "Meter", "m.", 1000.0),
33
+ Unit.new(:centimeter, "Centimeter", "cm.", 10.0),
34
+ Unit.new(:millimeter, "Millimeter", "mm.", 1.0)]
34
35
 
35
36
  attr_reader :amount, :unit
36
37
  delegate :to_f, :to => :amount
38
+ delegate :hash, :to => :attributes
37
39
 
38
40
  def initialize(amount = 0, unit)
39
41
  if unit = valid_unit?(unit)
@@ -41,6 +43,10 @@ module GeneralUnits
41
43
  @unit = unit
42
44
  end
43
45
  end
46
+
47
+ def attributes
48
+ {:amount => amount, :unit => unit}
49
+ end
44
50
 
45
51
  def convert_to(unit)
46
52
  if convert_unit = valid_unit?(unit)
@@ -49,8 +55,12 @@ module GeneralUnits
49
55
  end
50
56
  end
51
57
 
52
- def to_s(round = 2)
53
- "#{to_f.round(round)} #{unit.short}"
58
+ def to_s(round = nil)
59
+ "#{to_f.round(round||2)}"
60
+ end
61
+
62
+ def formatted(round = nil)
63
+ "#{to_s(round)} #{unit.short}"
54
64
  end
55
65
 
56
66
  def to_length
@@ -61,10 +71,8 @@ module GeneralUnits
61
71
 
62
72
  def valid_unit?(unit)
63
73
  unit_object = case unit
64
- when String then UNITS[unit.to_sym]
65
- when Symbol then UNITS[unit]
74
+ when String, Symbol then UNITS.find {|u| u.code.to_s == unit.to_s}
66
75
  when Unit then unit
67
- else UNITS[unit.to_s.to_sym]
68
76
  end
69
77
  unit_object || raise(TypeError, "Unprocessable unit #{unit.inspect}")
70
78
  end
@@ -5,9 +5,10 @@ module GeneralUnits
5
5
  ::GeneralUnits::Arithmetics.extend_class(self)
6
6
 
7
7
  class Unit
8
- attr_reader :name, :short, :grams
8
+ attr_reader :code, :name, :short, :grams
9
9
 
10
- def initialize(name, short, grams)
10
+ def initialize(code, name, short, grams)
11
+ @code = code
11
12
  @name = name.to_s
12
13
  @short = short.to_s
13
14
  @grams = grams.to_d
@@ -18,21 +19,22 @@ module GeneralUnits
18
19
  end
19
20
 
20
21
  def inspect
21
- "\"#{name}\""
22
+ code
22
23
  end
23
24
  end
24
25
 
25
- UNITS = {:short_ton_us => Unit.new("Short ton (US)", "Sht.", 907184.74),
26
- :pound_us => Unit.new("Pound (US)", "Pnd.", 453.59237),
27
- :ounce_us => Unit.new("Ounce (US)", "Ounce", 28.349523),
28
- :stone => Unit.new("Stone", "Stn", 6350.2932),
29
- :long_ton_uk => Unit.new("Long Ton (UK)", "Lngt.", 1016046.9),
30
- :metric_ton => Unit.new("Metric Ton", "Ton", 1000000.0),
31
- :kilogram => Unit.new("Kilogram", "Kg.", 1000.0),
32
- :gram => Unit.new("Gram", "g.", 1.0)}
26
+ UNITS = [Unit.new(:short_ton_us, "Short ton (US)", "Sht.", 907184.74),
27
+ Unit.new(:pound_us, "Pound (US)", "Pnd.", 453.59237),
28
+ Unit.new(:ounce_us, "Ounce (US)", "Ounce", 28.349523),
29
+ Unit.new(:stone, "Stone", "Stn", 6350.2932),
30
+ Unit.new(:long_ton_uk, "Long Ton (UK)", "Lngt.", 1016046.9),
31
+ Unit.new(:metric_ton, "Metric Ton", "Ton", 1000000.0),
32
+ Unit.new(:kilogram, "Kilogram", "Kg.", 1000.0),
33
+ Unit.new(:gram, "Gram", "g.", 1.0)]
33
34
 
34
35
  attr_reader :amount, :unit
35
36
  delegate :to_f, :to => :amount
37
+ delegate :hash, :to => :attributes
36
38
 
37
39
  def initialize(amount = 0, unit)
38
40
  if unit = valid_unit?(unit)
@@ -40,6 +42,10 @@ module GeneralUnits
40
42
  @unit = unit
41
43
  end
42
44
  end
45
+
46
+ def attributes
47
+ {:amount => amount, :unit => unit}
48
+ end
43
49
 
44
50
  def convert_to(unit)
45
51
  if convert_unit = valid_unit?(unit)
@@ -48,8 +54,12 @@ module GeneralUnits
48
54
  end
49
55
  end
50
56
 
51
- def to_s(round = 2)
52
- "#{to_f.round(round)} #{unit.short}"
57
+ def to_s(round = nil)
58
+ "#{to_f.round(round||2)}"
59
+ end
60
+
61
+ def formatted(round = nil)
62
+ "#{to_s(round)} #{unit.short}"
53
63
  end
54
64
 
55
65
  def to_weight
@@ -60,10 +70,8 @@ module GeneralUnits
60
70
 
61
71
  def valid_unit?(unit)
62
72
  unit_object = case unit
63
- when String then UNITS[unit.to_sym]
64
- when Symbol then UNITS[unit]
73
+ when String, Symbol then UNITS.find {|u| u.code.to_s == unit.to_s}
65
74
  when Unit then unit
66
- else UNITS[unit.to_s.to_sym]
67
75
  end
68
76
  unit_object || raise(TypeError, "Unprocessable unit #{unit.inspect}")
69
77
  end
@@ -1,3 +1,3 @@
1
1
  module GeneralUnits
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
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.2
4
+ version: 0.0.3
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-16 00:00:00.000000000 Z
11
+ date: 2014-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n