Class: Arel::Enhance::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/arel/enhance/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
# File 'lib/arel/enhance/node.rb', line 9

def initialize(object)
  @object = object
  @root_node = self
  @dirty = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



74
75
76
77
78
79
# File 'lib/arel/enhance/node.rb', line 74

def method_missing(name, *args, &block)
  child = children[name.to_s]
  return super if child.nil?

  child
end

Instance Attribute Details

#local_pathObject

Returns the value of attribute local_path.



4
5
6
# File 'lib/arel/enhance/node.rb', line 4

def local_path
  @local_path
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/arel/enhance/node.rb', line 5

def object
  @object
end

#parentObject

Returns the value of attribute parent.



6
7
8
# File 'lib/arel/enhance/node.rb', line 6

def parent
  @parent
end

#root_nodeObject

Returns the value of attribute root_node.



7
8
9
# File 'lib/arel/enhance/node.rb', line 7

def root_node
  @root_node
end

Instance Method Details

#[](key) ⇒ Object



86
87
88
# File 'lib/arel/enhance/node.rb', line 86

def [](key)
  children.fetch(key.to_s)
end

#add(path_node, node) ⇒ Object



51
52
53
54
55
56
# File 'lib/arel/enhance/node.rb', line 51

def add(path_node, node)
  node.local_path = path_node
  node.parent = self
  node.root_node = root_node
  children[path_node.value.to_s] = node
end

#child_at_path(path_items) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/arel/enhance/node.rb', line 90

def child_at_path(path_items)
  selected_node = self
  path_items.each do |path_item|
    selected_node = selected_node[path_item]
    return nil if selected_node.nil?
  end
  selected_node
end

#childrenObject



115
116
117
# File 'lib/arel/enhance/node.rb', line 115

def children
  @children ||= {}
end

#contextObject



123
124
125
# File 'lib/arel/enhance/node.rb', line 123

def context
  @context ||= {}
end

#dirty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/arel/enhance/node.rb', line 39

def dirty?
  root_node.instance_values.fetch('dirty')
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
31
32
33
# File 'lib/arel/enhance/node.rb', line 25

def each(&block)
  return enum_for(:each) unless block_given?

  yield self

  children.each_value do |child|
    child.each(&block)
  end
end

#fieldsObject



119
120
121
# File 'lib/arel/enhance/node.rb', line 119

def fields
  @fields ||= []
end

#full_pathObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/arel/enhance/node.rb', line 103

def full_path
  the_path = [local_path]
  current_parent = parent

  while current_parent
    the_path.unshift current_parent.local_path
    current_parent = current_parent.parent
  end

  the_path.compact
end

#inspectObject



15
16
17
# File 'lib/arel/enhance/node.rb', line 15

def inspect
  recursive_inspect('')
end

#query(**kwargs) ⇒ Object



99
100
101
# File 'lib/arel/enhance/node.rb', line 99

def query(**kwargs)
  Arel::Enhance::Query.call(self, kwargs)
end

#removeObject



43
44
45
# File 'lib/arel/enhance/node.rb', line 43

def remove
  mutate(nil, remove: true)
end

#replace(new_arel_node) ⇒ Object



47
48
49
# File 'lib/arel/enhance/node.rb', line 47

def replace(new_arel_node)
  mutate(new_arel_node)
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/arel/enhance/node.rb', line 81

def respond_to_missing?(method, include_private = false)
  child = children[method.to_s]
  child.present? || super
end

#to_sql(engine = Table.engine) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/arel/enhance/node.rb', line 58

def to_sql(engine = Table.engine)
  return nil if children.empty?

  if object.respond_to?(:to_sql)
    object.to_sql(engine)
  else
    collector = Arel::Collectors::SQLString.new
    collector = engine.connection.visitor.accept object, collector
    collector.value
  end
end

#to_sql_and_binds(engine = Table.engine) ⇒ Object



70
71
72
# File 'lib/arel/enhance/node.rb', line 70

def to_sql_and_binds(engine = Table.engine)
  object.to_sql_and_binds(engine)
end

#valueObject



19
20
21
22
23
# File 'lib/arel/enhance/node.rb', line 19

def value
  return unless value?

  fields.first
end

#value?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/arel/enhance/node.rb', line 35

def value?
  children.empty?
end