Class: Leftovers::ConfigLoader::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/leftovers/config_loader/node.rb

Constant Summary collapse

TO_RUBY_INSTANCE =
::Psych::Visitors::ToRuby.new(
  ::Psych::ScalarScanner.new(class_loader),
  class_loader
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, file, name = nil) ⇒ Node

Returns a new instance of Node.



15
16
17
18
19
# File 'lib/leftovers/config_loader/node.rb', line 15

def initialize(node, file, name = nil)
  @node = node
  @file = file
  @name = name
end

Instance Attribute Details

#errorObject

Returns the value of attribute error.



13
14
15
# File 'lib/leftovers/config_loader/node.rb', line 13

def error
  @error
end

#fileObject (readonly)

Returns the value of attribute file.



12
13
14
# File 'lib/leftovers/config_loader/node.rb', line 12

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/leftovers/config_loader/node.rb', line 12

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



12
13
14
# File 'lib/leftovers/config_loader/node.rb', line 12

def node
  @node
end

Instance Method Details

#all_errorsObject



33
34
35
# File 'lib/leftovers/config_loader/node.rb', line 33

def all_errors
  Array(error_message) + children.flat_map(&:all_errors)
end

#array?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/leftovers/config_loader/node.rb', line 87

def array?
  node.is_a?(::Psych::Nodes::Sequence)
end

#childrenObject



49
50
51
52
53
54
55
56
57
# File 'lib/leftovers/config_loader/node.rb', line 49

def children
  @children ||= if hash?
    prepare_hash_children
  elsif array?
    node.children.map { |value| self.class.new(value, file, "#{name} value") }
  else
    []
  end
end

#each_key(&block) ⇒ Object



67
68
69
# File 'lib/leftovers/config_loader/node.rb', line 67

def each_key(&block)
  keys.each(&block)
end

#error_messageObject



29
30
31
# File 'lib/leftovers/config_loader/node.rb', line 29

def error_message
  "Config SchemaError: #{location} #{@error}" if @error
end

#hash?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/leftovers/config_loader/node.rb', line 41

def hash?
  node.is_a?(::Psych::Nodes::Mapping)
end

#integer?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/leftovers/config_loader/node.rb', line 91

def integer?
  to_ruby.is_a?(::Integer)
end

#keysObject



59
60
61
# File 'lib/leftovers/config_loader/node.rb', line 59

def keys
  @keys ||= pairs.map(&:first)
end

#locationObject



45
46
47
# File 'lib/leftovers/config_loader/node.rb', line 45

def location
  "#{file.relative_path}:#{node.start_line + 1}:#{node.start_column}"
end

#name_Object



21
22
23
# File 'lib/leftovers/config_loader/node.rb', line 21

def name_
  "#{name} " if name
end

#pairsObject



63
64
65
# File 'lib/leftovers/config_loader/node.rb', line 63

def pairs
  @pairs ||= children.each_slice(2).to_a
end

#scalar?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/leftovers/config_loader/node.rb', line 83

def scalar?
  !array? and !hash?
end

#string?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/leftovers/config_loader/node.rb', line 79

def string?
  to_ruby.is_a?(::String)
end

#to_rubyObject



71
72
73
# File 'lib/leftovers/config_loader/node.rb', line 71

def to_ruby
  @to_ruby ||= TO_RUBY_INSTANCE.accept(node)
end

#to_sObject



37
38
39
# File 'lib/leftovers/config_loader/node.rb', line 37

def to_s
  to_ruby.to_s
end

#to_symObject



75
76
77
# File 'lib/leftovers/config_loader/node.rb', line 75

def to_sym
  to_ruby.to_sym if string?
end

#valid?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/leftovers/config_loader/node.rb', line 25

def valid?
  !error
end