class Hash
Public Instance Methods
blank?()
click to toggle source
# File lib/overload/blank.rb, line 43 def blank? self.keys.length == 0 end
data_attributes()
click to toggle source
# File lib/overload/hash.rb, line 78 def data_attributes self.keys.sort.map{ |k| 'data-%s="%s"' % [k, self[k].to_s.gsub('"', '"')]}.join(' ') end
except(*keys)
click to toggle source
Returns a hash that includes everything but the given keys.
hash = { a: true, b: false, c: nil} hash.except(:c) # => { a: true, b: false} hash # => { a: true, b: false, c: nil}
This is useful for limiting a set of parameters to everything but a few known toggles:
@person.update(params[:person].except(:admin))
# File lib/overload/hash.rb, line 139 def except(*keys) dup.except!(*keys) end
except!(*keys)
click to toggle source
Hash#except
in place, modifying current hash
# File lib/overload/hash.rb, line 144 def except!(*keys) keys.each { |key| delete(key) } self end
h()
click to toggle source
# File lib/overload/hash.rb, line 6 def h Hashie::Mash.new(self) end
h_wia()
click to toggle source
# File lib/overload/hash.rb, line 10 def h_wia HashWithIndifferentAccess.new(self) end
pluck(*args)
click to toggle source
# File lib/overload/hash.rb, line 82 def pluck *args string_args = args.map(&:to_s) self.select{ |k,v| string_args.index(k.to_s) } end
pretty_generate()
click to toggle source
# File lib/overload/hash.rb, line 112 def pretty_generate JSON.pretty_generate(self).gsub(/"([\w\-]+)":/) { %["#{$1.yellow}":] } end
slice(*keys)
click to toggle source
Returns hash with only se
# File lib/overload/hash.rb, line 117 def slice *keys keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) } end
slice!(*keys)
click to toggle source
# File lib/overload/hash.rb, line 122 def slice! *keys keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true) omit = slice(*self.keys - keys) hash = slice(*keys) hash.default = default hash.default_proc = default_proc if default_proc replace(hash) omit end
symbolize_keys()
click to toggle source
# File lib/overload/hash.rb, line 104 def symbolize_keys transform_keys{ |key| key.to_sym rescue key } end
symbolize_keys!()
click to toggle source
# File lib/overload/hash.rb, line 108 def symbolize_keys! transform_keys!{ |key| key.to_sym rescue key } end
tag(node=nil, text=nil)
click to toggle source
# File lib/overload/hash.rb, line 63 def tag node=nil, text=nil HtmlTagBuilder.build self, node, text end
to_opts(*keys)
click to toggle source
{…}.to_opts :name, :age {…}.to_opts name: String
, age: Integer
# File lib/overload/hash.rb, line 35 def to_opts *keys if keys.first.is_a?(Hash) # if given Hash check opt class types keys.first.each do |key, target_class| source = self[key] if target_class && !source.nil? && !source.is_a?(target_class) raise ArgumentError.new(%[Expected argument :#{key} to be of type "#{target_class}" not "#{source.class}"]) end end keys = keys.first.keys end not_allowed = self.keys - keys raise ArgumentError.new('Key :%s not allowed in option' % not_allowed.first) if not_allowed.first FreeStruct.new keys.inject({}) { |total, key| raise 'Hash key :%s is not allowed!' % key unless keys.include?(key) total[key] = self[key] total } end
to_query(namespace=nil)
click to toggle source
# File lib/overload/hash.rb, line 71 def to_query namespace=nil self.keys.sort.map { |k| name = namespace ? "#{namespace}[#{k}]" : k "#{name}=#{CGI::escape(self[k].to_s)}" }.join('&') end
to_readonly(name=nil)
click to toggle source
readonly hash with .to_h
# File lib/overload/hash.rb, line 15 def to_readonly name=nil Class.new.tap do |c| c.define_singleton_method(:to_h) do m_list = methods(false) - [:to_h] m_list.inject({}) do |h,m| h[m] = send(m) h[m] = h[m].to_h if h[m].class == Class h end end each do |k, v| v = v.to_readonly if v.class == Hash c.define_singleton_method(k) { v } end end end
to_struct()
click to toggle source
# File lib/overload/hash.rb, line 59 def to_struct to_opts *keys end
transform_keys() { |key| ... }
click to toggle source
# File lib/overload/hash.rb, line 87 def transform_keys return enum_for(:transform_keys) unless block_given? result = self.class.new each_key do |key| result[yield(key)] = self[key] end result end
transform_keys!() { |key| ... }
click to toggle source
# File lib/overload/hash.rb, line 96 def transform_keys! return enum_for(:transform_keys!) unless block_given? keys.each do |key| self[yield(key)] = delete(key) end self end