Class: Mongify::Database::DataRow
- Inherits:
-
Object
- Object
- Mongify::Database::DataRow
- 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
-
#delete(key) ⇒ Object
Deletes a given key from the object.
-
#include?(key) ⇒ Boolean
See if a given key is included.
-
#initialize(hash = {}) ⇒ DataRow
constructor
A new instance of DataRow.
-
#inspect ⇒ Object
Passes Inspect onto the internal hash.
-
#keys ⇒ Object
Returns a list of available keys.
-
#method_missing(meth, *args, &blk) ⇒ Object
Added the ability to read and write attributes in the hash.
-
#read_attribute(key) ⇒ Object
Used to manually read an attribute.
-
#respond_to?(method) ⇒ Boolean
Updated respond_to to return true if it’s a key the hash.
-
#to_hash ⇒ Object
Outputs an hash This is used to write into the no sql database.
-
#write_attribute(key, value) ⇒ Object
Used to manually write an attribute.
Constructor Details
#initialize(hash = {}) ⇒ DataRow
Returns a new instance of DataRow.
5 6 7 8 |
# File 'lib/mongify/database/data_row.rb', line 5 def initialize(hash={}) hash = {} if hash.nil? @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
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongify/database/data_row.rb', line 52 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
16 17 18 |
# File 'lib/mongify/database/data_row.rb', line 16 def delete(key) @hash.delete(key.to_s) end |
#include?(key) ⇒ Boolean
See if a given key is included
11 12 13 |
# File 'lib/mongify/database/data_row.rb', line 11 def include?(key) @hash.has_key?(key.to_s) end |
#inspect ⇒ Object
Passes Inspect onto the internal hash
41 42 43 |
# File 'lib/mongify/database/data_row.rb', line 41 def inspect @hash.inspect end |
#keys ⇒ Object
Returns a list of available keys
21 22 23 |
# File 'lib/mongify/database/data_row.rb', line 21 def keys @hash.keys end |
#read_attribute(key) ⇒ Object
Used to manually read an attribute
32 33 34 |
# File 'lib/mongify/database/data_row.rb', line 32 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
46 47 48 49 |
# File 'lib/mongify/database/data_row.rb', line 46 def respond_to?(method) return true if @hash.has_key?(method.gsub('=', '')) super(method) end |
#to_hash ⇒ Object
Outputs an hash This is used to write into the no sql database
27 28 29 |
# File 'lib/mongify/database/data_row.rb', line 27 def to_hash @hash end |
#write_attribute(key, value) ⇒ Object
Used to manually write an attribute
36 37 38 |
# File 'lib/mongify/database/data_row.rb', line 36 def write_attribute(key, value) @hash[key.to_s] = value end |