Module: Tableless::InstanceMethods
- Included in:
- ActiveRecord::TablelessModel
- Defined in:
- lib/tableless_model/instance_methods.rb
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Object
Overriding getter for the underlying hash keys so that only the defined attributes can be read.
-
#[]=(attribute_name, value) ⇒ Object
Overriding setter for the underlying hash keys so that only the defined attributes can be set.
-
#initialize(init_attributes = {}, &block) ⇒ Object
On initialising an instance of a tableless model, sets the default values for all the attributes defined.
-
#inspect ⇒ Object
The Hash object displays inspect information in the format.
-
#merge(hash) ⇒ Object
Since the object should only allow the defined attributes merging is forbidden.
-
#respond_to?(method_name) ⇒ Boolean
Returns true if the method name specified corresponds to the key of an attribute defined for the tableless model.
Instance Method Details
#[](attribute_name) ⇒ Object
Overriding getter for the underlying hash keys so that only the defined attributes can be read
36 37 38 39 |
# File 'lib/tableless_model/instance_methods.rb', line 36 def [](attribute_name) raise NoMethodError, "The attribute #{attribute_name} is undefined" unless self.class.attributes.has_key? attribute_name.to_s self.class.cast(attribute_name, super(attribute_name.to_s)) end |
#[]=(attribute_name, value) ⇒ Object
Overriding setter for the underlying hash keys so that only the defined attributes can be set
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tableless_model/instance_methods.rb', line 47 def []=(attribute_name, value) raise NoMethodError, "The attribute #{attribute_name} is undefined" unless self.class.attributes.has_key? attribute_name.to_s return_value = super(attribute_name.to_s, self.class.cast(attribute_name, value)) if self.__owner_object # This makes the tableless model compatible with partial_updates: # whenever a property in the tableless model is changed, we force the parent/owner object to a changed state # by updating it with a new, updated instance of the tableless model self.__owner_object.send "#{self.__serialized_attribute.to_s}=".to_sym, self end return_value end |
#initialize(init_attributes = {}, &block) ⇒ Object
On initialising an instance of a tableless model, sets the default values for all the attributes defined. Optionally, initialises the tableless model with the values specified as arguments, instead, overriding the default values
12 13 14 15 16 17 |
# File 'lib/tableless_model/instance_methods.rb', line 12 def initialize(init_attributes = {}, &block) super &block self.class.attributes.each_pair {|attribute_name, | self.send("#{attribute_name}=", [:default])} init_attributes.each_pair {|k,v| self.send("#{k}=", v)} if init_attributes end |
#inspect ⇒ Object
The Hash object displays inspect information in the format
"{:a=>1, :b=>2}"
to make the tableless model look a bit more like regular models, it shows instead the inspect information in this format:
"<#MyTablelessModel a=1 b=2>"
74 75 76 |
# File 'lib/tableless_model/instance_methods.rb', line 74 def inspect "<##{self.class.to_s}" << self.keys.sort.inject(""){|result, k| result << " #{k}=#{self[k].inspect}"; result } << ">" end |
#merge(hash) ⇒ Object
Since the object should only allow the defined attributes merging is forbidden
84 85 86 |
# File 'lib/tableless_model/instance_methods.rb', line 84 def merge(hash) raise NoMethodError end |
#respond_to?(method_name) ⇒ Boolean
Returns true if the method name specified corresponds to the key of an attribute defined for the tableless model
26 27 28 |
# File 'lib/tableless_model/instance_methods.rb', line 26 def respond_to?(method_name) key?(method_name) ? true : super end |