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.



12
13
14
15
16
17
18
19
# File 'lib/arel/enhance/node.rb', line 12

def initialize(object)
  @object = object
  @root_node = self
  @fields = []
  @children = {}
  @dirty = false
  @context = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

  child
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#local_pathObject

Returns the value of attribute local_path.



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

def local_path
  @local_path
end

#objectObject

Returns the value of attribute object.



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

def object
  @object
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#root_nodeObject

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

Instance Method Details

#[](key) ⇒ Object



92
93
94
# File 'lib/arel/enhance/node.rb', line 92

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

#add(path_node, node) ⇒ Object



57
58
59
60
61
62
# File 'lib/arel/enhance/node.rb', line 57

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



96
97
98
99
100
101
102
103
# File 'lib/arel/enhance/node.rb', line 96

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

#dirty?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/arel/enhance/node.rb', line 45

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

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

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
39
# File 'lib/arel/enhance/node.rb', line 31

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

  yield self

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

#full_pathObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/arel/enhance/node.rb', line 109

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



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

def inspect
  recursive_inspect('')
end

#query(**kwargs) ⇒ Object



105
106
107
# File 'lib/arel/enhance/node.rb', line 105

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

#removeObject



49
50
51
# File 'lib/arel/enhance/node.rb', line 49

def remove
  mutate(nil, remove: true)
end

#replace(new_arel_node) ⇒ Object



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

def replace(new_arel_node)
  mutate(new_arel_node)
end

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

Returns:

  • (Boolean)


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

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

#to_sql(engine = Table.engine) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/arel/enhance/node.rb', line 64

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



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

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

#valueObject



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

def value
  return unless value?

  @fields.first
end

#value?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/arel/enhance/node.rb', line 41

def value?
  children.empty?
end