Module: Cistern::Collection

Defined in:
lib/cistern/collection.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BLACKLISTED_ARRAY_METHODS =
[
  :compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
  :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
  :keep_if, :pop, :shift, :delete_at, :compact
].to_set

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



140
141
142
143
144
145
146
# File 'lib/cistern/collection.rb', line 140

def method_missing(method, *args, &block)
  if array_delegable?(method)
    to_a.public_send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#cisternObject

Returns the value of attribute cistern.



16
17
18
# File 'lib/cistern/collection.rb', line 16

def cistern
  @cistern
end

#loadedObject

Returns the value of attribute loaded.



16
17
18
# File 'lib/cistern/collection.rb', line 16

def loaded
  @loaded
end

#recordsObject

Returns the value of attribute records.



16
17
18
# File 'lib/cistern/collection.rb', line 16

def records
  @records
end

Class Method Details

.cistern_collection(cistern, klass, name) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
# File 'lib/cistern/collection.rb', line 8

def self.cistern_collection(cistern, klass, name)
  cistern.const_get(:Collections).module_eval "    def \#{name}(attributes={})\n      \#{klass.name}.new(attributes.merge(cistern: self))\n    end\n  EOS\nend\n", __FILE__, __LINE__

Instance Method Details

#==(comparison_object) ⇒ Object



128
129
130
131
132
# File 'lib/cistern/collection.rb', line 128

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.is_a?(self.class) &&
     comparison_object.to_a == to_a)
end

#all(_ = {}) ⇒ Object



59
60
61
# File 'lib/cistern/collection.rb', line 59

def all(_ = {})
  fail NotImplementedError
end

#buildObject



53
# File 'lib/cistern/collection.rb', line 53

alias_method :build, :initialize

#clearObject



71
72
73
74
# File 'lib/cistern/collection.rb', line 71

def clear
  self.loaded = false
  records && records.clear
end

#create(attributes = {}) ⇒ Object



63
64
65
# File 'lib/cistern/collection.rb', line 63

def create(attributes = {})
  new(attributes).save
end

#get(_identity) ⇒ Object



67
68
69
# File 'lib/cistern/collection.rb', line 67

def get(_identity)
  fail NotImplementedError
end

#initialize(attributes = {}) ⇒ Object



55
56
57
# File 'lib/cistern/collection.rb', line 55

def initialize(attributes = {})
  merge_attributes(attributes)
end

#inspectObject



76
77
78
79
80
81
# File 'lib/cistern/collection.rb', line 76

def inspect
  if Cistern.formatter
    Cistern.formatter.call(self)
  else super
  end
end

#load(objects) ⇒ Object

Should be called within #all to load records into the collection

Parameters:

  • objects (Array<Hash>)

    list of record attributes to be loaded

Returns:

  • self



91
92
93
94
95
# File 'lib/cistern/collection.rb', line 91

def load(objects)
  self.records = (objects || []).map { |object| new(object) }
  self.loaded = true
  self
end

#load_recordsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
# File 'lib/cistern/collection.rb', line 84

def load_records
  all unless loaded
end

#modelObject



97
98
99
# File 'lib/cistern/collection.rb', line 97

def model
  self.class.model
end

#new(attributes = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/cistern/collection.rb', line 101

def new(attributes = {})
  unless attributes.is_a?(::Hash)
    fail(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
  end
  model.new(
    {
      collection: self,
      cistern: cistern,
    }.merge(attributes)
  )
end

#reloadObject



113
114
115
116
117
# File 'lib/cistern/collection.rb', line 113

def reload
  clear
  load_records
  self
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
# File 'lib/cistern/collection.rb', line 124

def respond_to?(method, include_private = false)
  super || array_delegable?(method)
end

#serviceObject



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

def service
  Cistern.deprecation(
    '#service is deprecated.  Please use #cistern',
    caller[0]
  )
  @cistern
end

#service=(service) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/cistern/collection.rb', line 18

def service=(service)
  Cistern.deprecation(
    '#service= is deprecated.  Please use #cistern=',
    caller[0]
  )
  @cistern = service
end

#to_aObject



119
120
121
122
# File 'lib/cistern/collection.rb', line 119

def to_a
  load_records
  records || []
end