module Cistern::Collection

Attributes

cistern[RW]
loaded[RW]
records[RW]

Public Class Methods

cistern_collection(cistern, klass, name) click to toggle source
# File lib/cistern/collection.rb, line 12
  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
new(attributes = {}) click to toggle source
# File lib/cistern/collection.rb, line 59
def initialize(attributes = {})
  @loaded = false
  merge_attributes(attributes)
end
Also aliased as: build

Public Instance Methods

==(comparison_object) click to toggle source
# File lib/cistern/collection.rb, line 133
def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.is_a?(self.class) &&
     comparison_object.to_a == to_a)
end
all(_ = {}) click to toggle source
# File lib/cistern/collection.rb, line 64
def all(_ = {})
  fail NotImplementedError
end
build(attributes = {})
Alias for: new
clear() click to toggle source
# File lib/cistern/collection.rb, line 76
def clear
  self.loaded = false
  records && records.clear
end
create(attributes = {}) click to toggle source
# File lib/cistern/collection.rb, line 68
def create(attributes = {})
  new(attributes).save
end
get(_identity) click to toggle source
# File lib/cistern/collection.rb, line 72
def get(_identity)
  fail NotImplementedError
end
inspect() click to toggle source
Calls superclass method
# File lib/cistern/collection.rb, line 81
def inspect
  if Cistern.formatter
    Cistern.formatter.call(self)
  else super
  end
end
load(objects) click to toggle source

Should be called within all to load records into the collection @param [Array<Hash>] objects list of record attributes to be loaded @return self

# File lib/cistern/collection.rb, line 96
def load(objects)
  self.records = (objects || []).map { |object| new(object) }
  self.loaded = true
  self
end
load_records() click to toggle source

@api private

# File lib/cistern/collection.rb, line 89
def load_records
  all unless loaded
end
model() click to toggle source
# File lib/cistern/collection.rb, line 102
def model
  self.class.model
end
new(attributes = {}) click to toggle source
# File lib/cistern/collection.rb, line 106
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
reload() click to toggle source
# File lib/cistern/collection.rb, line 118
def reload
  clear
  load_records
  self
end
respond_to?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/cistern/collection.rb, line 129
def respond_to?(method, include_private = false)
  super || array_delegable?(method)
end
service() click to toggle source
# File lib/cistern/collection.rb, line 30
def service
  Cistern.deprecation(
    '#service is deprecated.  Please use #cistern',
    caller[0]
  )
  @cistern
end
service=(service) click to toggle source
# File lib/cistern/collection.rb, line 22
def service=(service)
  Cistern.deprecation(
    '#service= is deprecated.  Please use #cistern=',
    caller[0]
  )
  @cistern = service
end
to_a() click to toggle source
# File lib/cistern/collection.rb, line 124
def to_a
  load_records
  records || []
end

Protected Instance Methods

array_delegable?(method) click to toggle source
# File lib/cistern/collection.rb, line 141
def array_delegable?(method)
  Array.method_defined?(method) && !BLACKLISTED_ARRAY_METHODS.include?(method)
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/cistern/collection.rb, line 145
def method_missing(method, *args, &block)
  if array_delegable?(method)
    to_a.public_send(method, *args, &block)
  else
    super
  end
end