Class: I27r::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/i18n/translation/lib/yaml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Line

Returns a new instance of Line.



12
13
14
15
16
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 12

def initialize(text, options = {})
  @generated = !!options[:generated] || text.ends_with?('  #g')
  @yaml = YAML.load text.to_s + ' '
  @text = text
end

Instance Attribute Details

#textObject (readonly)

Returns the value of attribute text.



9
10
11
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 9

def text
  @text
end

Instance Method Details

#generated?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 30

def generated?
  @generated
end

#indentObject



26
27
28
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 26

def indent
  yaml? ? @text.scan(/^ */).first : ''
end

#keyObject



18
19
20
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 18

def key
  yaml? ? @yaml.keys.first : nil
end

#to_sObject



45
46
47
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 45

def to_s
  "#{@text}#{'  #g' if generated? && yaml? && !value.nil? && !@text.ends_with?('  #g')}"
end

#valueObject



22
23
24
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 22

def value
  yaml? ? @yaml.values.first : nil
end

#value=(val) ⇒ Object



38
39
40
41
42
43
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 38

def value=(val)
  if @yaml[self.key] != val
    @yaml[self.key] = val
    generate_text self.indent
  end
end

#yaml?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/generators/i18n/translation/lib/yaml.rb', line 34

def yaml?
  @yaml.is_a?(Hash) && @yaml.keys.first.is_a?(String)
end