lib/alba/resource.rb in alba-0.8.0 vs lib/alba/resource.rb in alba-0.9.0
- old
+ new
@@ -1,39 +1,34 @@
require 'alba/serializer'
-require 'alba/attribute'
require 'alba/one'
require 'alba/many'
require 'alba/serializers/default_serializer'
module Alba
# This module represents what should be serialized
module Resource
- DSLS = [:_attributes, :_serializer, :_key].freeze
+ DSLS = {_attributes: {}, _serializer: nil, _key: nil}.freeze
def self.included(base)
+ super
base.class_eval do
# Initialize
- DSLS.each do |name|
- initial = case name
- when :_attributes
- {}
- when :_serializer, :_name
- nil
- end
- instance_variable_set("@#{name}", initial) unless instance_variable_defined?("@#{name}")
+ DSLS.each do |name, initial|
+ instance_variable_set("@#{name}", initial.dup) unless instance_variable_defined?("@#{name}")
end
end
base.include InstanceMethods
base.extend ClassMethods
end
# Instance methods
module InstanceMethods
- attr_reader :_object
+ attr_reader :object, :_key, :params
- def initialize(object)
- @_object = object
- DSLS.each { |name| instance_variable_set("@#{name}", self.class.public_send(name)) }
+ def initialize(object, params: {})
+ @object = object
+ @params = params
+ DSLS.each_key { |name| instance_variable_set("@#{name}", self.class.public_send(name)) }
end
def serialize(with: nil)
serializer = case with
when nil
@@ -46,58 +41,64 @@
raise ArgumentError, 'Unexpected type for with, possible types are Class or Proc'
end
serializer.new(self).serialize
end
- def serializable_hash(with_key: true)
- get_attribute = lambda do |resource|
- @_attributes.transform_values do |attribute|
- attribute.to_hash(resource)
- end
- end
- serializable_hash = if collection?
- @_object.map(&get_attribute)
- else
- get_attribute.call(@_object)
- end
- with_key && @_key ? {@_key => serializable_hash} : serializable_hash
+ def serializable_hash
+ collection? ? @object.map(&converter) : converter.call(@object)
end
alias to_hash serializable_hash
def key
@_key || self.class.name.delete_suffix('Resource').downcase.gsub(/:{2}/, '_').to_sym
end
private
+ def converter
+ lambda do |resource|
+ @_attributes.transform_values do |attribute|
+ case attribute
+ when Symbol
+ resource.public_send attribute
+ when Proc
+ instance_exec(resource, &attribute)
+ when Alba::One, Alba::Many
+ attribute.to_hash(resource)
+ end
+ end
+ end
+ end
+
def inline_extended_serializer(with)
klass = ::Alba::Serializers::DefaultSerializer.clone
klass.class_eval(&with)
klass
end
def collection?
- @_object.is_a?(Enumerable)
+ @object.is_a?(Enumerable)
end
end
# Class methods
module ClassMethods
- attr_reader(*DSLS)
+ attr_reader(*DSLS.keys)
def inherited(subclass)
- DSLS.each { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}")) }
+ super
+ DSLS.each_key { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}").clone) }
end
def attributes(*attrs)
- attrs.each { |attr_name| @_attributes[attr_name] = Attribute.new(name: attr_name, method: attr_name) }
+ attrs.each { |attr_name| @_attributes[attr_name] = attr_name }
end
def attribute(name, &block)
raise ArgumentError, 'No block given in attribute method' unless block
- @_attributes[name] = Attribute.new(name: name, method: block)
+ @_attributes[name] = block
end
def one(name, resource: nil, &block)
@_attributes[name.to_sym] = One.new(name: name, resource: resource, &block)
end
@@ -110,9 +111,16 @@
@_serializer = name <= Alba::Serializer ? name : nil
end
def key(key)
@_key = key.to_sym
+ end
+
+ # Use this DSL in child class to ignore certain attributes
+ def ignoring(*attributes)
+ attributes.each do |attr_name|
+ @_attributes.delete(attr_name)
+ end
end
end
end
end