Class: PluckMap::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/pluck_map/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, model, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pluck_map/attribute.rb', line 6

def initialize(id, model, options={})
  @id = id
  @model = model
  @selects = Array(options.fetch(:select, id))
  @name = options.fetch(:as, id)
  @block = options[:map]

  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

    @selects.each do |select|
      if select.is_a?(String) && !select.is_a?(Arel::Nodes::SqlLiteral)
        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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



3
4
5
# File 'lib/pluck_map/attribute.rb', line 3

def block
  @block
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/pluck_map/attribute.rb', line 3

def id
  @id
end

#indexesObject

Returns the value of attribute indexes.



4
5
6
# File 'lib/pluck_map/attribute.rb', line 4

def indexes
  @indexes
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/pluck_map/attribute.rb', line 3

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/pluck_map/attribute.rb', line 3

def name
  @name
end

#selectsObject (readonly)

Returns the value of attribute selects.



3
4
5
# File 'lib/pluck_map/attribute.rb', line 3

def selects
  @selects
end

#valueObject (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



78
79
80
81
# File 'lib/pluck_map/attribute.rb', line 78

def ==(other)
  return false if self.class != other.class
  self.values == other.values
end

#apply(object) ⇒ Object



30
31
32
# File 'lib/pluck_map/attribute.rb', line 30

def apply(object)
  block.call(*object)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/pluck_map/attribute.rb', line 87

def eql?(other)
  return true if self.equal?(other)
  return false if self.class != other.class
  self.values.eql?(other.values)
end

#exec(values) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/pluck_map/attribute.rb', line 64

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

#hashObject



83
84
85
# File 'lib/pluck_map/attribute.rb', line 83

def hash
  values.hash
end

#nested?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/pluck_map/attribute.rb', line 42

def nested?
  false
end

#preload!(results) ⇒ Object



46
47
# File 'lib/pluck_map/attribute.rb', line 46

def preload!(results)
end

#to_rubyObject

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.



56
57
58
59
60
61
62
# File 'lib/pluck_map/attribute.rb', line 56

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

#value?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/pluck_map/attribute.rb', line 34

def value?
  defined?(@value)
end

#valuesObject



74
75
76
# File 'lib/pluck_map/attribute.rb', line 74

def values
  [id, selects, name, value, block]
end

#will_map?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pluck_map/attribute.rb', line 38

def will_map?
  !block.nil?
end