Class: Hash

Inherits:
Object show all
Defined in:
lib/overload/hash.rb,
lib/overload/blank.rb

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/overload/hash.rb', line 67

def blank?
  self.keys.count == 0
end

#data_attributesObject



78
79
80
# 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) ⇒ Object

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))


139
140
141
# File 'lib/overload/hash.rb', line 139

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object

Hash#except in place, modifying current hash



144
145
146
147
# File 'lib/overload/hash.rb', line 144

def except!(*keys)
  keys.each { |key| delete(key) }
  self
end

#hObject



6
7
8
# File 'lib/overload/hash.rb', line 6

def h
  Hashie::Mash.new(self)
end

#h_wiaObject



10
11
12
# File 'lib/overload/hash.rb', line 10

def h_wia
  HashWithIndifferentAccess.new(self)
end

#pluck(*args) ⇒ Object



82
83
84
85
# 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_generateObject



112
113
114
# File 'lib/overload/hash.rb', line 112

def pretty_generate
  JSON.pretty_generate(self).gsub(/"([\w\-]+)":/) { %["#{$1.yellow}":] }
end

#slice(*keys) ⇒ Object

Returns hash with only se



117
118
119
120
# 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) ⇒ Object



122
123
124
125
126
127
128
129
130
# 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_keysObject



104
105
106
# File 'lib/overload/hash.rb', line 104

def symbolize_keys
  transform_keys{ |key| key.to_sym rescue key }
end

#symbolize_keys!Object



108
109
110
# File 'lib/overload/hash.rb', line 108

def symbolize_keys!
  transform_keys!{ |key| key.to_sym rescue key }
end

#tag(node = nil, text = nil) ⇒ Object



63
64
65
# File 'lib/overload/hash.rb', line 63

def tag node=nil, text=nil
  HtmlTagBuilder.build self, node, text
end

#to_opts(*keys) ⇒ Object

….to_opts :name, :age ….to_opts name: String, age: Integer

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



71
72
73
74
75
76
# 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) ⇒ Object

readonly hash with .to_h



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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_structObject



59
60
61
# File 'lib/overload/hash.rb', line 59

def to_struct
  to_opts *keys
end

#transform_keysObject



87
88
89
90
91
92
93
94
# 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!Object



96
97
98
99
100
101
102
# 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