Class: PluckMap::Attribute
- Inherits:
-
Object
- Object
- PluckMap::Attribute
- Defined in:
- lib/pluck_map/attribute.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#selects ⇒ Object
readonly
Returns the value of attribute selects.
Instance Method Summary collapse
- #apply(object) ⇒ Object
-
#initialize(id, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
-
#keys ⇒ Object
These are the names of the values that are returned from the database (every row returned by the database will be a hash of key-value pairs).
- #no_map? ⇒ Boolean
-
#to_ruby(keys) ⇒ Object
When the PluckMapPresenter performs the query, it will receive an array of rows.
Constructor Details
#initialize(id, options = {}) ⇒ Attribute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pluck_map/attribute.rb', line 5 def initialize(id, ={}) @id = id @selects = Array(.fetch(:select, id)) @name = .fetch(:as, id) @alias = name.to_s.tr("_", " ") @block = [:map] if .key? :value @value = [: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 |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def alias @alias end |
#block ⇒ Object (readonly)
Returns the value of attribute block.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def block @block end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def name @name end |
#selects ⇒ Object (readonly)
Returns the value of attribute selects.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def selects @selects end |
Instance Method Details
#apply(object) ⇒ Object
22 23 24 |
# File 'lib/pluck_map/attribute.rb', line 22 def apply(object) block.call(*object) end |
#keys ⇒ Object
These are the names of the values that are returned from the database (every row returned by the database will be a hash of key-value pairs)
If we are only selecting one thing from the database then the PluckMapPresenter will automatically alias the select-expression, so the key will be the alias.
33 34 35 |
# File 'lib/pluck_map/attribute.rb', line 33 def keys selects.length == 1 ? [self.alias] : selects end |
#no_map? ⇒ Boolean
37 38 39 |
# File 'lib/pluck_map/attribute.rb', line 37 def no_map? block.nil? end |
#to_ruby(keys) ⇒ Object
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.
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
52 53 54 55 56 57 58 59 |
# File 'lib/pluck_map/attribute.rb', line 52 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 end |