Class: FlattenRecord::Meta::NormalizedAttr

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

Direct Known Subclasses

AssociatedAttr, RootNode

Instance Attribute Summary

Attributes inherited from Node

#model, #parent, #target_model

Instance Method Summary collapse

Methods inherited from Node

#initialize

Constructor Details

This class inherits a constructor from FlattenRecord::Meta::Node

Instance Method Details

#[](key) ⇒ Object



18
19
20
# File 'lib/flatten_record/meta/normalized_attr.rb', line 18

def [](key)
  instance_variable_get("@#{key}") 
end

#all_columnsObject



9
10
11
12
# File 'lib/flatten_record/meta/normalized_attr.rb', line 9

def all_columns
  child_columns = @include.values.collect(&:all_columns)
  @base_columns + @methods + @compute + child_columns.flatten 
end

#associated_modelsObject



14
15
16
# File 'lib/flatten_record/meta/normalized_attr.rb', line 14

def associated_models
  @include.blank? ? target_model : @include.values.collect(&:associated_models).flatten
end

#denormalize(instance, to_record) ⇒ Object



5
6
7
# File 'lib/flatten_record/meta/normalized_attr.rb', line 5

def denormalize(instance, to_record)
  denormalize_children(instance, to_record)
end

#id_columnObject



45
46
47
# File 'lib/flatten_record/meta/normalized_attr.rb', line 45

def id_column
  return @id_column unless @id_column.nil?
end

#prefixObject



22
23
24
# File 'lib/flatten_record/meta/normalized_attr.rb', line 22

def prefix
  custom_prefix || "#{parent.prefix}#{target_model_name}_"
end

#traverse_by(attr, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/flatten_record/meta/normalized_attr.rb', line 26

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
    found = nil
    @include.values.each do |node|
      n = node.traverse_by(attr, value)
      found = n unless n.nil?
    end 
    return found
  end
end