lib/pluck_map/attribute.rb in pluck_map-0.1.0 vs lib/pluck_map/attribute.rb in pluck_map-0.2.0

- old
+ new

@@ -4,15 +4,21 @@ def initialize(id, options={}) @id = id @selects = Array(options.fetch(:select, id)) @name = options.fetch(:as, id) - @alias = name.to_s.gsub('_', ' ') + @alias = name.to_s.tr("_", " ") @block = options[:map] - 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 + + 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 define a block if you are going to select " << + "more than one expression from the database" if selects.length > 1 && !block + end end def apply(object) block.call(*object) end @@ -42,9 +48,10 @@ # # The array of values will be correspond to the array # of keys. This method determines which values pertain # to it by figuring out which order its keys were selected in def to_ruby(keys) + return @value.inspect if defined?(@value) indexes = self.keys.map { |key| keys.index(key) } return "values[#{indexes[0]}]" if indexes.length == 1 && !block ruby = "values.values_at(#{indexes.join(", ")})" ruby = "invoke(:\"#{id}\", #{ruby})" if block ruby