Class: Shipping::Attribute

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
app/services/shipping/package.rb

Direct Known Subclasses

Length, Mass

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, unit) ⇒ Attribute

Returns a new instance of Attribute.

Raises:

  • (ArgumentError)


160
161
162
163
# File 'app/services/shipping/package.rb', line 160

def initialize(amount, unit)
  raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric)
  @amount, @unit = amount, unit.to_sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



195
196
197
198
199
200
201
202
# File 'app/services/shipping/package.rb', line 195

def method_missing(meth, *args)
  if args.size == 1 && self.class == (other = args.first).class
    other_amount_in_self_units = self.class.convert(other.amount, other.unit, self.unit)
    self.class.new(amount.send(meth, other_amount_in_self_units), self.unit)
  else
    amount.send(meth, *args)
  end
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



158
159
160
# File 'app/services/shipping/package.rb', line 158

def amount
  @amount
end

#unitObject (readonly)

Returns the value of attribute unit.



158
159
160
# File 'app/services/shipping/package.rb', line 158

def unit
  @unit
end

Class Method Details

.add_conversion(multiple_unit, other_unit, multiple) ⇒ Object



318
319
320
321
322
323
# File 'app/services/shipping/package.rb', line 318

def self.add_conversion(multiple_unit, other_unit, multiple)
  self.conversions[multiple_unit] ||={}
  self.conversions[multiple_unit][other_unit] = multiple
  self.conversions[other_unit] ||= {}
  self.conversions[other_unit][multiple_unit] = (1.0 / multiple)
end

.add_conversion_method_for(sym, options = {}) ⇒ Object



340
341
342
343
344
345
346
347
348
349
# File 'app/services/shipping/package.rb', line 340

def self.add_conversion_method_for(sym, options={})
  unit_name = sym.to_s
  class_eval do
    define_method("to_#{unit_name}") do
      return self if unit_name == self.unit.to_s
      self.class.new(self.class.convert(self.amount, self.unit, unit_name), unit_name)
    end
    alias_method("in_#{unit_name}","to_#{unit_name}")
  end
end

.add_methods_for(sym, options = {}) ⇒ Object



330
331
332
333
334
335
336
337
338
# File 'app/services/shipping/package.rb', line 330

def self.add_methods_for(sym, options={})
  add_conversion_method_for(sym, options)
  add_numeric_method = if options.has_key?(:add_numeric_methods)
    options[:add_numeric_methods]
  else
    self.add_numeric_methods
  end
  add_numeric_method_for(sym.to_s, options) if add_numeric_method
end

.add_numeric_method_for(unit_name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


351
352
353
354
355
356
357
358
359
360
# File 'app/services/shipping/package.rb', line 351

def self.add_numeric_method_for(unit_name, options={})
  unit_name = unit_name.to_sym
  raise ArgumentError, "#{unit_name.inspect} is not a unit in #{self.name}" unless units.include?(unit_name)
  klass = self
  Numeric.class_eval do
    define_method(unit_name) do
      klass.new(self, unit_name.to_sym)
    end
  end
end

.add_numeric_methods?Boolean

Returns:

  • (Boolean)


248
249
250
# File 'app/services/shipping/package.rb', line 248

def self.add_numeric_methods?
  self.add_numeric_methods
end

.add_to_system(unit_sym) ⇒ Object



283
284
285
286
287
288
289
290
291
# File 'app/services/shipping/package.rb', line 283

def self.add_to_system(unit_sym)
  if self.current_system
    self.units_to_systems[unit_sym] ||= begin
      sys_ary = self.systems_to_units[self.current_system] ||= []
      sys_ary << unit_sym
      self.current_system
    end
  end
end

.conversion_rate(from, to) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/services/shipping/package.rb', line 204

def self.conversion_rate(from, to)
  return nil unless self.conversions[from] and self.conversions[to]
  return self.conversions[from][to] ||=
  (1.0 / self.conversions[to][from] if self.conversions[to][from]) || begin
    shared_conversions = self.conversions[from].keys & self.conversions[to].keys
    if shared_conversions.any?
      primitive = shared_conversions.first
      self.conversions[from][primitive] * (1.0 / self.conversions[to][primitive])
    else
      self.conversions[from].each do |conversion_unit, multiple|
        if self.conversions[to].include?(conversion_unit)
          return multiple * conversion_rate(conversion) * (1.0 / self.conversions[to][conversion_unit])
        end
      end
      from_primitive = (self.conversions[from].keys & self.primitives).first
      to_primitive = (self.conversions[to].keys & self.primitives).first
      if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
        return self.conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / self.conversions[to][to_primitive])
      end
      raise StandardError, "No conversion path from #{from} to #{to}"
    end
  end
