Module: MobilityTyped::Writer

Extended by:
Mobility::Plugin
Defined in:
lib/mobility_typed/writer.rb

Defined Under Namespace

Classes: TypeError

Constant Summary collapse

TYPES_MAP =
{
  String => :string,
  Integer => :integer,
  Float => :float,
  TrueClass => :bool,
  FalseClass => :bool,
  Array => :array,
  Hash => :hash
}.freeze

Class Method Summary collapse

Class Method Details

.check_type(name, type) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/mobility_typed/writer.rb', line 52

def self.check_type(name, type)
  <<-EOL
  if !value.nil? 
    value_type = MobilityTyped::Writer.value_type(value)
    if value_type != :#{type}        
      raise MobilityTyped::Writer::TypeError, "#{name}= called with \#{value_type}, #{type} expected"
    end
  end
  EOL
end

.setup_sourceObject



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mobility_typed/writer.rb', line 40

def self.setup_source
  <<-EOL
    return super(value) if options[:super]
    if (locale &&= locale.to_sym)
      #{'Mobility.enforce_available_locales!(locale)' if I18n.enforce_available_locales}
      options[:locale] = true
    else
      locale = Mobility.locale
    end
  EOL
end

.value_type(value) ⇒ Object



63
64
65
# File 'lib/mobility_typed/writer.rb', line 63

def self.value_type(value)
  TYPES_MAP[value.class] || value.class
end