Module: Mysql2Model::Container

Defined in:
lib/mysql2_model/container.rb

Overview

include this module in your class to inherit all of this awsomeness

Examples:

class MyClass
  include Mysql2Model::Container
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Provide dynamic accessors to the internal attributes only



46
47
48
49
50
51
52
# File 'lib/mysql2_model/container.rb', line 46

def method_missing(method, *args, &block)
  if @attributes.key?(method)
    @attributes[method]
  else
    super
  end
end

Instance Method Details

#[](method) ⇒ Object

member notation convenience method



55
56
57
# File 'lib/mysql2_model/container.rb', line 55

def [](method)
  @attributes[method]
end

#[]=(method, value) ⇒ Object

member notation convenience method



59
60
61
# File 'lib/mysql2_model/container.rb', line 59

def []=(method,value)
  @attributes[method] = value
end

#attribute_get(method) ⇒ Object

datamapper style resource convenience method



64
65
66
# File 'lib/mysql2_model/container.rb', line 64

def attribute_get(method)
  @attributes[method]
end

#attribute_set(method, value) ⇒ Object

datamapper style resource convenience method



69
70
71
# File 'lib/mysql2_model/container.rb', line 69

def attribute_set(method,value)
  @attributes[method]=value
end

#idObject

Delegate the id to the attribute :id insted of the Object_id



41
42
43
# File 'lib/mysql2_model/container.rb', line 41

def id
  @attributes[:id]
end

#initialize(row) ⇒ Object

Parameters:

  • row (Hash)

    Expects a row from a Mysql2 resultset

Raises:

  • (ArgumentError)

    Row must be a Hash



26
27
28
29
# File 'lib/mysql2_model/container.rb', line 26

def initialize(row)
  raise ArgumentError, "must be a hash" unless row.is_a?(Hash)
  @attributes = row
end