Class: PluckMap::Attribute
- Inherits:
-
Object
- Object
- PluckMap::Attribute
- Defined in:
- lib/pluck_map/attribute.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#indexes ⇒ Object
Returns the value of attribute indexes.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#selects ⇒ Object
readonly
Returns the value of attribute selects.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #apply(object) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(id, options = {}) ⇒ Attribute
constructor
A new instance of Attribute.
- #no_map? ⇒ Boolean
-
#to_ruby(selects = nil) ⇒ Object
When the PluckMapPresenter performs the query, it will receive an array of rows.
- #values ⇒ Object
Constructor Details
#initialize(id, options = {}) ⇒ Attribute
Returns a new instance of Attribute.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/pluck_map/attribute.rb', line 6 def initialize(id, ={}) @id = id @selects = Array(.fetch(:select, id)) @name = .fetch(:as, id) @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
#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 |
#indexes ⇒ Object
Returns the value of attribute indexes.
4 5 6 |
# File 'lib/pluck_map/attribute.rb', line 4 def indexes @indexes 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 |
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/pluck_map/attribute.rb', line 3 def value @value end |
Instance Method Details
#==(other) ⇒ Object
55 56 57 58 |
# File 'lib/pluck_map/attribute.rb', line 55 def ==(other) return false if self.class != other.class self.values == other.values end |
#apply(object) ⇒ Object
22 23 24 |
# File 'lib/pluck_map/attribute.rb', line 22 def apply(object) block.call(*object) end |
#eql?(other) ⇒ Boolean
64 65 66 67 68 |
# File 'lib/pluck_map/attribute.rb', line 64 def eql?(other) return true if self.equal?(other) return false if self.class != other.class self.values.eql?(other.values) end |
#hash ⇒ Object
60 61 62 |
# File 'lib/pluck_map/attribute.rb', line 60 def hash values.hash end |
#no_map? ⇒ Boolean
26 27 28 |
# File 'lib/pluck_map/attribute.rb', line 26 def no_map? block.nil? end |
#to_ruby(selects = nil) ⇒ 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.
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pluck_map/attribute.rb', line 37 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) return "values[#{indexes[0]}]" if indexes.length == 1 && !block ruby = "values.values_at(#{indexes.join(", ")})" ruby = "invoke(:\"#{id}\", #{ruby})" if block ruby end |
#values ⇒ Object
51 52 53 |
# File 'lib/pluck_map/attribute.rb', line 51 def values [id, selects, name, value, block] end |