Class: Cistern::Collection

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

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 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?, #read_attribute, #requires, #requires_one, #write_attribute

Constructor Details

#initialize(attributes = {}) ⇒ Collection

Returns a new instance of Collection.



23
24
25
# File 'lib/cistern/collection.rb', line 23

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



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

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

Instance Attribute Details

#connectionObject

:nodoc:



11
12
13
# File 'lib/cistern/collection.rb', line 11

def connection
  @connection
end

#loadedObject

:nodoc:



11
12
13
# File 'lib/cistern/collection.rb', line 11

def loaded
  @loaded
end

#recordsObject

:nodoc:



11
12
13
# File 'lib/cistern/collection.rb', line 11

def records
  @records
end

Class Method Details

.model(new_model = nil) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/cistern/collection.rb', line 13

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

Instance Method Details

#==(comparison_object) ⇒ Object



96
97
98
99
100
# File 'lib/cistern/collection.rb', line 96

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

#all(identity) ⇒ Object

Raises:

  • (NotImplementedError)


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

def all(identity)
  raise NotImplementedError
end

#buildObject



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

alias build initialize

#clearObject



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

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

#create(attributes = {}) ⇒ Object



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

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

#get(identity) ⇒ Object

Raises:

  • (NotImplementedError)


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

def get(identity)
  raise NotImplementedError
end

#inspectObject



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

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



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

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.



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

def load_records
  self.all unless self.loaded
end

#modelObject



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

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

#new(attributes = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cistern/collection.rb', line 69

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



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

def reload
  clear
  load_records
  self
end

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

Returns:

  • (Boolean)


92
93
94
# File 'lib/cistern/collection.rb', line 92

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

#to_aObject



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

def to_a
  load_records
  self.records || []
end