Class: FlattenRecord::Meta::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/flatten_record/meta/node.rb

Direct Known Subclasses

Column, NormalizedAttr

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, target_model, model) ⇒ Node

Returns a new instance of Node.



6
7
8
9
10
# File 'lib/flatten_record/meta/node.rb', line 6

def initialize(parent, target_model, model)
  @parent = parent
  @target_model = target_model.to_s.underscore
  @model = model.to_s.underscore
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/flatten_record/meta/node.rb', line 4

def children
  @children
end

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/flatten_record/meta/node.rb', line 4

def model
  @model
end

#parentObject (readonly)

Returns the value of attribute parent.



4
5
6
# File 'lib/flatten_record/meta/node.rb', line 4

def parent
  @parent
end

#target_modelObject (readonly)

Returns the value of attribute target_model.



4
5
6
# File 'lib/flatten_record/meta/node.rb', line 4

def target_model
  @target_model
end

Instance Method Details

#prefixObject



34
35
36
37
38
39
# File 'lib/flatten_record/meta/node.rb', line 34

def prefix
  return custom_prefix unless custom_prefix.nil?
  return "" if is_parent_root?
  
  "#{target_model_name}_" 
end

#traverse_by(attr, value) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/flatten_record/meta/node.rb', line 20

def traverse_by(attr, value)
  attr_value = send("#{attr}")

  if !value.respond_to?(:to_s) || !attr_value.respond_to?(:to_s)
    raise "traverse error: to_s method required for comparison"
  end
 
  if value.to_s.downcase == attr_value.to_s.downcase
    return self
  else 
    return nil
  end
end