Class: Lkml::Simple::DictVisitor
- Inherits:
-
Object
- Object
- Lkml::Simple::DictVisitor
- Includes:
- Tree::Visitor
- Defined in:
- lib/lkml/simple.rb
Instance Attribute Summary collapse
-
#depth ⇒ Object
Returns the value of attribute depth.
Instance Method Summary collapse
-
#initialize ⇒ DictVisitor
constructor
A new instance of DictVisitor.
- #update_tree(target, update) ⇒ Object
- #visit(document) ⇒ Object
- #visit_block(node) ⇒ Object
- #visit_container(node) ⇒ Object
- #visit_list(node) ⇒ Object
- #visit_pair(node) ⇒ Object
- #visit_token(token) ⇒ Object
Constructor Details
#initialize ⇒ DictVisitor
Returns a new instance of DictVisitor.
23 24 25 26 |
# File 'lib/lkml/simple.rb', line 23 def initialize @depth = -1 @logger = Logger.new($stderr) end |
Instance Attribute Details
#depth ⇒ Object
Returns the value of attribute depth.
21 22 23 |
# File 'lib/lkml/simple.rb', line 21 def depth @depth end |
Instance Method Details
#update_tree(target, update) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/lkml/simple.rb', line 28 def update_tree(target, update) keys = update.keys raise KeyError, "Dictionary to update with cannot have multiple keys." if keys.length != 1 key = keys.first val = update[key] if Keys::PLURAL_KEYS.include?(key) plural_key = Keys.pluralize(key) if target.key?(plural_key) target[plural_key] << val else target[plural_key] = [val] end elsif target.key?(key) if @depth.zero? @logger.warn( format( 'Multiple declarations of top-level key "%s" found. Using the last-declared value.', key ) ) target[key] = val else raise KeyError, "Key \"#{key}\" already exists in tree " \ "and would overwrite the existing value." end else target[key] = val end end |
#visit(document) ⇒ Object
61 62 63 |
# File 'lib/lkml/simple.rb', line 61 def visit(document) visit_container(document.container) end |
#visit_block(node) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/lkml/simple.rb', line 77 def visit_block(node) container_dict = node.container ? node.container.accept(self) : {} container_dict = container_dict.dup container_dict["name"] = node.name.accept(self) if node.name { node.type.accept(self) => container_dict } end |
#visit_container(node) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/lkml/simple.rb', line 65 def visit_container(node) container = {} unless node.items.empty? @depth += 1 node.items.each do |item| update_tree(container, item.accept(self)) end @depth -= 1 end container end |
#visit_list(node) ⇒ Object
84 85 86 |
# File 'lib/lkml/simple.rb', line 84 def visit_list(node) { node.type.accept(self) => node.items.map { |item| item.accept(self) } } end |
#visit_pair(node) ⇒ Object
88 89 90 |
# File 'lib/lkml/simple.rb', line 88 def visit_pair(node) { node.type.accept(self) => node.value.accept(self) } end |
#visit_token(token) ⇒ Object
92 93 94 |
# File 'lib/lkml/simple.rb', line 92 def visit_token(token) token.value.to_s end |