Sha256: b6795d72bef31c3408c73e7067ef434b3447d12231099dfa941f5cd47a1be886

Contents?: true

Size: 990 Bytes

Versions: 1

Compression:

Stored size: 990 Bytes

Contents

module TrueUnits
    TRUE_UNITS_METHOD_NAME_PREFIX = "tu_"
    TRUE_UNITS_METHOD_NAME_PATTERN = /^tu_[a-z]+$/

    def method_missing(name, *args, &block)
      if TRUE_UNITS_METHOD_NAME_PATTERN.match name
        true_units_string(name[TRUE_UNITS_METHOD_NAME_PREFIX.length..-1], self)
      else
        super.method_missing(name, *args, &block)
      end
    end

    def respond_to?(name, include_private = false)
      TRUE_UNITS_METHOD_NAME_PATTERN.match(name) || super
    end

    private

    # will return true units depending value by predefined type
    def true_units type, value
      if value >= 5 && value <= 20
        v = 3
      else
        last_digit = value.to_s.last.to_i
        case last_digit
        when 1
          v = 1
        when 2..4
          v = 2
        else
          v = 3
        end
      end
      I18n.t "true_units.#{type}.v#{v}"
    end

    def true_units_string(type, value)
      "#{value.to_i} #{true_units(type, value.to_i)}"
    end
  end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
true_units-0.0.1 lib/true_units/modules/true_units.rb