Class: Userlist::Push::Resource
- Inherits:
-
Object
- Object
- Userlist::Push::Resource
show all
- Defined in:
- lib/userlist/push/resource.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(payload = {}, config = Userlist.config) ⇒ Resource
Returns a new instance of Resource.
63
64
65
66
67
68
69
|
# File 'lib/userlist/push/resource.rb', line 63
def initialize(payload = {}, config = Userlist.config)
raise Userlist::ArgumentError, 'Missing required payload' unless payload
@payload = payload
@config = config
@context = :push
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/userlist/push/resource.rb', line 118
def method_missing(method, *args, &block)
if method.to_s =~ /=$/
attribute = method.to_s.sub(/=$/, '')
payload[attribute.to_sym] = args.first
elsif payload.key?(method.to_sym)
payload[method.to_sym]
else
super
end
end
|
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
61
62
63
|
# File 'lib/userlist/push/resource.rb', line 61
def config
@config
end
|
#context ⇒ Object
Returns the value of attribute context.
61
62
63
|
# File 'lib/userlist/push/resource.rb', line 61
def context
@context
end
|
#payload ⇒ Object
Returns the value of attribute payload.
61
62
63
|
# File 'lib/userlist/push/resource.rb', line 61
def payload
@payload
end
|
Class Method Details
.association_names ⇒ Object
22
23
24
|
# File 'lib/userlist/push/resource.rb', line 22
def association_names
@association_names ||= associations.keys
end
|
.associations ⇒ Object
26
27
28
|
# File 'lib/userlist/push/resource.rb', line 26
def associations
@associations ||= {}
end
|
.endpoint ⇒ Object
9
10
11
|
# File 'lib/userlist/push/resource.rb', line 9
def endpoint
"/#{resource_name.downcase}s"
end
|
.from_payload(payload, config = Userlist.config) ⇒ Object
13
14
15
16
17
18
19
20
|
# File 'lib/userlist/push/resource.rb', line 13
def from_payload(payload, config = Userlist.config)
return payload if payload.nil?
return payload if payload.is_a?(self)
payload = { identifier: payload } if payload.is_a?(String) || payload.is_a?(Numeric)
new(payload, config)
end
|
.resource_name ⇒ Object
5
6
7
|
# File 'lib/userlist/push/resource.rb', line 5
def resource_name
name.split('::')[-1]
end
|
Instance Method Details
#association_names ⇒ Object
102
103
104
|
# File 'lib/userlist/push/resource.rb', line 102
def association_names
self.class.association_names.to_a
end
|
#attribute_names ⇒ Object
98
99
100
|
# File 'lib/userlist/push/resource.rb', line 98
def attribute_names
payload.keys.map(&:to_sym) - association_names
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
93
94
95
|
# File 'lib/userlist/push/resource.rb', line 93
def eql?(other)
hash == other.hash
end
|
#for_context(context) ⇒ Object
110
111
112
113
114
|
# File 'lib/userlist/push/resource.rb', line 110
def for_context(context)
dup.tap do |instance|
instance.instance_variable_set(:@context, context)
end
end
|
#hash ⇒ Object
89
90
91
|
# File 'lib/userlist/push/resource.rb', line 89
def hash
self.class.hash & payload.hash
end
|
#identifier ⇒ Object
85
86
87
|
# File 'lib/userlist/push/resource.rb', line 85
def identifier
payload[:identifier]
end
|
#push? ⇒ Boolean
106
107
108
|
# File 'lib/userlist/push/resource.rb', line 106
def push?
true
end
|
#respond_to_missing?(method, include_private = false) ⇒ Boolean
71
72
73
74
|
# File 'lib/userlist/push/resource.rb', line 71
def respond_to_missing?(method, include_private = false)
attribute = method.to_s.sub(/=$/, '')
payload.key?(attribute.to_sym) || super
end
|
#to_hash ⇒ Object
Also known as:
to_h
76
77
78
|
# File 'lib/userlist/push/resource.rb', line 76
def to_hash
Serializer.serialize(self, context: context)
end
|
#to_json(*args) ⇒ Object
81
82
83
|
# File 'lib/userlist/push/resource.rb', line 81
def to_json(*args)
to_hash.to_json(*args)
end
|