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
20
# File 'lib/arel/enhance/node.rb', line 12

def initialize(object)
  @object = object
  @path = Path.new
  @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



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

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

  child
end

Instance Attribute Details

#children ⇒ Object (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#context ⇒ Object (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#fields ⇒ Object (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#object ⇒ Object

Returns the value of attribute object.



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

def object
  @object
end

#parent ⇒ Object

Returns the value of attribute parent.



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

def parent
  @parent
end

#path ⇒ Object

Returns the value of attribute path.



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

def path
  @path
end

#root_node ⇒ Object

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



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

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

#add(path_node, node) ⇒ Object



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

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

#child_at_path(path_items) ⇒ Object



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

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)


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

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

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

Yields:

  • (_self)

Yield Parameters:



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

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

  yield self

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

#inspect ⇒ Object



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

def inspect
  recursive_inspect('')
end

#query(**kwargs) ⇒ Object



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

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

#remove ⇒ Object



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

def remove
  mutate(nil, remove: true)
end

#replace(new_node) ⇒ Object



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

def replace(new_node)
  mutate(new_node)
end

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

Returns:

  • (Boolean)


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

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

#to_sql(engine = Table.engine) ⇒ Object



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

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



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

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

#value ⇒ Object



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

def value
  return unless value?

  @fields.first
end

#value? ⇒ Boolean

Returns:

  • (Boolean)


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

def value?
  children.empty?
end