quantified 1.0.0
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 +7 -0
- data/.gitignore +7 -0
- data/.travis.yml +8 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +11 -0
- data/lib/quantified.rb +6 -0
- data/lib/quantified/attribute.rb +222 -0
- data/lib/quantified/length.rb +20 -0
- data/lib/quantified/mass.rb +19 -0
- data/lib/quantified/version.rb +3 -0
- data/quantified.gemspec +24 -0
- data/test/length_test.rb +94 -0
- data/test/mass_test.rb +96 -0
- data/test/test_helper.rb +12 -0
- metadata +91 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: b82fde76b902f73feeafc005768ef407964241c1
         | 
| 4 | 
            +
              data.tar.gz: b36878a61fde1cf01bb5b982295d15a66fa1317c
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: eb74b18116d50c2cff3a550c47cd93569d901e96f6f265e73d6a27774401e9591dad3893de93e2beac5f3e45128b00fe73a57365b02973600f8a41d152fa5685
         | 
| 7 | 
            +
              data.tar.gz: 6b0391b2b1a160eefccacbdb7cfea0c210fb56fa2a4ca6cd549882c8c08f0052f0b1adb3aa806a156bc7c7a8a69159bc2844c9f876e57c1c813df4356d8fc93d
         | 
    
        data/.gitignore
    ADDED
    
    
    
        data/.travis.yml
    ADDED
    
    
    
        data/Gemfile
    ADDED
    
    
    
        data/MIT-LICENSE
    ADDED
    
    | @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            Copyright (c) 2009 James MacAulay
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Permission is hereby granted, free of charge, to any person
         | 
| 4 | 
            +
            obtaining a copy of this software and associated documentation
         | 
| 5 | 
            +
            files (the "Software"), to deal in the Software without
         | 
| 6 | 
            +
            restriction, including without limitation the rights to use,
         | 
| 7 | 
            +
            copy, modify, merge, publish, distribute, sublicense, and/or sell
         | 
| 8 | 
            +
            copies of the Software, and to permit persons to whom the
         | 
| 9 | 
            +
            Software is furnished to do so, subject to the following
         | 
| 10 | 
            +
            conditions:
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            The above copyright notice and this permission notice shall be
         | 
| 13 | 
            +
            included in all copies or substantial portions of the Software.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
         | 
| 16 | 
            +
            EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
         | 
| 17 | 
            +
            OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
         | 
| 18 | 
            +
            NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
         | 
| 19 | 
            +
            HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
         | 
| 20 | 
            +
            WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
         | 
| 21 | 
            +
            FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
         | 
| 22 | 
            +
            OTHER DEALINGS IN THE SOFTWARE.
         | 
    
        data/README.md
    ADDED
    
    | @@ -0,0 +1,48 @@ | |
| 1 | 
            +
            # Quantified [](https://travis-ci.org/Shopify/quantified)
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            Pretty quantifiable measurements which feel like ActiveSupport::Duration.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            Access whichever included attributes you want like so:
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                require 'quantified/mass'
         | 
| 8 | 
            +
                require 'quantified/length'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            Add methods to Numeric (only plural, must correspond with plural unit names):
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                Mass.numeric_methods :grams, :kilograms, :ounces, :pounds
         | 
| 13 | 
            +
                Length.numeric_methods :metres, :centimetres, :inches, :feet
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Then you can do things like this:
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                1.feet == 12.inches
         | 
| 18 | 
            +
                # => true
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                18.inches.to_feet
         | 
| 21 | 
            +
                # => #<Quantified::Length: 1.5 feet>
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                (2.5).feet.in_millimetres.to_s
         | 
| 24 | 
            +
                # => "762.0 millimetres"
         | 
| 25 | 
            +
             | 
| 26 | 
            +
             | 
| 27 | 
            +
            You can easily define new attributes. Here's length.rb:
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                module Quantified
         | 
| 30 | 
            +
                  class Length < Attribute
         | 
| 31 | 
            +
                    system :metric do
         | 
| 32 | 
            +
                      primitive :metre
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                      one :centimetre, :is => Length.new(0.01, :metres)
         | 
| 35 | 
            +
                      one :millimetre, :is => Length.new(0.1, :centimetres)
         | 
| 36 | 
            +
                      one :kilometre, :is => Length.new(1000, :metres)
         | 
