Sha256: 532817753afc4f79b73ea2df7a4f1f052f38b889b0e8a903f62b3e58f334185d

Contents?: true

Size: 1.19 KB

Versions: 14

Compression:

Stored size: 1.19 KB

Contents

module Barometer
  class Data::Units
    include Comparable
    
    attr_accessor :metric
    
    def initialize(metric=true); @metric = metric; end
    
    #
    # HELPERS
    #
    
    def metric?; @metric; end
    def metric!; @metric=true; end
    def imperial!; @metric=false; end

    # assigns a value to the right attribute based on metric setting
    #
    def <<(value)
      return unless value
      
      # these values can be treated like 'nil'
      nil_values = ["NA", "N/A", ""]
    
      begin
        if value.is_a?(Array)
          value_m = value[0].to_f if (value[0] && !nil_values.include?(value[0]))
          value_i = value[1].to_f if (value[1] && !nil_values.include?(value[1]))
          value_b = nil
        else
          value_m = nil
          value_i = nil
          value_b = value.to_f if (value && !nil_values.include?(value))
        end
      rescue
        # do nothing
      end
    
      self.metric? ? self.metric_default = value_m || value_b :
        self.imperial_default = value_i || value_b
    end
    
    # stubs
    #
    def metric_default=(value); raise NotImplementedError; end
    def imperial_default=(value); raise NotImplementedError; end

  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
attack-barometer-0.5.0 lib/barometer/data/units.rb
attack-barometer-0.6.0 lib/barometer/data/units.rb
attack-barometer-0.6.1 lib/barometer/data/units.rb
barometer-0.7.3 lib/barometer/data/units.rb
barometer-0.7.2 lib/barometer/data/units.rb
barometer-0.7.1 lib/barometer/data/units.rb
barometer-0.7.0 lib/barometer/data/units.rb
barometer-0.6.7 lib/barometer/data/units.rb
barometer-0.6.6 lib/barometer/data/units.rb
barometer-0.6.4 lib/barometer/data/units.rb
barometer-0.6.1 lib/barometer/data/units.rb
barometer-0.5.0 lib/barometer/data/units.rb
barometer-0.6.3 lib/barometer/data/units.rb
barometer-0.6.2 lib/barometer/data/units.rb