Class: Dataset::Record::Meta
- Inherits:
-
Object
- Object
- Dataset::Record::Meta
- Defined in:
- lib/dataset/record/meta.rb
Overview
A mechanism to cache information about an ActiveRecord class to speed things up a bit for insertions, finds, and method generation.
Instance Attribute Summary collapse
-
#class_name ⇒ Object
readonly
:nodoc:.
-
#columns ⇒ Object
readonly
:nodoc:.
-
#record_class ⇒ Object
readonly
:nodoc:.
-
#table_name ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
- #descendants ⇒ Object
- #finder_name(klass) ⇒ Object
- #id_cache_key ⇒ Object
- #id_finder_names ⇒ Object
- #inheriting_record? ⇒ Boolean
-
#initialize(record_class) ⇒ Meta
constructor
A new instance of Meta.
- #model_finder_names ⇒ Object
- #timestamp_columns ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(record_class) ⇒ Meta
Returns a new instance of Meta.
13 14 15 16 17 18 |
# File 'lib/dataset/record/meta.rb', line 13 def initialize(record_class) @record_class = record_class @class_name = record_class.name @table_name = record_class.table_name @columns = record_class.columns end |
Instance Attribute Details
#class_name ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/dataset/record/meta.rb', line 7 def class_name @class_name end |
#columns ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/dataset/record/meta.rb', line 7 def columns @columns end |
#record_class ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/dataset/record/meta.rb', line 7 def record_class @record_class end |
#table_name ⇒ Object (readonly)
:nodoc:
7 8 9 |
# File 'lib/dataset/record/meta.rb', line 7 def table_name @table_name end |
Instance Method Details
#descendants ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/dataset/record/meta.rb', line 52 def descendants if record_class.respond_to?(:self_and_descendents_from_active_record) record_class.self_and_descendents_from_active_record else record_class.self_and_descendants_from_active_record end end |
#finder_name(klass) ⇒ Object
60 61 62 |
# File 'lib/dataset/record/meta.rb', line 60 def finder_name(klass) klass.name.underscore.gsub('/', '_').sub(/^(\w)_/, '\1').gsub(/_(\w)_/, '_\1') end |
#id_cache_key ⇒ Object
20 21 22 |
# File 'lib/dataset/record/meta.rb', line 20 def id_cache_key @id_cache_key ||= table_name end |
#id_finder_names ⇒ Object
37 38 39 40 41 42 |
# File 'lib/dataset/record/meta.rb', line 37 def id_finder_names @id_finder_names ||= begin names = descendants.collect {|c| finder_name c} names.uniq.collect {|n| "#{n}_id".to_sym} end end |
#inheriting_record? ⇒ Boolean
24 25 26 |
# File 'lib/dataset/record/meta.rb', line 24 def inheriting_record? !record_class.descends_from_active_record? end |
#model_finder_names ⇒ Object
44 45 46 |
# File 'lib/dataset/record/meta.rb', line 44 def model_finder_names @record_finder_names ||= descendants.collect {|c| finder_name(c).pluralize.to_sym}.uniq end |
#timestamp_columns ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/dataset/record/meta.rb', line 28 def ||= begin = %w(created_at created_on updated_at updated_on) columns.select do |column| .include?(column.name) end end end |
#to_s ⇒ Object
48 49 50 |
# File 'lib/dataset/record/meta.rb', line 48 def to_s "#<RecordMeta: #{table_name}>" end |