| 37 | 
            +
                    end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    system :imperial do
         | 
| 40 | 
            +
                      primitive :inch
         | 
| 41 | 
            +
                      one :inch, :is => Length.new(2.540, :centimetres)
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                      one :foot, :plural => :feet, :is => Length.new(12, :inches)
         | 
| 44 | 
            +
                      one :yard, :is => Length.new(3, :feet)
         | 
| 45 | 
            +
                      one :mile, :is => Length.new(5280, :feet)
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
                end
         | 
    
        data/Rakefile
    ADDED
    
    
    
        data/lib/quantified.rb
    ADDED
    
    
| @@ -0,0 +1,222 @@ | |
| 1 | 
            +
            module Quantified
         | 
| 2 | 
            +
              class Attribute
         | 
| 3 | 
            +
                include Comparable
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                attr_reader :amount, :unit
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                def initialize(amount, unit)
         | 
| 8 | 
            +
                  raise ArgumentError, "amount must be a Numeric" unless amount.is_a?(Numeric)
         | 
| 9 | 
            +
                  @amount, @unit = amount, unit.to_sym
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                def to_s
         | 
| 13 | 
            +
                  "#{amount} #{unit}"
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def inspect
         | 
| 17 | 
            +
                  "#<#{self.class.name}: #{amount} #{unit}>"
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def ==(other)
         | 
| 21 | 
            +
                  (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)
         | 
| 22 | 
            +
                rescue NoMethodError
         | 
| 23 | 
            +
                  amount == other
         | 
| 24 | 
            +
                end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                def eql?(other)
         | 
| 27 | 
            +
                  self.class == other.class && BigDecimal.new(amount.to_s) == BigDecimal.new(other.amount.to_s) && unit == other.unit
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                def <=>(other)
         | 
| 31 | 
            +
                  if self.class == other.class
         | 
| 32 | 
            +
                    self.class.convert(amount, unit, other.unit) <=> other.amount
         | 
| 33 | 
            +
                  else
         | 
| 34 | 
            +
                    amount <=> other
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                def system
         | 
| 39 | 
            +
                  self.class.units_to_systems[unit]
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                def coerce(other)
         | 
| 43 | 
            +
                  [other, amount]
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                def method_missing(meth, *args)
         | 
| 47 | 
            +
                  if args.size == 1 && self.class == (other = args.first).class
         | 
| 48 | 
            +
                    other_amount_in_self_units = self.class.convert(other.amount, other.unit, unit)
         | 
| 49 | 
            +
                    self.class.new(amount.send(meth, other_amount_in_self_units), unit)
         | 
| 50 | 
            +
                  else
         | 
| 51 | 
            +
                    amount.send(meth, *args)
         | 
| 52 | 
            +
                  end
         | 
| 53 | 
            +
                end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                def self.conversion_rate(from, to)
         | 
| 56 | 
            +
                  return nil unless conversions[from] and conversions[to]
         | 
| 57 | 
            +
                  conversions[from][to] ||=
         | 
| 58 | 
            +
                  (1.0 / conversions[to][from] if conversions[to][from]) || begin
         | 
| 59 | 
            +
                    shared_conversions = conversions[from].keys & conversions[to].keys
         | 
| 60 | 
            +
                    if shared_conversions.any?
         | 
| 61 | 
            +
                      primitive = shared_conversions.first
         | 
| 62 | 
            +
                      conversions[from][primitive] * (1.0 / conversions[to][primitive])
         | 
| 63 | 
            +
                    else
         | 
| 64 | 
            +
                      conversions[from].each do |conversion_unit, multiple|
         | 
| 65 | 
            +
                        if conversions[to].include?(conversion_unit)
         | 
| 66 | 
            +
                          return multiple * conversion_rate(conversion) * (1.0 / conversions[to][conversion_unit])
         | 
| 67 | 
            +
                        end
         | 
| 68 | 
            +
                      end
         | 
| 69 | 
            +
                      from_primitive = (conversions[from].keys & primitives).first
         | 
| 70 | 
            +
                      to_primitive = (conversions[to].keys & primitives).first
         | 
| 71 | 
            +
                      if from_primitive_to_primitive_multiple = conversion_rate(from_primitive, to_primitive)
         | 
| 72 | 
            +
                        return conversions[from][from_primitive] * from_primitive_to_primitive_multiple * (1.0 / conversions[to][to_primitive])
         | 
