lib/pluck_map/attribute.rb in pluck_map-0.6.2 vs lib/pluck_map/attribute.rb in pluck_map-1.0.0.rc1
- old
+ new
@@ -12,49 +12,62 @@
if options.key? :value
@value = options[:value]
@selects = []
else
- raise ArgumentError, "You must select at least one column" if selects.empty?
+ raise ArgumentError, "You must select at least one column" if @selects.empty?
raise ArgumentError, "You must define a block if you are going to select " <<
- "more than one expression from the database" if selects.length > 1 && !block
+ "more than one expression from the database" if @selects.length > 1 && !@block
- @selects = @selects.map do |select|
+ @selects.each do |select|
if select.is_a?(String) && !select.is_a?(Arel::Nodes::SqlLiteral)
- puts "DEPRECATION WARNING: Passing raw SQL as a String to :select is deprecated. Known-safe values can be passed by wrapping them in Arel.sql()."
- Arel.sql(select)
- else
- select
+ raise ArgumentError, "#{select.inspect} is not a valid value for :select. " <<
+ "If a string of raw SQL is safe, wrap it in Arel.sql()."
end
end
end
end
def apply(object)
block.call(*object)
end
+ def value?
+ defined?(@value)
+ end
+
def will_map?
!block.nil?
end
+ def nested?
+ false
+ end
+
+ def preload!(results)
+ end
+
# When the PluckMapPresenter performs the query, it will
# receive an array of rows. Each row will itself be an
# array of values.
#
# This method constructs a Ruby expression that will
# extract the appropriate values from each row that
# correspond to this Attribute.
- def to_ruby(selects = nil)
- if selects
- puts "DEPRECATION WARNING: PluckMap::Attribute#to_ruby no longer requires an argument. Replace `attribute.to_ruby(keys)` with `attribute.to_ruby`."
- end
-
- return @value.inspect if defined?(@value)
+ def to_ruby
+ return @value.inspect if value?
return "values[#{indexes[0]}]" if indexes.length == 1 && !block
ruby = "values.values_at(#{indexes.join(", ")})"
ruby = "invoke(:\"#{id}\", #{ruby})" if block
ruby
+ end
+
+ def exec(values)
+ return @value if value?
+ return values[indexes[0]] if indexes.length == 1 && !block
+ _values = values.values_at(*indexes)
+ _values = apply(_values) if block
+ _values
end
def values