Class: JunglePath::Query::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/jungle_path/query/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, alias_, node, parent, symbol) ⇒ Entity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/jungle_path/query/entity.rb', line 12

def initialize(token, alias_, node, parent, symbol)
  @name = Entity.get_name_from_token(token)
  if token[-1] == "@"
    @left_join = true
  else
    @left_join = false
  end
  if token.include? '.'
    @join_field_override = token.split('.')[1]
    if @join_field_override[-1] == '@'
      @join_field_override = @join_field_override[0..-2]
    end
  else
    @join_field_override = nil
  end

  @alias_ = alias_
  @node = node
  @parent = parent
  @symbol = symbol
  @fields = nil
  @filter = nil
  @sort = nil
  @limit = nil
  @offset = nil
  @field_node = nil #If this entity object is created from within the field list put the field's node here.
end

Instance Attribute Details

#alias_Object

Returns the value of attribute alias_.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def alias_
  @alias_
end

#field_nodeObject

Returns the value of attribute field_node.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def field_node
  @field_node
end

#fieldsObject

Returns the value of attribute fields.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def fields
  @fields
end

#filterObject

Returns the value of attribute filter.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def filter
  @filter
end

#join_field_overrideObject

Returns the value of attribute join_field_override.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def join_field_override
  @join_field_override
end

#left_joinObject (readonly)

Returns the value of attribute left_join.



11
12
13
# File 'lib/jungle_path/query/entity.rb', line 11

def left_join
  @left_join
end

#limitObject

Returns the value of attribute limit.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def limit
  @limit
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def name
  @name
end

#nodeObject

Returns the value of attribute node.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def node
  @node
end

#offsetObject

Returns the value of attribute offset.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def offset
  @offset
end

#parametersObject

Returns the value of attribute parameters.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def parameters
  @parameters
end

#parentObject

Returns the value of attribute parent.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def parent
  @parent
end

#sortObject

Returns the value of attribute sort.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def sort
  @sort
end

#symbolObject

Returns the value of attribute symbol.



10
11
12
# File 'lib/jungle_path/query/entity.rb', line 10

def symbol
  @symbol
end

Class Method Details

.get_name_from_token(token) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/jungle_path/query/entity.rb', line 111

def self.get_name_from_token(token)
  if token[-1] == "@"
    token = token[0..-2]
  end
  if token.include? '.'
    token = token.split('.')[0]
  end
  token = token.downcase.to_sym
end

.select_node_from_array_by_token(nodes, token) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/jungle_path/query/entity.rb', line 121

def self.select_node_from_array_by_token(nodes, token)
  # find closest matching node.
  # see gen_node_tree.rb lines 60+...
  node = nodes[0] # default
  if token.include? '.'
    name = token.split('.')[1]
    if name[-1] == '@'
      name = name[0..-2]
    end
    nodes.each do |n|
      if n.child_table_join_column_name.to_s.start_with?(name)
        node = n #found match
        break
      end
    end
  end
  node #return node.
end

Instance Method Details

#fields_that_are_entities_countObject



101
102
103
104
105
106
107
108
109
# File 'lib/jungle_path/query/entity.rb', line 101

def fields_that_are_entities_count
  count = 0
  fields.each do |field|
    if field.is_entity?
      count += 1
    end
  end
  count
end

#find_entity(name) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jungle_path/query/entity.rb', line 55

def find_entity(name)
  field = find_field(name)
  if field
    return field
  end

  if fields
    fields.each do |f|
      if f.is_entity?
        field = f.find_entity(name)
        if field
          return f
          #return field
        end
      end
    end
  end
  field
end

#find_field(name) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/jungle_path/query/entity.rb', line 44

def find_field(name)
  if fields
    fields.each do |field|
      if field.name == name
        return field
      end
    end
  end
  nil
end

#find_parent_aliasObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jungle_path/query/entity.rb', line 89

def find_parent_alias
  #binding.pry
  alias_ = nil
  p = parent
  while p and p.fields.count == p.fields_that_are_entities_count do
    p = p.parent
  end
  alias_ = p.alias_ if p
  alias_ = alias_.to_sym if alias_
  alias_
end

#find_symbolObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/jungle_path/query/entity.rb', line 75

def find_symbol
  symbol = node.symbol
  p = parent
  while true do
    if p.fields.count == p.fields_that_are_entities_count and p.parent
      symbol = p.node.symbol
      p = p.parent
    else
      break
    end
  end
  symbol
end

#is_entity?Boolean



40
41
42
# File 'lib/jungle_path/query/entity.rb', line 40

def is_entity?
  true
end