| 73 | 
            +
                      end
         | 
| 74 | 
            +
                      raise StandardError, "No conversion path from #{from} to #{to}"
         | 
| 75 | 
            +
                    end
         | 
| 76 | 
            +
                  end
         | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                def self.units(system = nil)
         | 
| 80 | 
            +
                  if system
         | 
| 81 | 
            +
                    systems_to_units[system.to_sym].dup
         | 
| 82 | 
            +
                  else
         | 
| 83 | 
            +
                    primitives | conversions.keys
         | 
| 84 | 
            +
                  end
         | 
| 85 | 
            +
                end
         | 
| 86 | 
            +
             | 
| 87 | 
            +
                def self.non_primitives
         | 
| 88 | 
            +
                  conversions.keys
         | 
| 89 | 
            +
                end
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                def self.systems
         | 
| 92 | 
            +
                  systems_to_units.keys
         | 
| 93 | 
            +
                end
         | 
| 94 | 
            +
             | 
| 95 | 
            +
                def self.add_numeric_methods?
         | 
| 96 | 
            +
                  add_numeric_methods
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                def self.numeric_methods(*args)
         | 
| 100 | 
            +
                  args.each do |arg|
         | 
| 101 | 
            +
                    add_numeric_method_for(arg.to_sym)
         | 
| 102 | 
            +
                  end
         | 
| 103 | 
            +
                end
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                protected
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                class << self
         | 
| 108 | 
            +
                  def primitives;              @primitives ||= [];             end
         | 
| 109 | 
            +
                  def add_numeric_methods;     @add_numeric_methods ||= false; end
         | 
| 110 | 
            +
                  attr_writer :add_numeric_methods
         | 
| 111 | 
            +
                  def conversions;             @conversions ||= {};            end
         | 
| 112 | 
            +
                  attr_reader :current_system
         | 
| 113 | 
            +
                  attr_writer :current_system
         | 
| 114 | 
            +
                  def systems_to_units;        @systems_to_units ||= {};       end
         | 
| 115 | 
            +
                  def units_to_systems;        @units_to_systems ||= {};       end
         | 
| 116 | 
            +
                end
         | 
| 117 | 
            +
             | 
| 118 | 
            +
                def self.system(system_name, &block)
         | 
| 119 | 
            +
                  old_system = current_system
         | 
| 120 | 
            +
                  self.current_system = system_name.to_sym
         | 
| 121 | 
            +
                  yield
         | 
| 122 | 
            +
                  self.current_system = old_system
         | 
| 123 | 
            +
                end
         | 
| 124 | 
            +
             | 
| 125 | 
            +
                def self.primitive(sym, options = {})
         | 
| 126 | 
            +
                  unit_sym = pluralize_unit(sym, options[:plural]).to_sym
         | 
| 127 | 
            +
                  primitives << unit_sym
         | 
| 128 | 
            +
                  add_to_system(unit_sym)
         | 
| 129 | 
            +
                  add_methods_for(unit_sym, options)
         | 
| 130 | 
            +
                end
         | 
| 131 | 
            +
             | 
| 132 | 
            +
                def self.add_to_system(unit_sym)
         | 
| 133 | 
            +
                  if current_system
         | 
| 134 | 
            +
                    units_to_systems[unit_sym] ||= begin
         | 
| 135 | 
            +
                      sys_ary = systems_to_units[current_system] ||= []
         | 
| 136 | 
            +
                      sys_ary << unit_sym
         | 
| 137 | 
            +
                      current_system
         | 
| 138 | 
            +
                    end
         | 
| 139 | 
            +
                  end
         | 
| 140 | 
            +
                end
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                def self.one(sym, options = {})
         | 
| 143 | 
            +
                  unit_sym = pluralize_unit(sym, options[:plural]).to_sym
         | 
| 144 | 
            +
                  add_to_system(unit_sym)
         | 
| 145 | 
            +
                  register_unit(unit_sym, options[:is].unit, options[:is].amount)
         | 
| 146 | 
            +
                  add_methods_for(unit_sym, options)
         | 
| 147 | 
            +
                end
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                def self.register_unit(multiple_unit, other_unit, multiple)
         | 
| 150 | 
            +
                  multiple_unit, other_unit = multiple_unit.to_sym, other_unit.to_sym
         | 
