lib/alba/resource.rb in alba-0.9.0 vs lib/alba/resource.rb in alba-0.10.0
- old
+ new
@@ -61,11 +61,13 @@
when Symbol
resource.public_send attribute
when Proc
instance_exec(resource, &attribute)
when Alba::One, Alba::Many
- attribute.to_hash(resource)
+ attribute.to_hash(resource, params: params)
+ else
+ raise ::Alba::Error, "Unsupported type of attribute: #{attribute.class}"
end
end
end
end
@@ -88,25 +90,25 @@
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] = attr_name }
+ attrs.each { |attr_name| @_attributes[attr_name.to_sym] = attr_name.to_sym }
end
def attribute(name, &block)
raise ArgumentError, 'No block given in attribute method' unless block
- @_attributes[name] = block
+ @_attributes[name.to_sym] = block
end
- def one(name, resource: nil, &block)
- @_attributes[name.to_sym] = One.new(name: name, resource: resource, &block)
+ def one(name, condition = nil, resource: nil, key: nil, &block)
+ @_attributes[key&.to_sym || name.to_sym] = One.new(name: name, condition: condition, resource: resource, &block)
end
- def many(name, resource: nil, &block)
- @_attributes[name.to_sym] = Many.new(name: name, resource: resource, &block)
+ def many(name, condition = nil, resource: nil, key: nil, &block)
+ @_attributes[key&.to_sym || name.to_sym] = Many.new(name: name, condition: condition, resource: resource, &block)
end
def serializer(name)
@_serializer = name <= Alba::Serializer ? name : nil
end
@@ -116,10 +118,10 @@
end
# Use this DSL in child class to ignore certain attributes
def ignoring(*attributes)
attributes.each do |attr_name|
- @_attributes.delete(attr_name)
+ @_attributes.delete(attr_name.to_sym)
end
end
end
end
end