Class: Findable::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/findable/collection.rb

Overview

Record collection class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, records) ⇒ Collection

Returns a new instance of Collection.

Raises:

  • (ArgumentError)


8
9
10
11
12
# File 'lib/findable/collection.rb', line 8

def initialize(model, records)
  raise ArgumentError unless records.is_a?(Array)
  @model = model
  @records = records
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



6
7
8
# File 'lib/findable/collection.rb', line 6

def model
  @model
end

#recordsObject (readonly) Also known as: to_a

Returns the value of attribute records.



6
7
8
# File 'lib/findable/collection.rb', line 6

def records
  @records
end

Instance Method Details

#eachObject



25
26
27
28
29
30
31
# File 'lib/findable/collection.rb', line 25

def each
  if block_given?
    records.each {|record| yield(record) }
  else
    records.to_enum
  end
end

#find(ids) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/findable/collection.rb', line 33

def find(ids)
  if ids.is_a?(Array)
    if refined = records.select {|record| record.id.in?(ids) }
      regenerate(refined)
    else
      raise not_found(id: ids)
    end
  else
    records.detect {|record| record.id == ids } || (raise not_found(id: ids))
  end
end

#find_by(conditions) ⇒ Object



45
46
47
48
49
# File 'lib/findable/collection.rb', line 45

def find_by(conditions)
  records.detect {|record|
    conditions.all? {|k, v| record.public_send(k) == v }
  }
end

#find_by!(conditions) ⇒ Object



51
52
53
# File 'lib/findable/collection.rb', line 51

def find_by!(conditions)
  find_by(conditions.dup) || (raise not_found(conditions))
end

#inspectObject



86
87
88
# File 'lib/findable/collection.rb', line 86

def inspect
  "[#{records.map(&:inspect).join(",\n")}]"
end

#order(*columns) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
65
66
67
68
# File 'lib/findable/collection.rb', line 61

def order(*columns)
  columns.flatten!
  raise ArgumentError, "Must contain arguments" if columns.empty?

  regenerate(records.sort_by {|record|
    columns.map {|column| record.public_send(column) }
  })
end

#ordered_find(*_ids) ⇒ Object



70
71
72
73
# File 'lib/findable/collection.rb', line 70

def ordered_find(*_ids)
  _ids.flatten!
  records.index_by(&:id).values_at(*_ids)
end

#pluck(*columns) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/findable/collection.rb', line 75

def pluck(*columns)
  columns.flatten!
  return records.map {|record| record.attributes.values } if columns.empty?
  single = (columns.size == 1)

  records.map {|record|
    values = columns.map {|column| record.public_send(column) }
    single ? values.first : values
  }
end

#presenceObject



21
22
23
# File 'lib/findable/collection.rb', line 21

def presence
  present? ? self : nil
end

#where(conditions) ⇒ Object



55
56
57
58
59
# File 'lib/findable/collection.rb', line 55

def where(conditions)
  regenerate(records.select {|record|
    conditions.all? {|k, v| record.public_send(k) == v }
  })
end