Class: FlattenRecord::Meta::Column

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

Direct Known Subclasses

ComputeColumn, IdColumn, MethodColumn

Instance Attribute Summary collapse

Attributes inherited from Node

#children, #model, #parent, #target_model

Instance Method Summary collapse

Methods inherited from Node

#prefix, #traverse_by

Constructor Details

#initialize(parent, col, target_model, model) ⇒ Column

Returns a new instance of Column.



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

def initialize(parent, col, target_model, model)
  super(parent, target_model, model)
  @column = col
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



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

def column
  @column
end

Instance Method Details

#column_prefix(default_name) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flatten_record/meta/column.rb', line 16

def column_prefix(default_name)
  col_prefix = parent._key.to_s
    
  if target_model.table_name.to_s == col_prefix ||
    target_model_name == col_prefix
    return default_name
  end

  default_prefix = @custom_prefix || parent.parent.prefix
  parent_key = parent._key.to_s + "_"
  model_prefix = target_model_name + "_"
  
  default_prefix + parent_key + model_prefix +  @column.name.to_s
end

#denormalize(instance, to_record) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flatten_record/meta/column.rb', line 35

def denormalize(instance, to_record)
  return nullify(to_records) if instance.blank?

  if instance.respond_to?(@column.name)
    to_record = assign_value(to_record, name) do |record|
      instance.send(@column.name.to_sym)
    end
  else
    raise "#{@column.name} is not found in #{instance.inspect}"
  end
  to_record
end

#nameObject



11
12
13
14
# File 'lib/flatten_record/meta/column.rb', line 11

def name
  default_name = parent.prefix + @column.name.to_s   
  is_parent_root? ? default_name : column_prefix(default_name)
end

#nullify(to_record) ⇒ Object



48
49
50
51
52
# File 'lib/flatten_record/meta/column.rb', line 48

def nullify(to_record)
  assign_value(to_record, name) do |record|
    nil
  end
end

#typeObject



31
32
33
# File 'lib/flatten_record/meta/column.rb', line 31

def type
  @column.type
end