Class: Locomotive::Steam::Models::I18nField

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/locomotive/steam/models/i18n_field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, translations) ⇒ I18nField

Returns a new instance of I18nField.



12
13
14
15
# File 'lib/locomotive/steam/models/i18n_field.rb', line 12

def initialize(name, translations)
  @name = name
  self.translations = translations
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/locomotive/steam/models/i18n_field.rb', line 10

def name
  @name
end

#translationsObject Also known as: __translations__, to_hash

Returns the value of attribute translations.



10
11
12
# File 'lib/locomotive/steam/models/i18n_field.rb', line 10

def translations
  @translations
end

Instance Method Details

#[](locale) ⇒ Object



22
23
24
# File 'lib/locomotive/steam/models/i18n_field.rb', line 22

def [](locale)
  @translations[locale]
end

#[]=(locale, value) ⇒ Object



26
27
28
# File 'lib/locomotive/steam/models/i18n_field.rb', line 26

def []=(locale, value)
  @translations[locale] = value
end

#apply(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/locomotive/steam/models/i18n_field.rb', line 49

def apply(&block)
  if default
    @translations = Hash.new(yield(default))
  else
    each do |l, _value|
      self[l] = block.arity == 2 ? yield(_value, l) : yield(_value)
    end
  end
  self
end

#blank?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/locomotive/steam/models/i18n_field.rb', line 42

def blank?
  @translations.default.blank? && (
    @translations.blank? ||
    @translations.values.all? { |v| v.blank? }
  )
end

#duplicate(new_name) ⇒ Object



60
61
62
# File 'lib/locomotive/steam/models/i18n_field.rb', line 60

def duplicate(new_name)
  self.class.new(new_name, self.translations)
end

#each(&block) ⇒ Object



38
39
40
# File 'lib/locomotive/steam/models/i18n_field.rb', line 38

def each(&block)
  @translations.each(&block)
end

#initialize_copy(field) ⇒ Object



17
18
19
20
# File 'lib/locomotive/steam/models/i18n_field.rb', line 17

def initialize_copy(field)
  super
  self.translations = field.translations.dup
end

#serialize(attributes) ⇒ Object



68
69
70
# File 'lib/locomotive/steam/models/i18n_field.rb', line 68

def serialize(attributes)
  attributes[@name] = @translations
end

#to_jsonObject



72
73
74
# File 'lib/locomotive/steam/models/i18n_field.rb', line 72

def to_json
  to_hash.to_json
end