module Spyke::AttributeAssignment

Public Class Methods

new(attributes = {}) { |self| ... } click to toggle source
# File lib/spyke/attribute_assignment.rb, line 36
def initialize(attributes = {})
  self.attributes = attributes
  @uri_template = scope.uri
  yield self if block_given?
end

Public Instance Methods

==(other) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 67
def ==(other)
  other.instance_of?(self.class) && id? && id == other.id
end
Also aliased as: eql?
as_json(options = nil) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 76
def as_json(options = nil)
  attributes.as_json(options)
end
attributes() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 42
def attributes
  @spyke_attributes
end
attributes=(new_attributes) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 46
def attributes=(new_attributes)
  @spyke_attributes ||= Attributes.new(scope.params)
  use_setters(new_attributes) if new_attributes
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 63
def hash
  id.hash
end
id() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 55
def id
  attributes[primary_key]
end
id=(value) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 59
def id=(value)
  attributes[primary_key] = value if value.present?
end
id?() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 51
def id?
  id.present?
end
inspect() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 72
def inspect
  "#<#{self.class}(#{@uri_template}) id: #{id.inspect} #{inspect_attributes}>"
end

Private Instance Methods

association(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 113
def association(name)
  associations[name].build(self)
end
association?(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 109
def association?(name)
  associations.has_key?(name)
end
attribute(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 121
def attribute(name)
  attributes[name]
end
attribute?(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 117
def attribute?(name)
  attributes.has_key?(name)
end
conflicting_ids?(attributes) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 91
def conflicting_ids?(attributes)
  primary_key != :id && attributes.key?(:id) && attributes.key?(primary_key)
end
depredicate(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 133
def depredicate(name)
  name.to_s.chomp('?').to_sym
end
inspect_attributes() click to toggle source
# File lib/spyke/attribute_assignment.rb, line 145
def inspect_attributes
  attributes.except(primary_key).map { |k, v| "#{k}: #{v.inspect}" }.join(' ')
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/spyke/attribute_assignment.rb, line 95
def method_missing(name, *args, &block)
  case
  when association?(name) then association(name).load
  when attribute?(name)   then attribute(name)
  when predicate?(name)   then predicate(name)
  when setter?(name)      then set_attribute(name, args.first)
  else super
  end
end
predicate(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 129
def predicate(name)
  !!attribute(depredicate(name))
end
predicate?(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 125
def predicate?(name)
  name.to_s.end_with?('?')
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/spyke/attribute_assignment.rb, line 105
def respond_to_missing?(name, include_private = false)
  association?(name) || attribute?(name) || predicate?(name) || super
end
set_attribute(name, value) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 141
def set_attribute(name, value)
  attributes[name.to_s.chomp('=')] = value
end
setter?(name) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 137
def setter?(name)
  name.to_s.end_with?('=')
end
use_setters(attributes) click to toggle source
# File lib/spyke/attribute_assignment.rb, line 82
def use_setters(attributes)
  # Set id attribute directly if using a custom primary key alongside an attribute named "id" to avoid clobbering
  set_attribute :id, attributes.delete(:id) if conflicting_ids?(attributes)

  attributes.to_h.each do |key, value|
    send "#{key}=", value
  end
end