Class: Shipping::Attribute

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

Overview

Service object: attribute.

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)


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

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

  @amount = amount
  @unit = unit.to_sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



200
201
202
203
204
205
206
207
# File 'app/services/shipping/package.rb', line 200

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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#unitObject (readonly)

Returns the value of attribute unit.



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

def unit
  @unit
end

Class Method Details

.add_conversion(multiple_unit, other_unit, multiple) ⇒ Object



322
323
324
325
326
327
# File 'app/services/shipping/package.rb', line 322

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

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



345
346
347
348
349
350
351
352
353
354
355
# File 'app/services/shipping/package.rb', line 345

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 == unit.to_s

      self.class.new(self.class.convert(amount, unit, unit_name), unit_name)
    end
    alias_method("in_#{unit_name}", "to_#{unit_name}")
  end
end

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



335
336
337
338
339
340
341
342
343
# File 'app/services/shipping/package.rb', line 335

def self.add_methods_for(sym, options = {})
  add_conversion_method_for(sym, options)
  add_numeric_method = if options.key?(:add_numeric_methods)
                         options[:add_numeric_methods]
                       else
                         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)


357
358
359
360
361
362
363
364
365
366
367
# File 'app/services/shipping/package.rb', line 357

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 #{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)


255
256
257
# File 'app/services/shipping/package.rb', line 255

def self.add_numeric_methods?
  add_numeric_methods
end

.add_to_system(unit_sym) ⇒ Object



288
289
290
291
292
293
294
295
296
# File 'app/services/shipping/package.rb', line 288

def self.add_to_system(unit_sym)
  return unless current_system

  units_to_systems[unit_sym] ||= begin
    sys_ary = systems_to_units[current_system] ||= []
    sys_ary << unit_sym
    current_system
  end
end

.conversion_rate(from, to) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/services/shipping/package.rb', line 209

def self.conversion_rate(from, to)
  return nil unless conversions[from] && conversions[to]

  # rubocop:disable Lint/NoReturnInBeginEndBlocks
  conversions[from][to] ||=
    (1.0 / conversions[to][from] if conversions[to][from]) || begin
      shared_conversions = conversions[from].keys & conversions[to].keys
      if shared_conversions.any?
        primitive = shared_conversions.first
        conversions[from][primitive] * (1.0 / conversions[to][primitive])
      else
        conversions[from].each do |conversion_unit, multiple|
          return multiple * conversion_rate(conversion) * (1.0 / conversions[to][conversion_unit]) if conversions[to].include?(conversion_unit)
        end
        from_primitive = (conversions[from].keys & primitives).first
        to_primitive = (conversions[to].keys & primitives).first
        if (from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive))
          return conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / conversions[to][to_primitive])
        end

        raise StandardError, "No conversion path from #{from} to #{to}"
      end
    end
  # rubocop:enable Lint/NoReturnInBeginEndBlocks
end

.convert(amount, from, to) ⇒ Object



329
330
331
332
333
# File 'app/services/shipping/package.rb', line 329

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

.non_primitivesObject



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

def self.non_primitives
  conversions.keys
end

.numeric_methods(*args) ⇒ Object



259
260
261
262
263
# File 'app/services/shipping/package.rb', line 259

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

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



298
299
300
301
302
303
# File 'app/services/shipping/package.rb', line 298

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



281
282
283
284
285
286
# File 'app/services/shipping/package.rb', line 281

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

.primitivesObject



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

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

.register_unit(multiple_unit, other_unit, multiple) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/services/shipping/package.rb', line 305

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

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

.system(system_name) ⇒ Object



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

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

.systemsObject



251
252
253
# File 'app/services/shipping/package.rb', line 251

def self.systems
  systems_to_units.keys
end

.units(system = nil) ⇒ Object



235
236
237
238
239
240
241
# File 'app/services/shipping/package.rb', line 235

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

Instance Method Details

#<=>(other) ⇒ Object



188
189
190
191
192
193
194
# File 'app/services/shipping/package.rb', line 188

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

#==(other) ⇒ Object



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


184
185
186
# File 'app/services/shipping/package.rb', line 184

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

#inspectObject



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

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

#systemObject



196
197
198
# File 'app/services/shipping/package.rb', line 196

def system
  self.class.units_to_systems[unit]
end

#to_sObject



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

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