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
11
# File 'lib/flatten_record/meta/node.rb', line 6

def initialize(parent, target_model, model)
  @parent = parent
  @target_model = target_model.is_a?(ActiveRecord::Base) ? 
    target_model : target_model.to_s.camelize.constantize
  @model = model
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



27
28
29
30
31
32
# File 'lib/flatten_record/meta/node.rb', line 27

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

#traverse_by(attr, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flatten_record/meta/node.rb', line 13

def traverse_by(attr, value)
  attr_value = instance_variable_get("@#{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 == attr_value.to_s
    return self
  else 
    return nil
  end
end