class TwitterCldr::Shared::Unit

Constants

DEFAULT_FORM

Attributes

locale[R]
value[R]

Public Class Methods

create(value, locale = TwitterCldr.locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 12
def create(value, locale = TwitterCldr.locale)
  subtype_for(locale).new(value, locale)
end
new(value, locale = TwitterCldr.locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 83
def initialize(value, locale = TwitterCldr.locale)
  @value = value
  @locale = locale
end

Private Class Methods

all_unit_methods_for(locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 52
def all_unit_methods_for(locale)
  unit_methods[locale] ||= all_unit_types_for(locale).map do |unit_type|
    unit_type_to_method_name(unit_type).to_sym
  end
end
all_unit_types_for(locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 41
def all_unit_types_for(locale)
  unit_types[locale] ||= begin
    resource = resource_for(locale)
    lengths = resource[:unitLength].keys

    lengths.each_with_object(Set.new) do |length, ret|
      ret.merge(resource[:unitLength][length].keys)
    end
  end
end
reader_for(locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 18
def reader_for(locale)
  readers[locale] ||= TwitterCldr::DataReaders::NumberDataReader.new(locale)
end
readers() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 74
def readers
  @readers ||= {}
end
resource_for(locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 22
def resource_for(locale)
  TwitterCldr.get_locale_resource(locale, :units)[locale][:units]
end
subtype_for(locale) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 26
def subtype_for(locale)
  subtypes[locale] ||= begin
    klass = Class.new(Unit)

    all_unit_types_for(locale).each do |unit_type|
      method_name = unit_type_to_method_name(unit_type)
      klass.send(:define_method, method_name) do |*args|
        format(unit_type, *args)
      end
    end

    klass
  end
end
subtypes() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 62
def subtypes
  @subtypes ||= {}
end
unit_methods() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 70
def unit_methods
  @unit_methods ||= {}
end
unit_type_to_method_name(unit_type) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 58
def unit_type_to_method_name(unit_type)
  unit_type.to_s.gsub('-', '_')
end
unit_types() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 66
def unit_types
  @unit_types ||= {}
end

Public Instance Methods

unit_types() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 88
def unit_types
  self.class.send(:all_unit_methods_for, locale)
end

Private Instance Methods

format(unit_type, options = {}) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 94
def format(unit_type, options = {})
  form = options.fetch(:form, DEFAULT_FORM)

  if variant = variant_for(form, unit_type)
    variant.sub('{0}', formatted_value)
  end
end
formatted_value() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 102
def formatted_value
  if value.is_a?(Numeric)
    self.class.send(:reader_for, locale).format_number(value)
  else
    value
  end
end
plural_rule() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 117
def plural_rule
  TwitterCldr::Formatters::Plurals::Rules.rule_for(value, locale)
end
resource() click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 121
def resource
  @resource ||= self.class.send(:resource_for, locale)
end
variant_for(form, unit_type) click to toggle source
# File lib/twitter_cldr/shared/unit.rb, line 110
def variant_for(form, unit_type)
  variant = resource[:unitLength]
    .fetch(form, {})
    .fetch(unit_type, {})
    .fetch(plural_rule.to_sym, nil)
end