Module: Cistern::Collection

Includes:
HashSupport
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

Methods included from HashSupport

#hash_except, #hash_except!, #hash_slice, #hash_stringify_keys

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



143
144
145
146
147
148
149
# File 'lib/cistern/collection.rb', line 143

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.



18
19
20
# File 'lib/cistern/collection.rb', line 18

def cistern
  @cistern
end

#loadedObject

Returns the value of attribute loaded.



18
19
20
# File 'lib/cistern/collection.rb', line 18

def loaded
  @loaded
end

#recordsObject

Returns the value of attribute records.



18
19
20
# File 'lib/cistern/collection.rb', line 18

def records
  @records
end

Class Method Details

.cistern_collection(cistern, klass, name) ⇒ Object

:nodoc:



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

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

Instance Method Details

#==(comparison_object) ⇒ Object



131
132
133
134
135
# File 'lib/cistern/collection.rb', line 131

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

#all(_ = {}) ⇒ Object



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

def all(_ = {})
  fail NotImplementedError
end

#buildObject



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

alias_method :build, :initialize

#clearObject



74
75
76
77
# File 'lib/cistern/collection.rb', line 74

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

#create(attributes = {}) ⇒ Object



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

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

#get(_identity) ⇒ Object



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

def get(_identity)
  fail NotImplementedError
end

#initialize(attributes = {}) ⇒ Object



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

def initialize(attributes = {})
  @loaded = false
  merge_attributes(attributes)
end

#inspectObject



79
80
81
82
83
84
# File 'lib/cistern/collection.rb', line 79

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



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

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.



87
88
89
# File 'lib/cistern/collection.rb', line 87

def load_records
  all unless loaded
end

#modelObject



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

def model
  self.class.model
end

#new(attributes = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/cistern/collection.rb', line 104

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



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

def reload
  clear
  load_records
  self
end

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

Returns:

  • (Boolean)


127
128
129
# File 'lib/cistern/collection.rb', line 127

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

#serviceObject



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

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

#service=(service) ⇒ Object



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

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

#to_aObject



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

def to_a
  load_records
  records || []
end