Class: Userlist::Push::Resource

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

Direct Known Subclasses

Company, Event, 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
# 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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



111
112
113
114
115
116
117
118
119
120
# File 'lib/userlist/push/resource.rb', line 111

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

#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

.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

.relationship_namesObject



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

def relationship_names
  @relationship_names ||= relationships.keys
end

.relationshipsObject



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

def relationships
  @relationships ||= {}
end

.resource_nameObject



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

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

Instance Method Details

#attribute_namesObject



97
98
99
# File 'lib/userlist/push/resource.rb', line 97

def attribute_names
  payload.keys.map(&:to_sym) - relationship_names
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


92
93
94
# File 'lib/userlist/push/resource.rb', line 92

def eql?(other)
  hash == other.hash
end

#hashObject



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

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

#identifierObject



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

def identifier
  payload[:identifier]
end

#push?Boolean

Returns:

  • (Boolean)


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

def push?
  true
end

#relationship_namesObject



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

def relationship_names
  self.class.relationship_names.to_a
end

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

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/userlist/push/resource.rb', line 70

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



75
76
77
# File 'lib/userlist/push/resource.rb', line 75

def to_hash
  Serializer.serialize(self)
end

#to_json(*args) ⇒ Object



80
81
82
# File 'lib/userlist/push/resource.rb', line 80

def to_json(*args)
  to_hash.to_json(*args)
end