Module: CacheableDelegator::ClassMethods

Defined in:
lib/cacheable_delegator.rb,
lib/cacheable_delegator.rb

Overview

Builder methods

Instance Method Summary collapse

Instance Method Details

#add_custom_column(col_name, opts = {}) ⇒ Object

Note: This must occur either during the .cache_and_delegate call

or after it. 
.cache_and_delegate *will* empty out custom_columns

And implicitly, @@source_class has already been defined at this point

By default, raise an error if the column_name does not correspond
 to an instance_method of @@source_class, unless the :bespoke option
 is true

opts: Hash of standard ActiveRecord::ConnectionAdapters::Column options

and several custom opts:
  :bespoke => if true, then create this column on CachedRecord even
        if its source_class does not have method_defined?(column_name)
  :serialize => this is passed along to upgrade_schema!
        which will then serialize the column via ActiveRecord.serialize


112
113
114
115
116
117
118
119
120
# File 'lib/cacheable_delegator.rb', line 112

def add_custom_column(col_name, opts={})
  col_str = col_name.to_s
  is_bespoke = opts.delete :bespoke     
  if !self.source_class.method_defined?(col_str) && is_bespoke != true
    raise ArgumentError, "Since :bespoke != true, #{self.source_class} was expected to respond_to? :#{col_str}"
  end

  custom_columns[col_name.to_s] = opts
end

#build_cache(source_obj) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cacheable_delegator.rb', line 161

def build_cache(source_obj)
  att_hsh = self.value_column_names.inject({}) do |hsh, cname|
    hsh[cname] = source_obj.send cname
    hsh
  end

  obj = self.new(att_hsh)
  obj.source_record = source_obj

  obj
end

#cache_and_delegate(klass, opts = {}, &blk) ⇒ Object

this method defines the relation to an existing ActiveModel it does not change the schema unless invoked with the bang!

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cacheable_delegator.rb', line 20

def cache_and_delegate(klass, opts={}, &blk)
  raise ArgumentError, "Must pass in a class, not a #{klass}" unless klass.is_a?(Class)
  self.source_class = klass
  belongs_to :source_record, class_name: self.source_class.to_s      

  # This is why you have to define custom columns in the given block
  # or after the call to cache and delegate

  reset_custom_columns!
  if block_given?
    yield self
  end


  self.source_class
end

#cache_and_delegate!(*args) ⇒ Object

also runs upgrade_schema!



39
40
41
42
43
# File 'lib/cacheable_delegator.rb', line 39

def cache_and_delegate!(*args)
  cache_and_delegate(*args)

  upgrade_schema!
end

#create_cache(obj) ⇒ Object



173
174
175
176
177
178
# File 'lib/cacheable_delegator.rb', line 173

def create_cache(obj)
  c = build_cache(obj)
  c.save 

  c
end

#custom_columnsObject



122
123
124
# File 'lib/cacheable_delegator.rb', line 122

def custom_columns
  self._custom_columns ||= {}
end

#reset_custom_columns!Object



126
127
128
# File 'lib/cacheable_delegator.rb', line 126

def reset_custom_columns!
  self._custom_columns = {}
end

#source_columnsObject



83
84
85
# File 'lib/cacheable_delegator.rb', line 83

def source_columns
  source_class.columns.reject{|c| EXCLUDED_FIELDS.any?{|f| f.to_s == c.name }}
end

#source_reflectionsObject



87
88
89
# File 'lib/cacheable_delegator.rb', line 87

def source_reflections
  source_class.reflections
end

#upgrade_schema!Object

assumes @@source_class has been set

will call ActiveRecord::Base.serialize based on parameters passed in from



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cacheable_delegator.rb', line 50

def upgrade_schema!
  # first we create a hash of source_columns
  source_column_hash = source_columns.inject({}) do |shash, source_col|
    source_col_atts = COL_ATTRIBUTES_TO_UPGRADE.inject({}){|hsh, att| hsh[att.to_sym] = source_col.send att; hsh  }
    shash[source_col.name] = source_col_atts

    shash
  end

  # then we merge it with custom_columns
  columns_to_add = source_column_hash.merge(custom_columns)


  # then we add them all in using .col method from active_record_inline_schema
  columns_to_add.each_pair do |col_name, col_atts|
    is_serialize = col_atts.delete(:serialize)
    col col_name, col_atts

    # by default, the client passes in serialize: true for plain serialization
    #  and has the option to pass in serialize: Hash
    if is_serialize
      serialize_klass = (is_serialize == true) ? Object : is_serialize
      serialize col_name, serialize_klass
    end
  end

  self.auto_upgrade!( :gentle => true )
end

#value_column_namesObject

value columns are just columns that aren’t in EXCLUDED_FIELDS



92
93
94
# File 'lib/cacheable_delegator.rb', line 92

def value_column_names
  self.column_names - EXCLUDED_FIELDS.map{|f| f.to_s}
end