| 151 | 
            +
                  conversions[multiple_unit] ||= {}
         | 
| 152 | 
            +
                  conversions[other_unit] ||= {}
         | 
| 153 | 
            +
             | 
| 154 | 
            +
                  if primitives.include?(multiple_unit) || primitives.include?(other_unit)
         | 
| 155 | 
            +
                    add_conversion(multiple_unit, other_unit, multiple)
         | 
| 156 | 
            +
                  else
         | 
| 157 | 
            +
                    [multiple_unit, other_unit].each do |this_unit|
         | 
| 158 | 
            +
                      conversions[this_unit].each do |this_other_unit, this_multiple|
         | 
| 159 | 
            +
                        if primitives.include?(this_other_unit)
         | 
| 160 | 
            +
                          add_conversion(multiple_unit, this_other_unit, multiple * this_multiple)
         | 
| 161 | 
            +
                        end
         | 
| 162 | 
            +
                      end
         | 
| 163 | 
            +
                    end
         | 
| 164 | 
            +
                  end
         | 
| 165 | 
            +
                end
         | 
| 166 | 
            +
             | 
| 167 | 
            +
                def self.add_conversion(multiple_unit, other_unit, multiple)
         | 
| 168 | 
            +
                  conversions[multiple_unit] ||= {}
         | 
| 169 | 
            +
                  conversions[multiple_unit][other_unit] = multiple
         | 
| 170 | 
            +
                  conversions[other_unit] ||= {}
         | 
| 171 | 
            +
                  conversions[other_unit][multiple_unit] = (1.0 / multiple)
         | 
| 172 | 
            +
                end
         | 
| 173 | 
            +
             | 
| 174 | 
            +
                def self.convert(amount, from, to)
         | 
| 175 | 
            +
                  from, to = from.to_sym, to.to_sym
         | 
| 176 | 
            +
                  amount * conversion_rate(from, to)
         | 
| 177 | 
            +
                end
         | 
| 178 | 
            +
             | 
| 179 | 
            +
                def self.add_methods_for(sym, options = {})
         | 
| 180 | 
            +
                  add_conversion_method_for(sym, options)
         | 
| 181 | 
            +
                  add_numeric_method = if options.has_key?(:add_numeric_methods)
         | 
| 182 | 
            +
                    options[:add_numeric_methods]
         | 
| 183 | 
            +
                  else
         | 
| 184 | 
            +
                    add_numeric_methods
         | 
| 185 | 
            +
                  end
         | 
| 186 | 
            +
                  add_numeric_method_for(sym.to_s, options) if add_numeric_method
         | 
| 187 | 
            +
                end
         | 
| 188 | 
            +
             | 
| 189 | 
            +
                def self.add_conversion_method_for(sym, options = {})
         | 
| 190 | 
            +
                  unit_name = sym.to_s
         | 
| 191 | 
            +
                  class_eval do
         | 
| 192 | 
            +
                    define_method("to_#{unit_name}") do
         | 
| 193 | 
            +
                      return self if unit_name == unit.to_s
         | 
| 194 | 
            +
                      self.class.new(self.class.convert(amount, unit, unit_name), unit_name)
         | 
| 195 | 
            +
                    end
         | 
| 196 | 
            +
                    alias_method("in_#{unit_name}", "to_#{unit_name}")
         | 
| 197 | 
            +
                  end
         | 
| 198 | 
            +
                end
         | 
| 199 | 
            +
             | 
| 200 | 
            +
                def self.add_numeric_method_for(unit_name, options = {})
         | 
| 201 | 
            +
                  unit_name = unit_name.to_sym
         | 
| 202 | 
            +
                  raise ArgumentError, "#{unit_name.inspect} is not a unit in #{name}" unless units.include?(unit_name)
         | 
| 203 | 
            +
                  klass = self
         | 
| 204 | 
            +
                  Numeric.class_eval do
         | 
| 205 | 
            +
                    define_method(unit_name) do
         | 
| 206 | 
            +
                      klass.new(self, unit_name.to_sym)
         | 
| 207 | 
            +
                    end
         | 
| 208 | 
            +
                  end
         | 
| 209 | 
            +
                end
         | 
| 210 | 
            +
             | 
| 211 | 
            +
                PLURAL_EXCEPTIONS = {
         | 
| 212 | 
            +
                  'inch' => 'inches',
         | 
| 213 | 
            +
                  'foot' => 'feet',
         | 
| 214 | 
            +
                }
         | 
