Module: Tableless::InstanceMethods

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

allows calls to attribute_name?, returning a true or false depending on whether the actual value of the attribute is truthy or falsy



97
98
99
100
101
102
103
104
# File 'lib/tableless_model/instance_methods.rb', line 97

def method_missing sym, *args, &block
  attribute_name = sym.to_s.gsub(/^(.*)\?$/, "\\1")
  if respond_to?(attribute_name)
    !!send(attribute_name, *args, &block) 
  else
    super sym, *args, &block
  end
end

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
40
# 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
  default = super(attribute_name)
  self.class.cast(attribute_name, default.is_a?(Proc) ? default.call : default)
end

#[]=(attribute_name, value) ⇒ Object

Overriding setter for the underlying hash keys so that only the defined attributes can be set

Raises:

  • (NoMethodError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tableless_model/instance_methods.rb', line 48

def []=(attribute_name, value)
  raise NoMethodError, "The attribute #{attribute_name} is undefined" unless self.class.attributes.has_key? attribute_name
  
  return_value = super(attribute_name, 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}=".to_sym, options[:default].is_a?(Proc) ? options[:default].call : options[:default])}
  init_attributes.each_pair {|k,v| self[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>"


75
76
77
78
79
80
# File 'lib/tableless_model/instance_methods.rb', line 75

def inspect
  "<##{self.class.to_s}" << self.keys.sort{ |a,b| a.to_s <=> b.to_s  }.inject("") do |result, k| 
    result += " #{k}=#{ self[k].is_a?(Time) ? self[k].strftime("%Y-%m-%d %H:%M:%S %Z") : self[k].inspect}"
    result 
  end + ">"
end

#merge(hash) ⇒ Object

Since the object should only allow the defined attributes merging is forbidden

Raises:

  • (NoMethodError)


88
89
90
# File 'lib/tableless_model/instance_methods.rb', line 88

def merge(hash)
  raise NoMethodError
end

#respond_to?(method) ⇒ 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