Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/dm_ruby_extensions/extend_hash.rb
Overview
Instance Method Summary collapse
-
#convert_date(date_symbol_or_string) ⇒ Object
takes a Hash (like the params hash), and converts the Rails date attributes to a real Date object.
-
#convert_datetime(date_symbol_or_string) ⇒ Object
takes a Hash (like the params hash), and converts the Rails datetime attributes to a real Date object.
-
#rekey(key_map = nil, &block) ⇒ Object
Borrowed from github.com/rubyworks/facets —————————————————————————— Rekey a hash:.
-
#rekey!(key_map = nil, &block) ⇒ Object
Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
-
#url_query_string(leading_slash = true) ⇒ Object
Convert hash of parameters to a query string —————————————————————————— rubocop:disable Style/OptionalBooleanParameter.
Instance Method Details
#convert_date(date_symbol_or_string) ⇒ Object
takes a Hash (like the params hash), and converts the Rails date attributes to a real Date object. => 2012, ‘start_date(2i)’ => 11, ‘start_date(3i)’ => 23.convert_date(:start_date)
9 10 11 12 13 14 |
# File 'lib/dm_ruby_extensions/extend_hash.rb', line 9 def convert_date(date_symbol_or_string) attribute = date_symbol_or_string.to_s return nil if self["#{attribute}(1i)"].nil? || self["#{attribute}(2i)"].nil? || self["#{attribute}(3i)"].nil? Date.new(self["#{attribute}(1i)"].to_i, self["#{attribute}(2i)"].to_i, self["#{attribute}(3i)"].to_i) end |
#convert_datetime(date_symbol_or_string) ⇒ Object
takes a Hash (like the params hash), and converts the Rails datetime attributes to a real Date object.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dm_ruby_extensions/extend_hash.rb', line 19 def convert_datetime(date_symbol_or_string) attribute = date_symbol_or_string.to_s if self["#{attribute}(1i)"].nil? || self["#{attribute}(2i)"].nil? || self["#{attribute}(3i)"].nil? || self["#{attribute}(4i)"].nil? || self["#{attribute}(5i)"].nil? return nil end Time.local(self["#{attribute}(1i)"].to_i, self["#{attribute}(2i)"].to_i, self["#{attribute}(3i)"].to_i, self["#{attribute}(4i)"].to_i, self["#{attribute}(5i)"].to_i) end |
#rekey(key_map = nil, &block) ⇒ Object
Borrowed from github.com/rubyworks/facets
Rekey a hash:
rekey()
rekey(from_key => to_key, ...)
rekey{|from_key| to_key}
rekey{|from_key, value| to_key}
If a key map is given, then the first key is changed to the second key.
foo = { :a=>1, :b=>2 }
foo.rekey(:a=>'a') #=> { 'a'=>1, :b=>2 }
foo.rekey(:b=>:x) #=> { :a =>1, :x=>2 }
foo.rekey('foo'=>'bar') #=> { :a =>1, :b=>2 }
If a block is given, converts all keys in the Hash according to the given block procedure.
foo = { :name=>'Gavin', :wife=>:Lisa }
foo.rekey{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa }
foo #=> { :name =>"Gavin", :wife=>:Lisa }
If no key map or block is given, then all keys are converted to Symbols.
Raises an ArgumentError if both a key_map and a block are given. If both are needed just call #rekey twice.
TODO: If nil is returned by block should the key be set to nil or the original key?
CREDIT: Trans, Gavin Kistner
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/dm_ruby_extensions/extend_hash.rb', line 71 def rekey(key_map = nil, &block) raise ArgumentError, 'argument or block' if key_map && block unless key_map || block block = lambda { |k| k.to_sym } # rubocop:disable Style/SymbolProc, Style/Lambda end if block hash = dup.clear if block.arity.abs == 1 each_pair do |k, v| hash[block[k]] = v # hash[block[k] || k] = v end else each_pair do |k, v| hash[block[k, v]] = v # hash[block[k,v] || k] = v end end else # hash = dup.clear # to keep default_proc # (keys - key_map.keys).each do |key| # hash[key] = self[key] # end # key_map.each do |from, to| # hash[to] = self[from] if key?(from) # end hash = dup # to keep default_proc key_map.each_pair do |from, to| hash[to] = hash.delete(from) if hash.key?(from) end end hash end |
#rekey!(key_map = nil, &block) ⇒ Object
Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
foo = { :name=>'Gavin', :wife=>:Lisa }
foo.rekey!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa }
foo #=> { "name"=>"Gavin", "wife"=>:Lisa }
CREDIT: Trans, Gavin Kistner
114 115 116 |
# File 'lib/dm_ruby_extensions/extend_hash.rb', line 114 def rekey!(key_map = nil, &block) replace(rekey(key_map, &block)) end |
#url_query_string(leading_slash = true) ⇒ Object
Convert hash of parameters to a query string
rubocop:disable Style/OptionalBooleanParameter
34 35 36 |
# File 'lib/dm_ruby_extensions/extend_hash.rb', line 34 def url_query_string(leading_slash = true) "#{leading_slash ? '/?' : '?'}#{to_query}" end |