| 215 | 
            +
                private_constant :PLURAL_EXCEPTIONS
         | 
| 216 | 
            +
             | 
| 217 | 
            +
                def self.pluralize_unit(word, provided_plural = nil)
         | 
| 218 | 
            +
                  return provided_plural unless provided_plural.nil?
         | 
| 219 | 
            +
                  PLURAL_EXCEPTIONS[word.to_s] || "#{word}s"
         | 
| 220 | 
            +
                end
         | 
| 221 | 
            +
              end
         | 
| 222 | 
            +
            end
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            module Quantified
         | 
| 2 | 
            +
              class Length < Attribute
         | 
| 3 | 
            +
                system :metric do
         | 
| 4 | 
            +
                  primitive :metre
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  one :centimetre, :is => Length.new(0.01, :metres)
         | 
| 7 | 
            +
                  one :millimetre, :is => Length.new(0.1, :centimetres)
         | 
| 8 | 
            +
                  one :kilometre, :is => Length.new(1000, :metres)
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                system :imperial do
         | 
| 12 | 
            +
                  primitive :inch
         | 
| 13 | 
            +
                  one :inch, :is => Length.new(0.0254, :metres)
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  one :foot, :plural => :feet, :is => Length.new(12, :inches)
         | 
| 16 | 
            +
                  one :yard, :is => Length.new(3, :feet)
         | 
| 17 | 
            +
                  one :mile, :is => Length.new(5280, :feet)
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            module Quantified
         | 
| 2 | 
            +
              class Mass < Attribute
         | 
| 3 | 
            +
                system :metric do
         | 
| 4 | 
            +
                  primitive :gram
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  one :milligram, :is => Mass.new(0.001, :grams)
         | 
| 7 | 
            +
                  one :kilogram, :is => Mass.new(1000, :grams)
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                system :imperial do
         | 
| 11 | 
            +
                  primitive :ounce
         | 
| 12 | 
            +
                  one :ounce, :is => Mass.new(28.349523125, :grams)
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  one :pound, :is => Mass.new(16, :ounces)
         | 
| 15 | 
            +
                  one :stone, :is => Mass.new(14, :pounds)
         | 
| 16 | 
            +
                  one :short_ton, :is => Mass.new(2000, :pounds)
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
    
        data/quantified.gemspec
    ADDED
    
    | @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            lib = File.expand_path('../lib/', __FILE__)
         | 
| 2 | 
            +
            $:.unshift lib unless $:.include?(lib)
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require 'quantified/version'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Gem::Specification.new do |s|
         | 
| 7 | 
            +
              s.name        = "quantified"
         | 
| 8 | 
            +
              s.version     = Quantified::VERSION
         | 
| 9 | 
            +
              s.license     = 'MIT'
         | 
| 10 | 
            +
              s.platform    = Gem::Platform::RUBY
         | 
| 11 | 
            +
              s.authors     = ["James MacAulay", "Willem van Bergen"]
         | 
| 12 | 
            +
              s.email       = ["james@shopify.com"]
         | 
| 13 | 
            +
              s.homepage    = "https://github.com/Shopify/quantified"
         | 
| 14 | 
            +
              s.summary     = "Pretty quantifiable measurements which feel like ActiveSupport::Duration."
         | 
| 15 | 
            +
              s.description = s.summary
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              s.add_development_dependency('rake')
         | 
| 18 | 
            +
              s.add_development_dependency('test-unit')
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              s.files        = `git ls-files`.split($/)
         | 
| 21 | 
            +
              s.executables  = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
         | 
| 22 | 
            +
              s.test_files   = s.files.grep(%r{^(test|spec|features)/})
         | 
| 23 | 
            +
              s.require_path = ['lib']
         | 
| 24 | 
            +
            end
         | 
    
        data/test/length_test.rb
    ADDED
    
    | @@ -0,0 +1,94 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
            require 'quantified/length'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class LengthTest < Test::Unit::TestCase
         | 
| 5 | 
            +
              include Quantified
         | 
| 6 | 
            +
              Length.numeric_methods :metres, :centimetres, :inches, :feet
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def setup
         | 
| 9 | 
            +
                @length = Length.new(5, :feet)
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def test_inspect
         | 
