Class: Userlist::Push::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/userlist/push/resource.rb

Direct Known Subclasses

Company, Event, Message, Relationship, User

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



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

#configObject (readonly)

Returns the value of attribute config.



61
62
63
# File 'lib/userlist/push/resource.rb', line 61

def config
  @config
end

#contextObject (readonly)

Returns the value of attribute context.



61
62
63
# File 'lib/userlist/push/resource.rb', line 61

def context
  @context
end

#payloadObject (readonly)

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_namesObject



22
23
24
# File 'lib/userlist/push/resource.rb', line 22

def association_names
  @association_names ||= associations.keys
end

.associationsObject



26
27
28
# File 'lib/userlist/push/resource.rb', line 26

def associations
  @associations ||= {}
end

.endpointObject



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_nameObject



5
6
7
# File 'lib/userlist/push/resource.rb', line 5

def resource_name
  name.split('::')[-1]
end

Instance Method Details

#association_namesObject



102
103
104
# File 'lib/userlist/push/resource.rb', line 102

def association_names
  self.class.association_names.to_a
end

#attribute_namesObject



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: ==

Returns:

  • (Boolean)


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

#hashObject



89
90
91
# File 'lib/userlist/push/resource.rb', line 89

def hash
  self.class.hash & payload.hash
end

#identifierObject



85
86
87
# File 'lib/userlist/push/resource.rb', line 85

def identifier
  payload[:identifier]
end

#push?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/userlist/push/resource.rb', line 106

def push?
  true
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (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_hashObject 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