Class: Mongify::Database::DataRow

Inherits:
Object
  • Object
show all
Defined in:
lib/mongify/database/data_row.rb

Overview

Class that will be used to allow user to modify data for the given row

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ DataRow

Returns a new instance of DataRow.



5
6
7
# File 'lib/mongify/database/data_row.rb', line 5

def initialize(hash={})
  @hash = hash.dup.stringify_keys!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Added the ability to read and write attributes in the hash



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mongify/database/data_row.rb', line 51

def method_missing(meth, *args, &blk)
  match = meth.to_s.match(/^([a-zA-Z\_]+)(=|$)$/)
  if match
    attribute, setter = match[1], !match[2].blank?
    if setter
      write_attribute(attribute, args.first)
    else
      read_attribute(attribute)
    end
  else
    super(meth, *args, &blk)
  end
end

Instance Method Details

#delete(key) ⇒ Object

Deletes a given key from the object



15
16
17
# File 'lib/mongify/database/data_row.rb', line 15

def delete(key)
  @hash.delete(key)
end

#include?(key) ⇒ Boolean

See if a given key is included

Returns:

  • (Boolean)


10
11
12
# File 'lib/mongify/database/data_row.rb', line 10

def include?(key)
  @hash.has_key?(key)
end

#inspectObject

Passes Inspect onto the internal hash



40
41
42
# File 'lib/mongify/database/data_row.rb', line 40

def inspect
  @hash.inspect
end

#keysObject

Returns a list of available keys



20
21
22
# File 'lib/mongify/database/data_row.rb', line 20

def keys
  @hash.keys
end

#read_attribute(key) ⇒ Object

Used to manually read an attribute



31
32
33
# File 'lib/mongify/database/data_row.rb', line 31

def read_attribute(key)
  @hash[key.to_s]
end

#respond_to?(method) ⇒ Boolean

Updated respond_to to return true if it’s a key the hash

Returns:

  • (Boolean)


45
46
47
48
# File 'lib/mongify/database/data_row.rb', line 45

def respond_to?(method)
  return true if @hash.has_key?(method.gsub('=', ''))
  super(method)
end

#to_hashObject

Outputs an hash This is used to write into the no sql database



26
27
28
# File 'lib/mongify/database/data_row.rb', line 26

def to_hash
  @hash
end

#write_attribute(key, value) ⇒ Object

Used to manually write an attribute



35
36
37
# File 'lib/mongify/database/data_row.rb', line 35

def write_attribute(key, value)
  @hash[key.to_s] = value
end