Class: Dish::Plate
- Inherits:
-
Object
show all
- Defined in:
- lib/dish/plate.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash) ⇒ Plate
Returns a new instance of Plate.
13
14
15
16
|
# File 'lib/dish/plate.rb', line 13
def initialize(hash)
@_original_hash = Hash[hash.map { |k, v| [k.to_s, v] }]
@_value_cache = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/dish/plate.rb', line 29
def method_missing(method, *args, &block)
method = method.to_s
key = method[0..-2]
if method.end_with?("?")
!!_get_value(key)
elsif method.end_with? '='
_set_value(key, args.first)
else
_get_value(method)
end
end
|
Class Method Details
.coerce(key, klass_or_proc) ⇒ Object
8
9
10
|
# File 'lib/dish/plate.rb', line 8
def coerce(key, klass_or_proc)
coercions[key.to_s] = klass_or_proc
end
|
.coercions ⇒ Object
4
5
6
|
# File 'lib/dish/plate.rb', line 4
def coercions
@coercions ||= Hash.new(Plate)
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
22
23
24
25
|
# File 'lib/dish/plate.rb', line 22
def ==(other)
return false unless other.respond_to?(:to_h)
to_h == other.to_h
end
|
#as_hash ⇒ Object
49
50
51
52
53
|
# File 'lib/dish/plate.rb', line 49
def as_hash
warn 'Dish::Plate#as_hash has been deprecated. Use Dish::Plate#to_h.'
to_h
end
|
#hash ⇒ Object
18
19
20
|
# File 'lib/dish/plate.rb', line 18
def hash
to_h.hash
end
|
#methods(regular = true) ⇒ Object
67
68
69
70
|
# File 'lib/dish/plate.rb', line 67
def methods(regular = true)
valid_keys = to_h.keys.map(&:to_sym)
valid_keys + super
end
|
#respond_to_missing?(method, *args) ⇒ Boolean
41
42
43
|
# File 'lib/dish/plate.rb', line 41
def respond_to_missing?(method, *args)
_check_for_presence(method.to_s) || super
end
|
#to_h ⇒ Object
45
46
47
|
# File 'lib/dish/plate.rb', line 45
def to_h
@_original_hash
end
|
#to_json(*args) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/dish/plate.rb', line 55
def to_json(*args)
if defined?(Motion::Project::Config)
NSJSONSerialization.dataWithJSONObject(to_h, options: 0, error: nil).to_str
elsif defined?(JSON)
to_h.to_json(*args)
else
raise "#{self.class}#to_json depends on Hash#to_json. Try again after using `require 'json'`."
end
end
|