class Userlist::Push::Resource

Attributes

config[R]
payload[R]

Public Class Methods

endpoint() click to toggle source
# File lib/userlist/push/resource.rb, line 9
def endpoint
  "/#{resource_name.downcase}s"
end
from_payload(payload, config = Userlist.config) click to toggle source
# 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
new(payload = {}, config = Userlist.config) click to toggle source
# File lib/userlist/push/resource.rb, line 63
def initialize(payload = {}, config = Userlist.config)
  @payload = payload
  @config = config
end
relationship_names() click to toggle source
# File lib/userlist/push/resource.rb, line 22
def relationship_names
  @relationship_names ||= relationships.keys
end
relationships() click to toggle source
# File lib/userlist/push/resource.rb, line 26
def relationships
  @relationships ||= {}
end
resource_name() click to toggle source
# File lib/userlist/push/resource.rb, line 5
def resource_name
  name.split('::')[-1]
end

Protected Class Methods

has_many(name, **options) click to toggle source
# File lib/userlist/push/resource.rb, line 42
        def has_many(name, **options) # rubocop:disable Naming/PredicateName
          relationships[name.to_sym] = options

          generated_methods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{name}
              relationship = self.class.relationships[:#{name}]

              ResourceCollection.new(payload[:#{name}], relationship, self, config)
            end
          RUBY
        end
has_one(name, type:) click to toggle source
# File lib/userlist/push/resource.rb, line 32
        def has_one(name, type:) # rubocop:disable Naming/PredicateName
          relationships[name.to_sym] = { type: type }

          generated_methods.class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def #{name}
              #{type}.from_payload(payload[:#{name}], config)
            end
          RUBY
        end

Private Class Methods

generated_methods() click to toggle source
# File lib/userlist/push/resource.rb, line 56
def generated_methods
  @generated_methods ||= Module.new.tap { |mod| include mod }
end

Public Instance Methods

==(other)
Alias for: eql?
attribute_names() click to toggle source
# File lib/userlist/push/resource.rb, line 99
def attribute_names
  payload.keys.map(&:to_sym) - relationship_names
end
eql?(other) click to toggle source
# File lib/userlist/push/resource.rb, line 94
def eql?(other)
  hash == other.hash
end
Also aliased as: ==
hash() click to toggle source
# File lib/userlist/push/resource.rb, line 90
def hash
  self.class.hash & payload.hash
end
identifier() click to toggle source
# File lib/userlist/push/resource.rb, line 86
def identifier
  payload[:identifier]
end
push?() click to toggle source
# File lib/userlist/push/resource.rb, line 107
def push?
  true
end
relationship_names() click to toggle source
# 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) click to toggle source
Calls superclass method
# 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_h()
Alias for: to_hash
to_hash() click to toggle source
# File lib/userlist/push/resource.rb, line 73
def to_hash
  Serializer.serialize(self)
end
Also aliased as: to_h
to_json(*args) click to toggle source
# File lib/userlist/push/resource.rb, line 78
def to_json(*args)
  to_hash.to_json(*args)
end
url() click to toggle source
# File lib/userlist/push/resource.rb, line 82
def url
  "#{self.class.endpoint}/#{identifier}"
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# 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