class Quantified::Attribute
Constants
- PLURAL_EXCEPTIONS
Attributes
add_numeric_methods[W]
current_system[RW]
amount[R]
unit[R]
Public Class Methods
add_numeric_methods?()
click to toggle source
# File lib/quantified/attribute.rb, line 95 def self.add_numeric_methods? add_numeric_methods end
coder(unit)
click to toggle source
# File lib/quantified/attribute.rb, line 105 def self.coder(unit) Coder.new(self, unit) end
conversion_rate(from, to)
click to toggle source
# File lib/quantified/attribute.rb, line 55 def self.conversion_rate(from, to) return nil unless conversions[from] and conversions[to] 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| if conversions[to].include?(conversion_unit) return multiple * conversion_rate(conversion) * (1.0 / conversions[to][conversion_unit]) end 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 end
new(amount, unit)
click to toggle source
# File lib/quantified/attribute.rb, line 7 def initialize(amount, unit) raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric) @amount, @unit = amount, unit.to_sym end
non_primitives()
click to toggle source
# File lib/quantified/attribute.rb, line 87 def self.non_primitives conversions.keys end
numeric_methods(*args)
click to toggle source
# File lib/quantified/attribute.rb, line 99 def self.numeric_methods(*args) args.each do |arg| add_numeric_method_for(arg.to_sym) end end
systems()
click to toggle source
# File lib/quantified/attribute.rb, line 91 def self.systems systems_to_units.keys end
units(system = nil)
click to toggle source
# File lib/quantified/attribute.rb, line 79 def self.units(system = nil) if system systems_to_units[system.to_sym].dup else primitives | conversions.keys end end
Protected Class Methods
add_coder_method_for(sym, options = {})
click to toggle source
# File lib/quantified/attribute.rb, line 205 def self.add_coder_method_for(sym, options = {}) (class << self; self; end).instance_eval do define_method(sym) do Coder.new(self, sym) end end end
add_conversion(multiple_unit, other_unit, multiple)
click to toggle source
# File lib/quantified/attribute.rb, line 171 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 = {})
click to toggle source
# File lib/quantified/attribute.rb, line 194 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 = {})
click to toggle source
# File lib/quantified/attribute.rb, line 183 def self.add_methods_for(sym, options = {}) add_conversion_method_for(sym, options) add_coder_method_for(sym, options) add_numeric_method = if options.has_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 = {})
click to toggle source
# File lib/quantified/attribute.rb, line 213 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()
click to toggle source
# File lib/quantified/attribute.rb, line 113 def add_numeric_methods; @add_numeric_methods ||= false; end
add_to_system(unit_sym)
click to toggle source
# File lib/quantified/attribute.rb, line 136 def self.add_to_system(unit_sym) if current_system units_to_systems[unit_sym] ||= begin sys_ary = systems_to_units[current_system] ||= [] sys_ary << unit_sym current_system end end end
conversions()
click to toggle source
# File lib/quantified/attribute.rb, line 115 def conversions; @conversions ||= {}; end
convert(amount, from, to)
click to toggle source
# File lib/quantified/attribute.rb, line 178 def self.convert(amount, from, to) from, to = from.to_sym, to.to_sym amount * conversion_rate(from, to) end
one(sym, options = {})
click to toggle source
# File lib/quantified/attribute.rb, line 146 def self.one(sym, options = {}) unit_sym = pluralize_unit(sym, options[:plural]).to_sym add_to_system(unit_sym) register_unit(unit_sym, options[:is].unit, options[:is].amount) add_methods_for(unit_sym, options) end
pluralize_unit(word, provided_plural = nil)
click to toggle source
# File lib/quantified/attribute.rb, line 230 def self.pluralize_unit(word, provided_plural = nil) return provided_plural unless provided_plural.nil? PLURAL_EXCEPTIONS[word.to_s] || "#{word}s" end
primitive(sym, options = {})
click to toggle source
# File lib/quantified/attribute.rb, line 129 def self.primitive(sym, options = {}) unit_sym = pluralize_unit(sym, options[:plural]).to_sym primitives << unit_sym add_to_system(unit_sym) add_methods_for(unit_sym, options) end
primitives()
click to toggle source
# File lib/quantified/attribute.rb, line 112 def primitives; @primitives ||= []; end
register_unit(multiple_unit, other_unit, multiple)
click to toggle source
# File lib/quantified/attribute.rb, line 153 def self.register_unit(multiple_unit, other_unit, multiple) multiple_unit, other_unit = multiple_unit.to_sym, 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| if primitives.include?(this_other_unit) add_conversion(multiple_unit, this_other_unit, multiple * this_multiple) end end end end end
system(system_name) { || ... }
click to toggle source
# File lib/quantified/attribute.rb, line 122 def self.system(system_name, &block) old_system = current_system self.current_system = system_name.to_sym yield self.current_system = old_system end
systems_to_units()
click to toggle source
# File lib/quantified/attribute.rb, line 118 def systems_to_units; @systems_to_units ||= {}; end
units_to_systems()
click to toggle source
# File lib/quantified/attribute.rb, line 119 def units_to_systems; @units_to_systems ||= {}; end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/quantified/attribute.rb, line 30 def <=>(other) if self.class == other.class self.class.convert(amount, unit, other.unit) <=> other.amount else amount <=> other end end
==(other)
click to toggle source
# File lib/quantified/attribute.rb, line 20 def ==(other) (BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit) || BigDecimal.new(self.class.convert(amount, unit, other.unit).to_s) == BigDecimal.new(other.amount.to_s) rescue NoMethodError amount == other end
coerce(other)
click to toggle source
# File lib/quantified/attribute.rb, line 42 def coerce(other) [other, amount] end
eql?(other)
click to toggle source
# File lib/quantified/attribute.rb, line 26 def eql?(other) self.class == other.class && BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit end
inspect()
click to toggle source
# File lib/quantified/attribute.rb, line 16 def inspect "#<#{self.class.name}: #{amount} #{unit}>" end
method_missing(meth, *args)
click to toggle source
# File lib/quantified/attribute.rb, line 46 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, unit) self.class.new(amount.send(meth, other_amount_in_self_units), unit) else amount.send(meth, *args) end end
system()
click to toggle source
# File lib/quantified/attribute.rb, line 38 def system self.class.units_to_systems[unit] end
to_s()
click to toggle source
# File lib/quantified/attribute.rb, line 12 def to_s "#{amount} #{unit}" end