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)



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

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



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

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

#all(_ = {}) ⇒ Object



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

def all(_ = {})
  fail NotImplementedError
end

#buildObject



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

alias_method :build, :initialize

#clearObject



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

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

#create(attributes = {}) ⇒ Object



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

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

#get(_identity) ⇒ Object



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

def get(_identity)
  fail NotImplementedError
end

#initialize(attributes = {}) ⇒ Object



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

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

#inspectObject



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

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



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

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.



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

def load_records
  all unless loaded
end

#modelObject



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

def model
  self.class.model
end

#new(attributes = {}) ⇒ Object



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

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



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

def reload
  clear
  load_records
  self
end

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

Returns:

  • (Boolean)


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

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



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

def to_a
  load_records
  records || []
end