Class: Patriot::Util::DBClient::HashRecord
- Defined in:
- lib/patriot/util/db_client/hash_record.rb
Overview
a record implementation based on Hash
Instance Method Summary collapse
- #get_id ⇒ Object
-
#initialize(record) ⇒ HashRecord
constructor
A new instance of HashRecord.
-
#method_missing(mth, *args, &blk) ⇒ Object
used as accessors.
- #to_hash(keys = @record.keys) ⇒ Object
Constructor Details
#initialize(record) ⇒ HashRecord
Returns a new instance of HashRecord.
10 11 12 13 14 |
# File 'lib/patriot/util/db_client/hash_record.rb', line 10 def initialize(record) @record = {} # ignore fixnum which is not column name record.each{|k,v| @record[k.to_sym] = v unless k.is_a?(Fixnum)} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mth, *args, &blk) ⇒ Object
used as accessors
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/patriot/util/db_client/hash_record.rb', line 29 def method_missing(mth, *args, &blk) key = mth type = :get if key.to_s.end_with?("=") key = key.to_s.slice(0..-2).to_sym type = :set end if type == :get if @record.has_key?(mth) return @record[mth] else return nil end elsif type == :set @record[key] = args[0] return end super end |
Instance Method Details
#get_id ⇒ Object
17 18 19 |
# File 'lib/patriot/util/db_client/hash_record.rb', line 17 def get_id return @record[:id] end |
#to_hash(keys = @record.keys) ⇒ Object
22 23 24 25 26 |
# File 'lib/patriot/util/db_client/hash_record.rb', line 22 def to_hash(keys = @record.keys) hash = {} keys.each{|k| hash[k] = @record[k]} return hash end |