Module: Tableless::InstanceMethods

Included in:
ActiveRecord::TablelessModel
Defined in:
lib/tableless_model/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#[](attribute_name) ⇒ Object

Overriding getter for the underlying hash keys so that only the defined attributes can be read

Raises:

  • (NoMethodError)


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

Raises:

  • (NoMethodError)


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, options| self.send("#{attribute_name}=", options[:default])}
  init_attributes.each_pair {|k,v| self.send("#{k}=", v)} if init_attributes
end

#inspectObject

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

Raises:

  • (NoMethodError)


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

Returns:

  • (Boolean)


26
27
28
# File 'lib/tableless_model/instance_methods.rb', line 26

def respond_to?(method_name)
  key?(method_name) ? true : super
end