lib/alba/resource.rb in alba-0.2.0 vs lib/alba/resource.rb in alba-0.3.0

- old
+ new

@@ -5,11 +5,11 @@ require 'alba/serializers/default_serializer' module Alba # This module represents what should be serialized module Resource - DSLS = [:_attributes, :_one, :_many, :_serializer].freeze + DSLS = [:_attributes, :_serializer].freeze def self.included(base) base.class_eval do # Initialize DSLS.each do |name| initial = name == :_serializer ? nil : {} @@ -27,46 +27,28 @@ DSLS.each { |name| instance_variable_set("@#{name}", self.class.public_send(name)) } end def serialize(with: nil) serializer = case with + when nil + @_serializer || Alba::Serializers::DefaultSerializer when ->(obj) { obj.is_a?(Class) && obj <= Alba::Serializer } with when Symbol const_get(with.to_s.capitalize) when String const_get(with) - when nil - @_serializer || Alba::Serializers::DefaultSerializer end serializer.new(serializable_hash).serialize end def serializable_hash - attrs.merge(ones).merge(manies) - end - alias to_hash serializable_hash - - private - - def attrs @_attributes.transform_values do |attribute| attribute.to_hash(@_resource) - end || {} + end end - - def ones - @_one.transform_values do |one| - one.to_hash(@_resource) - end || {} - end - - def manies - @_many.transform_values do |many| - many.to_hash(@_resource) - end || {} - end + alias to_hash serializable_hash end # Class methods module ClassMethods attr_accessor(*DSLS) @@ -84,14 +66,14 @@ @_attributes[name] = Attribute.new(name: name, method: block) end def one(name, resource: nil, &block) - @_one[name.to_sym] = One.new(name: name, resource: resource, &block) + @_attributes[name.to_sym] = One.new(name: name, resource: resource, &block) end def many(name, resource: nil, &block) - @_many[name.to_sym] = Many.new(name: name, resource: resource, &block) + @_attributes[name.to_sym] = Many.new(name: name, resource: resource, &block) end def serializer(name) @_serializer = name <= Alba::Serializer ? name : nil end