Class: Smooth::Collection
- Inherits:
-
Object
- Object
- Smooth::Collection
show all
- Includes:
- Cacheable
- Defined in:
- lib/smooth/collection.rb,
lib/smooth/collection/query.rb,
lib/smooth/collection/cacheable.rb
Defined Under Namespace
Modules: Cacheable
Classes: Query
Constant Summary
collapse
- InvalidBackend =
Class.new(Exception)
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Cacheable
#create_cache_adapter
Constructor Details
#initialize(namespace, models = [], options = {}) ⇒ Collection
Create an instance of a Smooth::Collection.
Examples:
Smooth::Collection.new(namespace: "namespace", backend: "file")
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/smooth/collection.rb', line 36
def initialize namespace, models=[], options={}
if models.is_a?(Hash)
options = models
models = Array(options[:models])
end
if namespace.is_a?(Hash)
options = namespace
end
if namespace.is_a?(String)
options[:namespace] = namespace
end
@options = options
if options[:model] and !options[:backend]
options[:backend] = "active_record"
end
validate_backend
if options[:cache].is_a?(String)
options[:cache] = {
backend: "redis"
}
end
if options[:cache]
@cache_adapter = create_cache_adapter(options[:cache])
end
@model_class = options[:model_class] || self.class.model_class
@models = models
end
|
Class Attribute Details
.backend ⇒ Object
Returns the value of attribute backend.
10
11
12
|
# File 'lib/smooth/collection.rb', line 10
def backend
@backend
end
|
.model_class ⇒ Object
Returns the value of attribute model_class.
10
11
12
|
# File 'lib/smooth/collection.rb', line 10
def model_class
@model_class
end
|
.namespace ⇒ Object
Returns the value of attribute namespace.
10
11
12
|
# File 'lib/smooth/collection.rb', line 10
def namespace
@namespace
end
|
Instance Attribute Details
#backend ⇒ Object
Returns the value of attribute backend.
7
8
9
|
# File 'lib/smooth/collection.rb', line 7
def backend
@backend
end
|
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/smooth/collection.rb', line 7
def options
@options
end
|
Class Method Details
.uses_backend(backend = :file) ⇒ Object
25
26
27
28
|
# File 'lib/smooth/collection.rb', line 25
def self.uses_backend backend=:file
self.backend = backend
self
end
|
.uses_model(model = Smooth::Model) ⇒ Object
15
16
17
18
|
# File 'lib/smooth/collection.rb', line 15
def self.uses_model model=Smooth::Model
self.model_class = model
self
end
|
.uses_namespace(namespace = "smooth:collections") ⇒ Object
20
21
22
23
|
# File 'lib/smooth/collection.rb', line 20
def self.uses_namespace namespace="smooth:collections"
self.namespace = namespace
self
end
|
Instance Method Details
#add(data = {}, options = {}) ⇒ Object
96
97
98
99
100
101
102
103
104
|
# File 'lib/smooth/collection.rb', line 96
def add data={}, options={}
options[:collection] = self
model = data_to_model(data, options)
@models << model
model
end
|
#fetch(options = {}) ⇒ Object
139
140
141
142
|
# File 'lib/smooth/collection.rb', line 139
def fetch options={}
response = query(options, options[:query_options])
reset parse(response, options)
end
|
#find(id) ⇒ Object
112
113
114
|
# File 'lib/smooth/collection.rb', line 112
def find id
find_by(:id, id)
end
|
#find_by(attribute, value) ⇒ Object
106
107
108
109
110
|
# File 'lib/smooth/collection.rb', line 106
def find_by attribute, value
q = {}
q[attribute.to_sym] = value
data_to_model query(q).first
end
|
#length ⇒ Object
152
153
154
|
# File 'lib/smooth/collection.rb', line 152
def length
models.length
end
|
#models ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/smooth/collection.rb', line 116
def models
fetch_from_index unless @fetched
Array(@models).map do |model|
data_to_model(model, collection: self)
end
end
|
#parse(response, options = {}) ⇒ Object
144
145
146
|
# File 'lib/smooth/collection.rb', line 144
def parse response, options={}
response
end
|
#query(params = {}, options = {}) ⇒ Object
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/smooth/collection.rb', line 128
def query params={}, options={}
query = Query.new(params)
query = apply_scope_parameters_to(query)
return backend.query(query) unless cacheable?
cache_adapter.fetch(query.cache_key) do
backend.query(query)
end
end
|
#reset(models = nil) ⇒ Object
148
149
150
|
# File 'lib/smooth/collection.rb', line 148
def reset models=nil
@models = Array(models)
end
|
#sync(method = "read", model = {}, options = {}, &block) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/smooth/collection.rb', line 73
def sync method="read", model={}, options={}, &block
case method.to_sym
when :read
fetch_from_index
when :update
model = data_to_model(model,options)
backend.update model.as_json
fetch_from_index
when :create
model = data_to_model(model,options)
attributes = backend.create(model.as_json)
fetch_from_index
model = data_to_model(attributes, options)
when :destroy
backend.destroy model.id
else
fetch_from_index
end
end
|
#url ⇒ Object
124
125
126
|
# File 'lib/smooth/collection.rb', line 124
def url
backend.url rescue @namespace
end
|