Sha256: e95142dc647edae11b29485f38e1bd4991b7ba1d639cf11c4a8c3434a3e51e78

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

require 'rest_in_peace/definition_proxy'

module RESTinPeace

  def self.included(base)
    base.send :extend, ClassMethods
  end

  def api
    self.class.api
  end

  def initialize(attributes = {})
    force_attributes_from_hash(attributes)
  end

  def hash_for_updates
    hash_representation = { id: id }
    self.class.rip_attributes[:write].map do |key|
      value = send(key)
      hash_representation[key] = hash_representation_of_object(value)
    end
    if self.class.rip_namespace
      { id: id, self.class.rip_namespace => hash_representation }
    else
      hash_representation
    end
  end

  def update_attributes(attributes)
    attributes.each do |key, value|
      next unless respond_to?("#{key}=")
      send("#{key}=", value)
    end
  end

  def to_h
    hash_representation = {}
    self.class.rip_attributes.values.flatten.each do |attr|
      hash_representation[attr] = send(attr)
    end
    hash_representation
  end

  def force_attributes_from_hash(attributes)
    attributes.each do |key, value|
      next unless respond_to?(key)
      if respond_to?("#{key}=")
        send("#{key}=", value)
      else
        instance_variable_set("@#{key}", value)
      end
    end
  end

  def hash_representation_of_object(object)
    return object.hash_for_updates if object.respond_to?(:hash_for_updates)
    return object.map { |element| hash_representation_of_object(element) } if object.is_a?(Array)
    object
  end

  module ClassMethods
    attr_accessor :api
    attr_accessor :rip_namespace

    def rest_in_peace(&block)
      definition_proxy = RESTinPeace::DefinitionProxy.new(self)
      definition_proxy.instance_eval(&block)
    end

    def rip_registry
      @rip_registry ||= {
        resource: [],
        collection: [],
      }
    end

    def rip_attributes
      @rip_attributes ||= {
        read: [],
        write: [],
      }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rest-in-peace-2.0.4 lib/rest_in_peace.rb
rest-in-peace-2.0.3 lib/rest_in_peace.rb
rest-in-peace-2.0.2 lib/rest_in_peace.rb
rest-in-peace-2.0.1 lib/rest_in_peace.rb
rest-in-peace-2.0.0 lib/rest_in_peace.rb