Class: I18nFlow::YamlAstProxy::Mapping

Inherits:
Node
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/i18n_flow/yaml_ast_proxy/mapping.rb

Constant Summary

Constants inherited from Node

Node::TAG_IGNORE, Node::TAG_ONLY, Node::TAG_TODO

Instance Attribute Summary

Attributes inherited from Node

#file_path, #ignored_violation, #node, #parent, #scopes

Instance Method Summary collapse

Methods inherited from Node

#==, #clone, #get, #initialize, #keys, #to_yaml, #value

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

This class inherits a constructor from I18nFlow::YamlAstProxy::Node

Instance Method Details

#batchObject



34
35
36
37
38
39
40
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 34

def batch
  @locked = true
  yield
ensure
  @locked = false
  synchronize!
end

#delete(key) ⇒ Object



28
29
30
31
32
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 28

def delete(key)
  indexed_object.delete(key)
  cache.delete(key)
  synchronize!
end

#eachObject



10
11
12
13
14
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 10

def each
  indexed_object.each do |k, _|
    yield k, cache[k]
  end
end

#merge!(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 42

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

  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

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



20
21
22
23
24
25
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 20

def set(key, value)
  super.tap do
    cache.delete(key)
    synchronize!
  end
end

#sort_keys!Object



58
59
60
61
62
63
64
# File 'lib/i18n_flow/yaml_ast_proxy/mapping.rb', line 58

def sort_keys!
  @indexed_object = indexed_object
    .sort_by { |k, v| [v.is_a?(Psych::Nodes::Mapping) ? 1 : 0, k] }
    .to_h
  @cache = nil
  synchronize!
end

#valuesObject



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

def values
  indexed_object.map { |k, _| cache[k] }
end