Module: HasHelpers::CoreExt::Hash

Defined in:
lib/has_helpers/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#camelize_keysObject



44
45
46
# File 'lib/has_helpers/core_ext/hash.rb', line 44

def camelize_keys
  clone.camelize_keys!
end

#camelize_keys!Object

Camelize the first level keys of a hash.

Example

h = Hash.new h = "Brendan"

h.camelize_keys! # => { firstName: "Brendan" }



35
36
37
38
39
40
41
42
# File 'lib/has_helpers/core_ext/hash.rb', line 35

def camelize_keys!
  keys.each do |key|
    camelized_key = key.to_s.camelize(:lower)
    camelized_key = camelized_key.to_sym if key.is_a? Symbol
    self[camelized_key] = self.delete(key)
  end
  self
end

#to_actionObject



48
49
50
# File 'lib/has_helpers/core_ext/hash.rb', line 48

def to_action
  ::Action.new(self)
end

#to_html_attrsObject



60
61
62
# File 'lib/has_helpers/core_ext/hash.rb', line 60

def to_html_attrs
  ::HasHelpers::UIAttributes::HTMLAttributes::HTMLAttrs.new(self)
end

#to_iconObject



52
53
54
# File 'lib/has_helpers/core_ext/hash.rb', line 52

def to_icon
  ::HasHelpers::Icon.new(self[:name], **self.except(:name))
end

#to_inputObject



56
57
58
# File 'lib/has_helpers/core_ext/hash.rb', line 56

def to_input
  ::Input.new(self)
end

#to_renderableObject



64
65
66
# File 'lib/has_helpers/core_ext/hash.rb', line 64

def to_renderable
  ::RenderObject.new(self)
end

#with_nested_accessObject



24
25
26
# File 'lib/has_helpers/core_ext/hash.rb', line 24

def with_nested_access
  clone.with_nested_access!
end

#with_nested_access!Object

Allow any level of access by providing a default value at each level.

Examples

Hash without nested access (or any given defaults)

h = Hash.new h # => nil h[:c] # => NoMethodError: undefined method `[]' for nil:NilClass

h.with_nested_access!
h[:b]          # => {}
h[:b][:c]      # => {}
h[:b][:c][:d]  # => {}


19
20
21
22
# File 'lib/has_helpers/core_ext/hash.rb', line 19

def with_nested_access!
  self.default_proc = -> h, key { h[key] = ::Hash.new(&h.default_proc) }
  self
end