end

.convert(amount, from, to) ⇒ Object



325
326
327
328
# File 'app/services/shipping/package.rb', line 325

def self.convert(amount, from, to)
  from, to = from.to_sym, to.to_sym
  amount * conversion_rate(from, to)
end

.non_primitivesObject



240
241
242
# File 'app/services/shipping/package.rb', line 240

def self.non_primitives
  self.conversions.keys
end

.numeric_methods(*args) ⇒ Object



252
253
254
255
256
# File 'app/services/shipping/package.rb', line 252

def self.numeric_methods(*args)
  args.each do |arg|
    add_numeric_method_for(arg.to_sym)
  end
end

.one(sym, options = {}) ⇒ Object



293
294
295
296
297
298
# File 'app/services/shipping/package.rb', line 293

def self.one(sym, options={})
  unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
  add_to_system(unit_sym)
  register_unit(unit_sym, options[:is].unit, options[:is].amount)
  add_methods_for(unit_sym, options)
end

.primitive(sym, options = {}) ⇒ Object



276
277
278
279
280
281
# File 'app/services/shipping/package.rb', line 276

def self.primitive(sym, options={})
  unit_sym = (options[:plural] || sym.to_s.pluralize).to_sym
  self.primitives << unit_sym
  add_to_system(unit_sym)
  add_methods_for(unit_sym, options)
end

.primitivesObject



236
237
238
# File 'app/services/shipping/package.rb', line 236

def self.primitives
  read_inheritable_attribute(:primitives).dup
end

.register_unit(multiple_unit, other_unit, multiple) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'app/services/shipping/package.rb', line 300

def self.register_unit(multiple_unit, other_unit, multiple)
  multiple_unit, other_unit = multiple_unit.to_sym, other_unit.to_sym
  self.conversions[multiple_unit] ||= {}
  self.conversions[other_unit] ||= {}

  if self.primitives.include?(multiple_unit) || self.primitives.include?(other_unit)
    add_conversion(multiple_unit, other_unit, multiple)
  else
    [multiple_unit, other_unit].each do |this_unit|
      self.conversions[this_unit].each do |this_other_unit, this_multiple|
        if self.primitives.include?(this_other_unit)
          add_conversion(multiple_unit, this_other_unit, multiple * this_multiple)
        end
      end
    end
  end
end

.system(system_name, &block) ⇒ Object



269
270
271
272
273
274
# File 'app/services/shipping/package.rb', line 269

def self.system(system_name, &block)
  old_system = self.current_system
  self.current_system = system_name.to_sym
  yield
  self.current_system = old_system
end

.systemsObject



244
245
246
# File 'app/services/shipping/package.rb', line 244

def self.systems
  self.systems_to_units.keys
end

.units(system = nil) ⇒ Object



228
229
230
231
232
233
234
# File 'app/services/shipping/package.rb', line 228

def self.units(system=nil)
  if system
    self.systems_to_units[system.to_sym].dup
  else
    read_inheritable_attribute(:primitives) | self.conversions.keys
  end
end

Instance Method Details

#<=>(other) ⇒ Object



183
184
185
186
187
188
189
# File 'app/services/shipping/package.rb', line 183

def <=>(other)
  if self.class == other.class
    self.class.convert(self.amount, self.unit, other.unit) <=> other.amount
  else
    self.amount <=> other
  end
end

#==(other) ⇒ Object



173
174
175
176
177
# File 'app/services/shipping/package.rb', line 173

def ==(other)
  (BigDecimal(self&.amount&.to_s) == BigDecimal(other&.amount&.to_s) && self&.unit == other&.unit) || BigDecimal(self&.class&.convert(self&.amount, self&.unit, other&.unit)&.to_s) == BigDecimal(other&.amount&.to_s)
rescue NoMethodError
  self.amount == other
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
# File 'app/services/shipping/package.rb', line 179

def eql?(other)
  self.class == other.class && BigDecimal(self.amount.to_s) == BigDecimal(other.amount.to_s) && self.unit == other.unit
end

#inspectObject



169
170
171
# File 'app/services/shipping/package.rb', line 169

def inspect
  "#<#{self.class.name}: #{amount} #{unit}>"
end

#systemObject



191
192
193
# File 'app/services/shipping/package.rb', line 191

def system
  self.class.units_to_systems[unit]
end

#to_sObject



165
166
167
# File 'app/services/shipping/package.rb', line 165

def to_s
  "#{amount} #{unit}"
end