Class: I18nAttributes::GeneratorHelpers::YamlFileData

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_attributes/generator_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options, &block) ⇒ YamlFileData

Returns a new instance of YamlFileData.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/i18n_attributes/generator_helpers.rb', line 15

def initialize(*options, &block)
  options = options.extract_options!
  return self if test = options.delete(:test) || false
  locale = options.delete(:locale) || raise("locale is required.")
  singular_name = options.delete(:singular_name) || raise("singular_name is required.")
  human_name = options.delete(:human_name) || raise("human_name is required.")
  attributes = options.delete(:attributes) || raise("attributes is required.")
  model_i18n_scope = options.delete(:model_i18n_scope) || raise("model_i18n_scope is required.")
  @locale = locale.to_s
  @singular_name = singular_name.to_s
  @human_name = human_name.to_s
  @attributes = attributes.to_hash
  @model_i18n_scope = model_i18n_scope.to_s


  set_locale_translator(&block)
  set_columns_hash(&block)
  generate(&block)
  @yaml_file_data
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def attributes
  @attributes
end

#columns_hashObject (readonly)

Returns the value of attribute columns_hash.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def columns_hash
  @columns_hash
end

#human_nameObject (readonly)

Returns the value of attribute human_name.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def human_name
  @human_name
end

#localeObject (readonly)

Returns the value of attribute locale.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def locale
  @locale
end

#locale_translatorObject (readonly)

Returns the value of attribute locale_translator.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def locale_translator
  @locale_translator
end

#model_i18n_scopeObject (readonly)

Returns the value of attribute model_i18n_scope.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def model_i18n_scope
  @model_i18n_scope
end

#singular_nameObject (readonly)

Returns the value of attribute singular_name.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def singular_name
  @singular_name
end

#yaml_file_dataObject (readonly)

Returns the value of attribute yaml_file_data.



12
13
14
# File 'lib/i18n_attributes/generator_helpers.rb', line 12

def yaml_file_data
  @yaml_file_data
end

Instance Method Details

#generate(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/i18n_attributes/generator_helpers.rb', line 36

def generate(&block)
  @yaml_file_data =
    YAML.dump_stream(
      {
        locale => {
          model_i18n_scope => {
            "models" => {
              singular_name => @locale_translator ? translate(human_name, &block) : human_name
            },
            "attributes" => {
              singular_name => @columns_hash
            }
          }
        }
      }
    )
end

#set_columns_hash(&block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/i18n_attributes/generator_helpers.rb', line 54

def set_columns_hash(&block)
  @columns_hash =
    Hash[ @attributes.keys.map {|k|
            [
              k.to_s,
              @locale_translator ? translate(k, &block) : k.to_s.humanize
            ]
          }
        ]
end

#set_locale_translator(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/i18n_attributes/generator_helpers.rb', line 65

def set_locale_translator(&block)
  translator = I18nAttributes::Configuration.translator
  locale_translator = translator[@locale.to_sym] || translator[@locale.to_sym]
  @locale_translator =
    if !locale_translator.blank? and locale_translator.class == Proc
      locale_translator
    else
       nil
    end
end

#translate(word) {|word.to_s| ... } ⇒ Object

Yields:

  • (word.to_s)


76
77
78
79
# File 'lib/i18n_attributes/generator_helpers.rb', line 76

def translate(word, &block)
  yield word.to_s if block
  @locale_translator.call(word.to_s)
end