| 13 | 
            +
                assert_equal "#<Quantified::Length: 5 feet>", @length.inspect
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              def test_to_s
         | 
| 17 | 
            +
                assert_equal "5 feet", @length.to_s
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              def test_initialize_from_numeric
         | 
| 21 | 
            +
                assert_equal "5 feet", 5.feet.to_s
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              def test_equalities
         | 
| 25 | 
            +
                assert_equal 1.feet, (1.0).feet
         | 
| 26 | 
            +
                # == based on value
         | 
| 27 | 
            +
                assert_equal 6.feet, Length.new(2, :yards)
         | 
| 28 | 
            +
                # eql? based on value and unit
         | 
| 29 | 
            +
                assert !6.feet.eql?(Length.new(2, :yards))
         | 
| 30 | 
            +
                # equal? based on object identity
         | 
| 31 | 
            +
                assert !2.feet.equal?(2.feet)
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def test_convert_mm_to_inches
         | 
| 35 | 
            +
                assert_equal 12, Length.new(304.8, :millimetres).to_inches
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def test_convert_yards_to_feet
         | 
| 39 | 
            +
                assert 6.feet.eql?(Length.new(2, :yards).to_feet)
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def test_convert_feet_to_yards
         | 
| 43 | 
            +
                assert Length.new(2, :yards).eql?(6.feet.to_yards)
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def test_convert_yards_to_millimetres
         | 
| 47 | 
            +
                assert_in_epsilon Length.new(914.4, :millimetres).to_f, Length.new(1, :yards).to_millimetres.to_f
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def test_convert_millimetres_to_yards
         | 
| 51 | 
            +
                assert_in_epsilon Length.new(1, :yards).to_f, Length.new(914.4, :millimetres).to_yards.to_f
         | 
| 52 | 
            +
              end
         | 
| 53 | 
            +
             | 
| 54 | 
            +
              def test_convert_metres_to_inches
         | 
| 55 | 
            +
                assert_in_epsilon 1.inches.to_f, (0.0254).metres.to_inches.to_f
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
              def test_comparison_with_numeric
         | 
| 59 | 
            +
                assert 2.feet > 1
         | 
| 60 | 
            +
                assert 2.feet == 2
         | 
| 61 | 
            +
                assert 2.feet <= 2
         | 
| 62 | 
            +
                assert 2.feet < 3
         | 
| 63 | 
            +
              end
         | 
| 64 | 
            +
             | 
| 65 | 
            +
              def test_method_missing_to_i
         | 
| 66 | 
            +
                assert_equal 2, (2.4).feet.to_i
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              def test_method_missing_to_f
         | 
| 70 | 
            +
                assert_equal 2.4, (2.4).feet.to_f
         | 
| 71 | 
            +
              end
         | 
| 72 | 
            +
             | 
| 73 | 
            +
              def test_method_missing_minus
         | 
| 74 | 
            +
                assert_equal 2.feet, 5.feet - 3.feet
         | 
| 75 | 
            +
              end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
              def test_numeric_methods_not_added_for_some_units
         | 
| 78 | 
            +
                assert_raises(NoMethodError) do
         | 
| 79 | 
            +
                  2.yards
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
                assert_raises(NoMethodError) do
         | 
| 82 | 
            +
                  2.millimetres
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
              end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
              def test_systems
         | 
| 87 | 
            +
                assert_equal [:metric, :imperial], Length.systems
         | 
| 88 | 
            +
                assert_equal [:metres, :centimetres, :millimetres, :kilometres], Length.units(:metric)
         | 
| 89 | 
            +
                assert_equal [:inches, :feet, :yards, :miles], Length.units(:imperial)
         | 
| 90 | 
            +
             | 
| 91 | 
            +
                assert_equal :metric, 2.centimetres.system
         | 
| 92 | 
            +
                assert_equal :imperial, 2.feet.system
         | 
| 93 | 
            +
              end
         | 
| 94 | 
            +
            end
         | 
    
        data/test/mass_test.rb
    ADDED
    
    | @@ -0,0 +1,96 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
            require 'quantified/mass'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class MassTest < Test::Unit::TestCase
         | 
| 5 | 
            +
              include Quantified
         | 
| 6 | 
            +
              Mass.numeric_methods :grams, :kilograms, :ounces, :pounds
         | 
| 7 | 
            +
             | 
| 8 | 
            +
              def setup
         | 
