Class: CachedColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/cached_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, options = {}, &block) ⇒ CachedColumn

Returns a new instance of CachedColumn.



6
7
8
9
10
# File 'lib/cached_column.rb', line 6

def initialize(column, options = {}, &block)
  @column  = column
  @options = options
  @block   = block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/cached_column.rb', line 4

def block
  @block
end

#columnObject

Returns the value of attribute column.



4
5
6
# File 'lib/cached_column.rb', line 4

def column
  @column
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/cached_column.rb', line 4

def options
  @options
end

Instance Method Details

#before_save(record) ⇒ Object



12
13
14
15
# File 'lib/cached_column.rb', line 12

def before_save(record)
  record.send("#{column}=", computed_value(record))
  true
end

#computed_value(record) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/cached_column.rb', line 17

def computed_value(record)
  if block
    record.instance_eval(&block)
  else
    record.send(options[:method] || column)
  end
end