Class: I18nFlow::YamlAstProxy::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
NodeMetaData
Defined in:
lib/i18n_flow/yaml_ast_proxy/node.rb

Direct Known Subclasses

Mapping, Sequence

Constant Summary collapse

TAG_IGNORE =
/^!ignore:(args|key)$/
TAG_TODO =
/^!todo(?::([,a-zA-Z_-]+))?$/
TAG_ONLY =
/^!only(?::([,a-zA-Z_-]+))?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NodeMetaData

#alias?, #anchor, #end_column, #end_line, #full_key, #has_anchor?, #key, #locale, #mapping?, #marked_as_only?, #marked_as_todo?, #num_lines, #scalar?, #sequence?, #start_column, #start_line, #todo_locales, #valid_locale?, #valid_locales

Constructor Details

#initialize(node, parent: nil, scopes: [], file_path: nil) ⇒ Node

Returns a new instance of Node.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 22

def initialize(
  node,
  parent:    nil,
  scopes:    [],
  file_path: nil
)
  @node      = node
  @parent    = parent
  @scopes    = scopes
  @file_path = file_path

  parse_tag!(node.tag)
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



17
18
19
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 17

def file_path
  @file_path
end

#ignored_violationObject (readonly)

Returns the value of attribute ignored_violation.



18
19
20
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 18

def ignored_violation
  @ignored_violation
end

#nodeObject

Returns the value of attribute node.



13
14
15
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 13

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



15
16
17
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 15

def parent
  @parent
end

#scopesObject (readonly)

Returns the value of attribute scopes.



16
17
18
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 16

def scopes
  @scopes
end

Instance Method Details

#==(other) ⇒ Object



77
78
79
80
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 77

def ==(other)
  return false unless other.is_a?(self.class)
  identity_data == other.identity_data
end

#batchObject



73
74
75
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 73

def batch
  yield
end

#cloneObject



91
92
93
94
95
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 91

def clone
  super.tap do |n|
    n.node = node.clone
  end
end

#get(key) ⇒ Object Also known as: []



36
37
38
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 36

def get(key)
  wrap(indexed_object[key], key: key)
end

#keysObject



86
87
88
89
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 86

def keys
  return [] if scalar? || alias?
  indexed_object.keys
end

#merge!(other) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 50

def merge!(other)
  return unless other&.is_a?(Node)

  if scalar? && other.scalar?
    node.value = other.value
    return
  end

  if !scalar? && !other.scalar?
    batch do
      other.batch do
        other.each do |k, rhs|
          if (lhs = self[k])
            lhs.merge!(rhs)
          else
            self[k] = rhs.node
          end
        end
      end
    end
  end
end

#set(key, value) ⇒ Object Also known as: []=



41
42
43
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 41

def set(key, value)
  indexed_object[key] = value
end

#to_yamlObject



82
83
84
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 82

def to_yaml
  parent.to_yaml(nil, line_width: -1)
end

#valueObject



46
47
48
# File 'lib/i18n_flow/yaml_ast_proxy/node.rb', line 46

def value
  node.value if node.respond_to?(:value)
end