| 9 | 
            +
                @mass = Mass.new(5, :pounds)
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def test_inspect
         | 
| 13 | 
            +
                assert_equal "#<Quantified::Mass: 5 pounds>", @mass.inspect
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              def test_to_s
         | 
| 17 | 
            +
                assert_equal "5 pounds", @mass.to_s
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              def test_initialize_from_numeric
         | 
| 21 | 
            +
                assert_equal "5 pounds", 5.pounds.to_s
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              def test_equalities
         | 
| 25 | 
            +
                assert_equal 1.pounds, (1.0).pounds
         | 
| 26 | 
            +
                # == based on value
         | 
| 27 | 
            +
                assert_equal 4000.pounds, Mass.new(2, :short_tons)
         | 
| 28 | 
            +
                # eql? based on value and unit
         | 
| 29 | 
            +
                assert !4000.pounds.eql?(Mass.new(2, :short_tons))
         | 
| 30 | 
            +
                # equal? based on object identity
         | 
| 31 | 
            +
                assert !2.pounds.equal?(2.pounds)
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def test_convert_short_tons_to_pounds
         | 
| 35 | 
            +
                assert 4000.pounds.eql?(Mass.new(2, :short_tons).to_pounds)
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def test_convert_pounds_to_short_tons
         | 
| 39 | 
            +
                assert Mass.new(2, :short_tons).eql?(4000.pounds.to_short_tons)
         | 
| 40 | 
            +
              end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
              def test_convert_short_tons_to_milligrams
         | 
| 43 | 
            +
                assert Mass.new(907_184_740, :milligrams).eql?(Mass.new(1, :short_tons).to_milligrams)
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
              def test_convert_milligrams_to_short_tons
         | 
| 47 | 
            +
                assert Mass.new(1, :short_tons).eql?(Mass.new(907_184_740, :milligrams).to_short_tons)
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
             | 
| 50 | 
            +
              def test_convert_grams_to_ounces
         | 
| 51 | 
            +
                assert 1.ounces.eql?((28.349523125).grams.to_ounces)
         | 
| 52 | 
            +
                assert 1.ounces.eql?((28.349523125).grams.in_ounces)
         | 
| 53 | 
            +
              end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
              def test_comparison_with_numeric
         | 
| 56 | 
            +
                assert 2.pounds > 1
         | 
| 57 | 
            +
                assert 2.pounds == 2
         | 
| 58 | 
            +
                assert 2.pounds <= 2
         | 
| 59 | 
            +
                assert 2.pounds < 3
         | 
| 60 | 
            +
              end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
              def test_method_missing_to_i
         | 
| 63 | 
            +
                assert_equal 2, (2.4).pounds.to_i
         | 
| 64 | 
            +
              end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
              def test_method_missing_to_f
         | 
| 67 | 
            +
                assert_equal 2.4, (2.4).pounds.to_f
         | 
| 68 | 
            +
              end
         | 
| 69 | 
            +
             | 
| 70 | 
            +
              def test_method_missing_minus
         | 
| 71 | 
            +
                assert_equal 2.pounds, 5.pounds - 3.pounds
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              def test_numeric_methods_not_added_for_some_units
         | 
| 75 | 
            +
                assert_raises NoMethodError do
         | 
| 76 | 
            +
                  2.short_tons
         | 
| 77 | 
            +
                end
         | 
| 78 | 
            +
                assert_raises NoMethodError do
         | 
| 79 | 
            +
                  2.milligrams
         | 
| 80 | 
            +
                end
         | 
| 81 | 
            +
              end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
              def test_systems
         | 
| 84 | 
            +
                assert_equal [:metric, :imperial], Mass.systems
         | 
| 85 | 
            +
                assert_equal [:grams, :milligrams, :kilograms], Mass.units(:metric)
         | 
| 86 | 
            +
                assert_equal [:ounces, :pounds, :stones, :short_tons], Mass.units(:imperial)
         | 
| 87 | 
            +
              end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
              def test_right_side_comparison_with_fixnum
         | 
| 90 | 
            +
                assert Mass.new(14, :grams) < 20
         | 
| 91 | 
            +
              end
         | 
| 92 | 
            +
             | 
| 93 | 
            +
              def test_left_side_comparison_with_fixnum
         | 
| 94 | 
            +
                assert 20 > Mass.new(14, :grams)
         | 
