Class: PluckMap::Attributes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pluck_map/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes, model) ⇒ Attributes

Returns a new instance of Attributes.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pluck_map/attributes.rb', line 7

def initialize(attributes, model)
  @model = model
  @_attributes = attributes.freeze
  @_attributes_by_id = {}
  @selects = []
  attributes.each do |attribute|
    attribute.indexes = attribute.selects.map do |select|
      selects.find_index(select) || begin
        selects.push(select)
        selects.length - 1
      end
    end
    _attributes_by_id[attribute.id] = attribute
  end
  _attributes_by_id.freeze
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/pluck_map/attributes.rb', line 5

def model
  @model
end

#selectsObject (readonly)

Returns the value of attribute selects.



5
6
7
# File 'lib/pluck_map/attributes.rb', line 5

def selects
  @selects
end

Instance Method Details

#==(other) ⇒ Object



67
68
69
70
# File 'lib/pluck_map/attributes.rb', line 67

def ==(other)
  return false if self.class != other.class
  _attributes == other.send(:_attributes)
end

#[](index) ⇒ Object



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

def [](index)
  _attributes[index]
end

#by_idObject



44
45
46
# File 'lib/pluck_map/attributes.rb', line 44

def by_id
  _attributes_by_id
end

#each(&block) ⇒ Object



26
27
28
# File 'lib/pluck_map/attributes.rb', line 26

def each(&block)
  _attributes.each(&block)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/pluck_map/attributes.rb', line 76

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

#hashObject



72
73
74
# File 'lib/pluck_map/attributes.rb', line 72

def hash
  _attributes.hash
end

#idsObject



40
41
42
# File 'lib/pluck_map/attributes.rb', line 40

def ids
  _attributes_by_id.keys
end

#lengthObject



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

def length
  _attributes.length
end

#nested?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pluck_map/attributes.rb', line 61

def nested?
  _attributes.any?(&:nested?)
end

#to_json_arrayObject



48
49
50
51
52
53
# File 'lib/pluck_map/attributes.rb', line 48

def to_json_array
  PluckMap::BuildJsonArray.new(*selects.map do |select|
    select = model.arel_table[select] if select.is_a?(Symbol)
    select
  end)
end

#will_map?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/pluck_map/attributes.rb', line 57

def will_map?
  _attributes.any?(&:will_map?)
end