Class: Divan::Base
- Inherits:
-
Models::Base
- Object
- Models::Base
- Divan::Base
show all
- Defined in:
- lib/divan/base.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opts = {}) ⇒ Base
Returns a new instance of Base.
5
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/divan/base.rb', line 5
def initialize(opts = {})
opts = opts.clone
@id = opts.delete(:id) || opts.delete(:_id) || Divan::Utils.uuid
@rev = opts.delete(:rev) || opts.delete(:_rev)
@meta_attributes = opts.find_all{ |k,v| k.to_s[0..0] == '_' }.inject({}) do |hash, (key, value)|
hash[key.to_s[1..-1].to_sym] = opts.delete key
hash
end
@attributes = opts
@attributes[self.class.type_field.to_sym] = self.class.type_name unless self.class.top_level_model?
self.class.properties.each{ |property| @attributes[property] ||= nil }
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/divan/base.rb', line 35
def method_missing(method, *args, &block)
method = method.to_s
if method[-1..-1] == '='
attrib = method[0..-2].to_sym
return @attributes[attrib] = args.first
end
if( @attributes.keys.include?( (attrib = method[0..-1].to_sym) ) )
return @attributes[attrib]
end
raise NoMethodError, "undefined method '#{method}' for #{self}:#{self.class}"
end
|
Class Attribute Details
.database ⇒ Object
Returns the value of attribute database.
65
66
67
|
# File 'lib/divan/base.rb', line 65
def database
@database
end
|
.model_name ⇒ Object
Returns the value of attribute model_name.
66
67
68
|
# File 'lib/divan/base.rb', line 66
def model_name
@model_name
end
|
.type_field ⇒ Object
83
84
85
|
# File 'lib/divan/base.rb', line 83
def type_field
@type_field ||= 'divan_doc_type'
end
|
.type_name ⇒ Object
79
80
81
|
# File 'lib/divan/base.rb', line 79
def type_name
@type_name ||= model_name
end
|
.view_by_params ⇒ Object
Returns the value of attribute view_by_params.
65
66
67
|
# File 'lib/divan/base.rb', line 65
def view_by_params
@view_by_params
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
3
4
5
|
# File 'lib/divan/base.rb', line 3
def attributes
@attributes
end
|
#id ⇒ Object
Returns the value of attribute id.
3
4
5
|
# File 'lib/divan/base.rb', line 3
def id
@id
end
|
#last_request ⇒ Object
Returns the value of attribute last_request.
3
4
5
|
# File 'lib/divan/base.rb', line 3
def last_request
@last_request
end
|
Returns the value of attribute meta_attributes.
3
4
5
|
# File 'lib/divan/base.rb', line 3
def meta_attributes
@meta_attributes
end
|
#rev ⇒ Object
Returns the value of attribute rev.
3
4
5
|
# File 'lib/divan/base.rb', line 3
def rev
@rev
end
|
Class Method Details
.define_view(param, functions) ⇒ Object
135
136
137
138
|
# File 'lib/divan/base.rb', line 135
def define_view(param, functions)
@views ||= {}
@views[param.to_sym] = functions
end
|
.define_view_all ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/divan/base.rb', line 140
def define_view_all
if top_level_model?
define_view :all, :map => "function(doc){ if(doc._id[0] != \"_\"){ emit(null, doc) } }"
else
define_view :all, :map => "function(doc){ if(doc.#{type_field} == \"#{type_name}\"){ emit(null, doc) } }"
end
end
|
.inherited(subclass) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/divan/base.rb', line 68
def inherited(subclass)
strs = subclass.name.match(/[^:]*\Z/)[0].split(/([A-Z][^A-Z]*)/)
strs.delete ''
subclass.model_name = strs.map{ |x| x.downcase }.join('_')
if database
subclass.top_level_model! if database.name == subclass.model_name
subclass.define_view_all
subclass.database = database
end
end
|
.properties ⇒ Object
99
100
101
|
# File 'lib/divan/base.rb', line 99
def properties
@properties ||= ( superclass.methods.include? :properties ) ? superclass.properties.clone : []
end
|
.property(*args) ⇒ Object
103
104
105
|
# File 'lib/divan/base.rb', line 103
def property(*args)
properties.concat args.flatten
end
|
.query_view(view, args = {}) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/divan/base.rb', line 148
def query_view(view, args={})
args ||= {}
args = args.clone
[:key, :startkey, :endkey].each do |k|
args[k] = args[k].to_json if args[k]
end
if args[:keys]
keys = args.delete(:keys).to_json
view_path = Divan::Utils.formatted_path "_design/#{model_name}/_view/#{view}", args
last_request = database.client[view_path].post keys
else
args[:key] = nil.to_json if args[:key].nil? && args[:startkey].nil? && args[:endkey].nil?
view_path = Divan::Utils.formatted_path "_design/#{model_name}/_view/#{view}", args
last_request = database.client[view_path].get
end
results = JSON.parse last_request, :symbolize_names => true
results[:rows].map do |row|
obj = self.new row[:value]
obj.last_request = last_request
obj
end
end
|
.top_level_model!(true_or_false = true) ⇒ Object
87
88
89
|
# File 'lib/divan/base.rb', line 87
def top_level_model!(true_or_false = true)
@top_level_model = true_or_false
end
|
.top_level_model? ⇒ Boolean
91
92
93
|
# File 'lib/divan/base.rb', line 91
def top_level_model?
@top_level_model ||= false
end
|
.views ⇒ Object
95
96
97
|
# File 'lib/divan/base.rb', line 95
def views
@views ||= {}
end
|
Instance Method Details
#[](key) ⇒ Object
18
19
20
|
# File 'lib/divan/base.rb', line 18
def [](key)
@attributes[key]
end
|
#[]=(key, value) ⇒ Object
22
23
24
|
# File 'lib/divan/base.rb', line 22
def []=(key, value)
@attributes[key] = value
end
|
#database ⇒ Object
31
32
33
|
# File 'lib/divan/base.rb', line 31
def database
self.class.database
end
|
#to_s ⇒ Object
Also known as:
inspect
47
48
49
|
# File 'lib/divan/base.rb', line 47
def to_s
"\#<#{database.name.to_s.split('_').map{|str| "#{str[0..0].upcase}#{str[1..-1]}" }} #{@attributes.inspect.gsub("\\\"", "\"")}>"
end
|
#validate ⇒ Object
Also known as:
valid?
26
27
28
|
# File 'lib/divan/base.rb', line 26
def validate
true
end
|