| 95 | 
            +
              end
         | 
| 96 | 
            +
            end
         | 
    
        data/test/test_helper.rb
    ADDED
    
    
    
        metadata
    ADDED
    
    | @@ -0,0 +1,91 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: quantified
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - James MacAulay
         | 
| 8 | 
            +
            - Willem van Bergen
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2015-01-26 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: rake
         | 
| 16 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                requirements:
         | 
| 18 | 
            +
                - - '>='
         | 
| 19 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 20 | 
            +
                    version: '0'
         | 
| 21 | 
            +
              type: :development
         | 
| 22 | 
            +
              prerelease: false
         | 
| 23 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 24 | 
            +
                requirements:
         | 
| 25 | 
            +
                - - '>='
         | 
| 26 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 27 | 
            +
                    version: '0'
         | 
| 28 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 29 | 
            +
              name: test-unit
         | 
| 30 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 31 | 
            +
                requirements:
         | 
| 32 | 
            +
                - - '>='
         | 
| 33 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 34 | 
            +
                    version: '0'
         | 
| 35 | 
            +
              type: :development
         | 
| 36 | 
            +
              prerelease: false
         | 
| 37 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 38 | 
            +
                requirements:
         | 
| 39 | 
            +
                - - '>='
         | 
| 40 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 41 | 
            +
                    version: '0'
         | 
| 42 | 
            +
            description: Pretty quantifiable measurements which feel like ActiveSupport::Duration.
         | 
| 43 | 
            +
            email:
         | 
| 44 | 
            +
            - james@shopify.com
         | 
| 45 | 
            +
            executables: []
         | 
| 46 | 
            +
            extensions: []
         | 
| 47 | 
            +
            extra_rdoc_files: []
         | 
| 48 | 
            +
            files:
         | 
| 49 | 
            +
            - .gitignore
         | 
| 50 | 
            +
            - .travis.yml
         | 
| 51 | 
            +
            - Gemfile
         | 
| 52 | 
            +
            - MIT-LICENSE
         | 
| 53 | 
            +
            - README.md
         | 
| 54 | 
            +
            - Rakefile
         | 
| 55 | 
            +
            - lib/quantified.rb
         | 
| 56 | 
            +
            - lib/quantified/attribute.rb
         | 
| 57 | 
            +
            - lib/quantified/length.rb
         | 
| 58 | 
            +
            - lib/quantified/mass.rb
         | 
| 59 | 
            +
            - lib/quantified/version.rb
         | 
| 60 | 
            +
            - quantified.gemspec
         | 
| 61 | 
            +
            - test/length_test.rb
         | 
| 62 | 
            +
            - test/mass_test.rb
         | 
| 63 | 
            +
            - test/test_helper.rb
         | 
| 64 | 
            +
            homepage: https://github.com/Shopify/quantified
         | 
| 65 | 
            +
            licenses:
         | 
| 66 | 
            +
            - MIT
         | 
| 67 | 
            +
            metadata: {}
         | 
| 68 | 
            +
            post_install_message: 
         | 
| 69 | 
            +
            rdoc_options: []
         | 
| 70 | 
            +
            require_paths:
         | 
| 71 | 
            +
            - - lib
         | 
| 72 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 73 | 
            +
              requirements:
         | 
| 74 | 
            +
              - - '>='
         | 
| 75 | 
            +
                - !ruby/object:Gem::Version
         | 
| 76 | 
            +
                  version: '0'
         | 
| 77 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 78 | 
            +
              requirements:
         | 
| 79 | 
            +
              - - '>='
         | 
| 80 | 
            +
                - !ruby/object:Gem::Version
         | 
| 81 | 
            +
                  version: '0'
         | 
| 82 | 
            +
            requirements: []
         | 
| 83 | 
            +
            rubyforge_project: 
         | 
| 84 | 
            +
            rubygems_version: 2.0.14
         | 
| 85 | 
            +
            signing_key: 
         | 
| 86 | 
            +
            specification_version: 4
         | 
| 87 | 
            +
            summary: Pretty quantifiable measurements which feel like ActiveSupport::Duration.
         | 
| 88 | 
            +
            test_files:
         | 
| 89 | 
            +
            - test/length_test.rb
         | 
| 90 | 
            +
            - test/mass_test.rb
         | 
| 91 | 
            +
            - test/test_helper.rb
         |