lib/alba/resource.rb in alba-0.7.0 vs lib/alba/resource.rb in alba-0.8.0

- old
+ new

@@ -25,12 +25,14 @@ base.extend ClassMethods end # Instance methods module InstanceMethods - def initialize(resource) - @_resource = resource + attr_reader :_object + + def initialize(object) + @_object = object DSLS.each { |name| instance_variable_set("@#{name}", self.class.public_send(name)) } end def serialize(with: nil) serializer = case with @@ -51,13 +53,13 @@ @_attributes.transform_values do |attribute| attribute.to_hash(resource) end end serializable_hash = if collection? - @_resource.map(&get_attribute) + @_object.map(&get_attribute) else - get_attribute.call(@_resource) + get_attribute.call(@_object) end with_key && @_key ? {@_key => serializable_hash} : serializable_hash end alias to_hash serializable_hash @@ -72,19 +74,19 @@ klass.class_eval(&with) klass end def collection? - @_resource.is_a?(Enumerable) + @_object.is_a?(Enumerable) end end # Class methods module ClassMethods - attr_accessor(*DSLS) + attr_reader(*DSLS) def inherited(subclass) - DSLS.each { |name| subclass.public_send("#{name}=", instance_variable_get("@#{name}")) } + DSLS.each { |name| subclass.instance_variable_set("@#{name}", instance_variable_get("@#{name}")) } end def attributes(*attrs) attrs.each { |attr_name| @_attributes[attr_name] = Attribute.new(name: attr_name, method: attr_name) } end