Module: Tanuki::ModelBehavior
- Included in:
- Tanuki_Model
- Defined in:
- lib/tanuki/behavior/model_behavior.rb
Overview
Tanuki::ModelBehavior contains basic methods for a framework model. In is included in the base model class.
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(mod) ⇒ Object
Extends the including module with Tanuki::ModelBehavior::ClassMethods.
Instance Method Summary collapse
-
#[](attribute) ⇒ Object
Returns the value of a given attribute, loading the model on demand.
-
#[]=(attribute, value) ⇒ Object
Sets the value of a given attribute.
-
#errors ⇒ Object
Returns the modification errors hash.
-
#get_updates ⇒ Object
Returns model updates hash.
-
#has_errors? ⇒ Boolean
Returns
trueif there are any modification errors. -
#initialize(data = {}, lazy = false) ⇒ Object
Creates new model with dataset row
data. -
#internals_get(attribute) ⇒ Object
Returns transport representation of data.
-
#internals_set(attribute, internal_value) ⇒ Object
Sets transport representation of data.
Class Method Details
.included(mod) ⇒ Object
Extends the including module with Tanuki::ModelBehavior::ClassMethods.
113 114 115 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 113 def included(mod) mod.extend ClassMethods end |
Instance Method Details
#[](attribute) ⇒ Object
Returns the value of a given attribute, loading the model on demand.
15 16 17 18 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 15 def [](attribute) ensure_loaded! unless self.class[attribute].present_in(@_data) self.class[attribute].get(@_data) end |
#[]=(attribute, value) ⇒ Object
Sets the value of a given attribute.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 21 def []=(attribute, value) @_errors ||= {} @_original ||= {} begin @_original[attribute] = self[attribute] unless @_original.include? attribute self.class[attribute].set(@_data, value) @_errors.delete(attribute) rescue @_errors[attribute] = {:value => value, :error => $!} end end |
#errors ⇒ Object
Returns the modification errors hash.
34 35 36 37 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 34 def errors @_errors ||= {} @_errors end |
#get_updates ⇒ Object
Returns model updates hash. This method is used internally to generate a data source update.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 52 def get_updates # TODO Rewrite this properly @_original ||= {} original_data = {} self.class.attributes.each_pair do |name, attrib| attrib.set(original_data, @_original[name]) end updates = {} original_data.each_pair do |field, value| updates[field] = data[field] if data[field] != original_data[field] end updates end |
#has_errors? ⇒ Boolean
Returns true if there are any modification errors.
67 68 69 70 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 67 def has_errors? @_errors ||= {} @_errors == {} end |
#initialize(data = {}, lazy = false) ⇒ Object
Creates new model with dataset row data. If the model is lazy, data should contain only row keys.
9 10 11 12 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 9 def initialize(data={}, lazy=false) @_data = data @_loaded = !lazy end |
#internals_get(attribute) ⇒ Object
Returns transport representation of data.
40 41 42 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 40 def internals_get(attribute) self.class[attribute].internals_get(@_data) end |
#internals_set(attribute, internal_value) ⇒ Object
Sets transport representation of data.
45 46 47 48 |
# File 'lib/tanuki/behavior/model_behavior.rb', line 45 def internals_set(attribute, internal_value) @_errors ||= {} internals_set(self.class[attribute], @_data) end |