Class: Cistern::Collection

Inherits:
Array
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods
Defined in:
lib/cistern/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #attributes=, #dup, #identity, #identity=, #merge_attributes, #new_record?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



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

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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

Class Method Details

.model(new_model = nil) ⇒ Object



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

def self.model(new_model=nil)
  if new_model == nil
    @model
  else
    @model = new_model
  end
end

Instance Method Details

#buildObject



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

alias build initialize

#clearObject



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

def clear
  @loaded = true
  super
end

#create(attributes = {}) ⇒ Object



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

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

#get(identity) ⇒ Object

Raises:

  • (NotImplementedError)


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

def get(identity)
  raise NotImplementedError
end

#inspectObject



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

def inspect
  lazy_load unless @loaded
  Cistern.formatter.call(self)
end

#lazy_loadObject

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.



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

def lazy_load
  self.all
end

#load(objects) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/cistern/collection.rb', line 60

def load(objects)
  clear
  for object in objects
    self << new(object)
  end
  self
end

#modelObject



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

def model
  self.class.instance_variable_get('@model')
end

#new(attributes = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cistern/collection.rb', line 72

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

#reloadObject



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

def reload
  clear
  lazy_load
  self
end