Class: YiffSpace::Utils::OpenHash
- Inherits:
-
ActiveSupport::HashWithIndifferentAccess
- Object
- ActiveSupport::HashWithIndifferentAccess
- YiffSpace::Utils::OpenHash
show all
- Defined in:
- lib/yiffspace/utils/open_hash.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(constructor = nil, respond_to_missing: true) ⇒ OpenHash
Returns a new instance of OpenHash.
8
9
10
11
|
# File 'lib/yiffspace/utils/open_hash.rb', line 8
def initialize(constructor = nil, respond_to_missing: true)
super(constructor)
@respond_to_missing = respond_to_missing
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/yiffspace/utils/open_hash.rb', line 20
def method_missing(name, *args)
if name.end_with?("=")
public_send(:[]=, name.to_s.chomp("=").to_sym, args.first)
elsif key?(name)
public_send(:[], name)
elsif @respond_to_missing
nil
else
super
end
end
|
Class Method Details
.from(hash = nil, recursive: true, respond_to_missing: true, **kwargs) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/yiffspace/utils/open_hash.rb', line 32
def self.from(hash = nil, recursive: true, respond_to_missing: true, **kwargs)
hash = kwargs if hash.nil? && kwargs.any?
raise(ArgumentError, "no hash provided") if hash.nil?
oh = OpenHash.new(respond_to_missing: respond_to_missing)
hash.each do |key, value|
if recursive && value.is_a?(Hash)
oh[key] = OpenHash.from(value, recursive: recursive)
elsif recursive && value.is_a?(Array)
oh[key] = value.map { |v| v.is_a?(Hash) ? OpenHash.from(v, recursive: recursive, respond_to_missing: respond_to_missing) : v }
else
oh[key] = value
end
end
oh
end
|
.from_array(items) ⇒ Object
49
50
51
|
# File 'lib/yiffspace/utils/open_hash.rb', line 49
def self.from_array(items, **)
items.map { |item| from(item, **) }
end
|
Instance Method Details
#respond_to_missing?(name, include_private = false) ⇒ Boolean
13
14
15
16
17
18
|
# File 'lib/yiffspace/utils/open_hash.rb', line 13
def respond_to_missing?(name, include_private = false)
return true if @respond_to_missing
name = name.to_s
key?(name) || name.end_with?("=") || super
end
|