Class: Hash
Constant Summary collapse
- COMPACT_NILS =
true
- DONT_COMPACT_NILS =
false
Instance Method Summary collapse
- #slice(*desired_keys) ⇒ Object
-
#to_clause(join_string, compact_nils = DONT_COMPACT_NILS) ⇒ Object
Takes an optional block to provide a single “field = ?” type subclause for each key-value pair.
- #to_set_clause ⇒ Object
- #to_where_clause ⇒ Object
Instance Method Details
#slice(*desired_keys) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/m4dbi/hash.rb', line 42 def slice( *desired_keys ) Hash[ *( select { |key,value| desired_keys.include? key }.flatten ) ] end |
#to_clause(join_string, compact_nils = DONT_COMPACT_NILS) ⇒ Object
Takes an optional block to provide a single “field = ?” type subclause for each key-value pair.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/m4dbi/hash.rb', line 7 def to_clause( join_string, compact_nils = DONT_COMPACT_NILS ) # The clause items and the values have to be in the same order. keys_ = keys if block_given? mapping = keys_.map { |field| yield field } else mapping = keys_.map { |field| "#{field} = ?" } end clause = mapping.join( join_string ) values_ = keys_.map { |key| self[ key ] } if compact_nils values_.compact! end [ clause, values_ ] end |
#to_set_clause ⇒ Object
35 36 37 |
# File 'lib/m4dbi/hash.rb', line 35 def to_set_clause to_clause( ", " ) end |
#to_where_clause ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/m4dbi/hash.rb', line 25 def to_where_clause to_clause( " AND ", COMPACT_NILS ) { |field| if self[ field ].nil? "#{field} IS NULL" else "#{field} = ?" end } end |