lib/alba/conditional_attribute.rb in alba-2.4.1 vs lib/alba/conditional_attribute.rb in alba-2.4.2
- old
+ new
@@ -1,7 +1,8 @@
require_relative 'association'
require_relative 'constants'
+require 'ostruct'
module Alba
# Represents attribute with `if` option
# @api private
class ConditionalAttribute
@@ -21,11 +22,11 @@
return Alba::REMOVE_KEY unless condition_passes?(resource, object)
fetched_attribute = yield(@body)
return fetched_attribute unless with_two_arity_proc_condition
- return Alba::REMOVE_KEY unless resource.instance_exec(object, attribute_from_association_body_or(fetched_attribute), &@condition)
+ return Alba::REMOVE_KEY unless resource.instance_exec(object, objectize(fetched_attribute), &@condition)
fetched_attribute
end
private
@@ -46,10 +47,20 @@
def with_two_arity_proc_condition
@condition.is_a?(Proc) && @condition.arity >= 2
end
- def attribute_from_association_body_or(fetched_attribute)
- @body.is_a?(Alba::Association) ? @body.object : fetched_attribute
+ # OpenStruct is used as a simple solution for converting Hash or Array of Hash into an object
+ # Using OpenStruct is not good in general, but in this case there's no other solution
+ def objectize(fetched_attribute)
+ return fetched_attribute unless @body.is_a?(Alba::Association)
+
+ if fetched_attribute.is_a?(Array)
+ fetched_attribute.map do |hash|
+ OpenStruct.new(hash)
+ end
+ else
+ OpenStruct.new(fetched_attribute)
+ end
end
end
end