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
# File 'lib/userlist/push/resource.rb', line 63

def initialize(payload = {}, config = Userlist.config)
  @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)



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

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



99
100
101
# File 'lib/userlist/push/resource.rb', line 99

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

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/userlist/push/resource.rb', line 94

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

#hashObject



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

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

#identifierObject



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

def identifier
  payload[:identifier]
end

#push?Boolean

Returns:

  • (Boolean)


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

def push?
  true
end

#relationship_namesObject



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

def relationship_names
  self.class.relationship_names.to_a
end

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

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/userlist/push/resource.rb', line 68

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



73
74
75
# File 'lib/userlist/push/resource.rb', line 73

def to_hash
  Serializer.serialize(self)
end

#to_json(*args) ⇒ Object



78
79
80
# File 'lib/userlist/push/resource.rb', line 78

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

#urlObject



82
83
84
# File 'lib/userlist/push/resource.rb', line 82

def url
  "#{self.class.endpoint}/#{identifier}"
end