Class: PassiveColumns::Loader
- Inherits:
-
Object
- Object
- PassiveColumns::Loader
- Defined in:
- lib/passive_columns/loader.rb
Overview
Loader is a class helper that loads a column value from the database if it is not loaded yet.
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#passive_columns ⇒ Object
readonly
Returns the value of attribute passive_columns.
Instance Method Summary collapse
-
#initialize(model, passive_columns) ⇒ Loader
constructor
A new instance of Loader.
- #load(column, force: false) ⇒ any
Constructor Details
#initialize(model, passive_columns) ⇒ Loader
Returns a new instance of Loader.
10 11 12 13 |
# File 'lib/passive_columns/loader.rb', line 10 def initialize(model, passive_columns) @model = model @passive_columns = passive_columns end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/passive_columns/loader.rb', line 6 def model @model end |
#passive_columns ⇒ Object (readonly)
Returns the value of attribute passive_columns.
6 7 8 |
# File 'lib/passive_columns/loader.rb', line 6 def passive_columns @passive_columns end |
Instance Method Details
#load(column, force: false) ⇒ any
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/passive_columns/loader.rb', line 18 def load(column, force: false) return yield if block_given? model.send(column) rescue ActiveModel::MissingAttributeError allowed_columns = (force ? [column] : passive_columns).map(&:to_s) raise if allowed_columns.exclude?(column.to_s) || identity_constraints.value?(nil) value = pick_value(column) model[column] = value model.send(:clear_attribute_change, column) model[column] end |