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)



115
116
117
118
119
120
121
# File 'lib/cistern/collection.rb', line 115

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

Instance Attribute Details

#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

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

Class Method Details

.service_collection(service, klass, name) ⇒ Object

:nodoc:



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

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

Instance Method Details

#==(comparison_object) ⇒ Object



103
104
105
106
107
# File 'lib/cistern/collection.rb', line 103

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

#all(_ = {}) ⇒ Object



34
35
36
# File 'lib/cistern/collection.rb', line 34

def all(_ = {})
  fail NotImplementedError
end

#buildObject



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

alias_method :build, :initialize

#clearObject



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

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

#create(attributes = {}) ⇒ Object



38
39
40
# File 'lib/cistern/collection.rb', line 38

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

#get(_identity) ⇒ Object



42
43
44
# File 'lib/cistern/collection.rb', line 42

def get(_identity)
  fail NotImplementedError
end

#initialize(attributes = {}) ⇒ Object



30
31
32
# File 'lib/cistern/collection.rb', line 30

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

#inspectObject



51
52
53
54
55
56
# File 'lib/cistern/collection.rb', line 51

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



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

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.



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

def load_records
  all unless loaded
end

#modelObject



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

def model
  self.class.model
end

#new(attributes = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cistern/collection.rb', line 76

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,
      service: service
    }.merge(attributes)
  )
end

#reloadObject



88
89
90
91
92
# File 'lib/cistern/collection.rb', line 88

def reload
  clear
  load_records
  self
end

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

Returns:

  • (Boolean)


99
100
101
# File 'lib/cistern/collection.rb', line 99

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

#to_aObject



94
95
96
97
# File 'lib/cistern/collection.rb', line 94

def to_a
  load_records
  records || []
end