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=, #changed, #dirty?, #dirty_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.



25
26
27
# File 'lib/cistern/collection.rb', line 25

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)



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

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 Also known as: service

: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



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

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

Instance Method Details

#==(comparison_object) ⇒ Object



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

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)


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

def all(identity)
  raise NotImplementedError
end

#buildObject



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

alias build initialize

#clearObject



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

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

#create(attributes = {}) ⇒ Object



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

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

#get(identity) ⇒ Object

Raises:

  • (NotImplementedError)


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

def get(identity)
  raise NotImplementedError
end

#inspectObject



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

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



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

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.



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

def load_records
  self.all unless self.loaded
end

#modelObject



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

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

#new(attributes = {}) ⇒ Object



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

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



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

def reload
  clear
  load_records
  self
end

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

Returns:

  • (Boolean)


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

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

#to_aObject



89
90
91
92
# File 'lib/cistern/collection.rb', line 89

def to_a
  load_records
  self.records || []
end