Class: Dataset::Record::Heirarchy

Inherits:
Object
  • Object
show all
Defined in:
lib/dataset/record/heirarchy.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class) ⇒ Heirarchy

Returns a new instance of Heirarchy.



9
10
11
12
13
14
# File 'lib/dataset/record/heirarchy.rb', line 9

def initialize(base_class)
  @base_class = base_class
  @class_name = base_class.name
  @table_name = base_class.table_name
  @columns    = base_class.columns
end

Instance Attribute Details

#base_classObject (readonly)

Returns the value of attribute base_class.



5
6
7
# File 'lib/dataset/record/heirarchy.rb', line 5

def base_class
  @base_class
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



5
6
7
# File 'lib/dataset/record/heirarchy.rb', line 5

def class_name
  @class_name
end

#columnsObject (readonly)

Returns the value of attribute columns.



5
6
7
# File 'lib/dataset/record/heirarchy.rb', line 5

def columns
  @columns
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



5
6
7
# File 'lib/dataset/record/heirarchy.rb', line 5

def table_name
  @table_name
end

Instance Method Details

#finder_name(klass) ⇒ Object



42
43
44
# File 'lib/dataset/record/heirarchy.rb', line 42

def finder_name(klass)
  klass.name.underscore.gsub('/', '_').sub(/^(\w)_/, '\1').gsub(/_(\w)_/, '_\1')
end

#id_cache_keyObject



16
17
18
# File 'lib/dataset/record/heirarchy.rb', line 16

def id_cache_key
  @id_cache_key ||= table_name
end

#id_finder_name(klass) ⇒ Object



46
47
48
# File 'lib/dataset/record/heirarchy.rb', line 46

def id_finder_name(klass)
  "#{finder_name(klass)}_id".to_sym
end

#id_finder_namesObject



20
21
22
# File 'lib/dataset/record/heirarchy.rb', line 20

def id_finder_names
  @id_finder_names ||= [id_finder_name(base_class)]
end

#model_finder_name(klass) ⇒ Object



50
51
52
# File 'lib/dataset/record/heirarchy.rb', line 50

def model_finder_name(klass)
  finder_name(klass).pluralize.to_sym
end

#model_finder_namesObject



24
25
26
# File 'lib/dataset/record/heirarchy.rb', line 24

def model_finder_names
  @model_finder_names ||= [model_finder_name(base_class)]
end

#timestamp_columnsObject



54
55
56
57
58
59
60
61
# File 'lib/dataset/record/heirarchy.rb', line 54

def timestamp_columns
   @timestamp_columns ||= begin
     timestamps = %w(created_at created_on updated_at updated_on)
     columns.select do |column|
       timestamps.include?(column.name)
     end
   end
end

#to_sObject



28
29
30
# File 'lib/dataset/record/heirarchy.rb', line 28

def to_s
  "#<Heirarchy: #{table_name}>"
end

#update(record_class) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/dataset/record/heirarchy.rb', line 32

def update(record_class)
  record_class.ancestors.each do |c|
    finder_name = model_finder_name(c)
    unless model_finder_names.include?(finder_name)
      model_finder_names << finder_name
      id_finder_names << id_finder_name(c)
    end
  end
end