Module: I18nFlow::YamlAstProxy

Defined in:
lib/i18n_flow/yaml_ast_proxy.rb,
lib/i18n_flow/yaml_ast_proxy/node.rb,
lib/i18n_flow/yaml_ast_proxy/mapping.rb,
lib/i18n_flow/yaml_ast_proxy/sequence.rb,
lib/i18n_flow/yaml_ast_proxy/node_meta_data.rb

Defined Under Namespace

Modules: NodeMetaData Classes: Mapping, Node, Sequence

Class Method Summary collapse

Class Method Details

.create(node, parent: nil, scopes: [], file_path: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/i18n_flow/yaml_ast_proxy.rb', line 7

def self.create(node, parent: nil, scopes: [], file_path: nil)
  case node
  when NilClass
    nil
  when Node
    if node.parent == parent \
      && node.scopes == scopes \
      && node.file_path == file_path
      node
    else
      node.class.new(node.node,
        parent:    parent,
        scopes:    scopes,
        file_path: file_path,
      )
    end
  when Psych::Nodes::Stream, Psych::Nodes::Document
    Node.new(node.children.first,
      parent:    node,
      scopes:    scopes,
      file_path: file_path,
    )
  when Psych::Nodes::Mapping
    Mapping.new(node,
      parent:    parent,
      scopes:    scopes,
      file_path: file_path,
    )
  when Psych::Nodes::Sequence
    Sequence.new(node,
      parent:    parent,
      scopes:    scopes,
      file_path: file_path,
    )
  else
    Node.new(node,
      parent:    parent,
      scopes:    scopes,
      file_path: file_path,
    )
  end
end

.first_key_node_of(node) ⇒ Object



76
77
78
# File 'lib/i18n_flow/yaml_ast_proxy.rb', line 76

def self.first_key_node_of(node)
  first_node_of(node, 0)
end

.first_value_node_of(node) ⇒ Object



80
81
82
# File 'lib/i18n_flow/yaml_ast_proxy.rb', line 80

def self.first_value_node_of(node)
  first_node_of(node, 1)
end

.mark_as_todo(ast) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/i18n_flow/yaml_ast_proxy.rb', line 58

def self.mark_as_todo(ast)
  if ast.alias?
    return
  end
  if ast.scalar?
    ast.node.tag = '!todo'

    # https://github.com/ruby/psych/blob/f30b65befa4f0a5a8548d482424a84a2383b0284/ext/psych/yaml/emitter.c#L1187
    ast.node.plain = ast.node.quoted = false

    return
  end

  ast.each do |k, v|
    mark_as_todo(v)
  end
end

.new_rootObject



50
51
52
53
54
55
56
# File 'lib/i18n_flow/yaml_ast_proxy.rb', line 50

def self.new_root
  doc = Psych::Nodes::Document.new([], [], true)
  doc.children << Psych::Nodes::Mapping.new
  stream = Psych::Nodes::Stream.new
  stream.children << doc
  create(stream)
end