Class: CouchRest::Model::Proxyable::ModelProxy
- Inherits:
-
Object
- Object
- CouchRest::Model::Proxyable::ModelProxy
show all
- Defined in:
- lib/couchrest/model/proxyable.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#all(opts = {}, &block) ⇒ Object
-
#build_from_database(attrs = {}, options = {}, &block) ⇒ Object
-
#count(opts = {}) ⇒ Object
-
#design_doc ⇒ Object
-
#first(opts = {}) ⇒ Object
-
#first_from_view(name, *args) ⇒ Object
-
#get(id) ⇒ Object
(also: #find)
-
#has_view?(view) ⇒ Boolean
-
#initialize(model, owner, owner_name, database) ⇒ ModelProxy
constructor
A new instance of ModelProxy.
-
#last(opts = {}) ⇒ Object
-
#method_missing(m, *args, &block) ⇒ Object
-
#new(attrs = {}, options = {}, &block) ⇒ Object
-
#save_design_doc(db = nil) ⇒ Object
-
#view(name, query = {}, &block) ⇒ Object
-
#view_by(*args) ⇒ Object
Constructor Details
#initialize(model, owner, owner_name, database) ⇒ ModelProxy
68
69
70
71
72
73
|
# File 'lib/couchrest/model/proxyable.rb', line 68
def initialize(model, owner, owner_name, database)
@model = model
@owner = owner
@owner_name = owner_name
@database = database
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/couchrest/model/proxyable.rb', line 84
def method_missing(m, *args, &block)
if has_view?(m)
if model.respond_to?(m)
return model.send(m, *args).proxy(self)
else
query = args.shift || {}
return view(m, query, *args, &block)
end
elsif m.to_s =~ /^find_(by_.+)/
view_name = $1
if has_view?(view_name)
return first_from_view(view_name, *args)
end
end
super
end
|
Instance Attribute Details
#database ⇒ Object
Returns the value of attribute database.
66
67
68
|
# File 'lib/couchrest/model/proxyable.rb', line 66
def database
@database
end
|
#model ⇒ Object
Returns the value of attribute model.
66
67
68
|
# File 'lib/couchrest/model/proxyable.rb', line 66
def model
@model
end
|
#owner ⇒ Object
Returns the value of attribute owner.
66
67
68
|
# File 'lib/couchrest/model/proxyable.rb', line 66
def owner
@owner
end
|
#owner_name ⇒ Object
Returns the value of attribute owner_name.
66
67
68
|
# File 'lib/couchrest/model/proxyable.rb', line 66
def owner_name
@owner_name
end
|
Instance Method Details
#all(opts = {}, &block) ⇒ Object
103
104
105
|
# File 'lib/couchrest/model/proxyable.rb', line 103
def all(opts = {}, &block)
proxy_update_all(@model.all({:database => @database}.merge(opts), &block))
end
|
#build_from_database(attrs = {}, options = {}, &block) ⇒ Object
80
81
82
|
# File 'lib/couchrest/model/proxyable.rb', line 80
def build_from_database(attrs = {}, options = {}, &block)
proxy_block_update(:build_from_database, attrs, options, &block)
end
|
#count(opts = {}) ⇒ Object
107
108
109
|
# File 'lib/couchrest/model/proxyable.rb', line 107
def count(opts = {})
@model.count({:database => @database}.merge(opts))
end
|
#design_doc ⇒ Object
145
146
147
|
# File 'lib/couchrest/model/proxyable.rb', line 145
def design_doc
@model.design_doc
end
|
#first(opts = {}) ⇒ Object
111
112
113
|
# File 'lib/couchrest/model/proxyable.rb', line 111
def first(opts = {})
proxy_update(@model.first({:database => @database}.merge(opts)))
end
|
#first_from_view(name, *args) ⇒ Object
138
139
140
141
142
|
# File 'lib/couchrest/model/proxyable.rb', line 138
def first_from_view(name, *args)
(args.last.is_a?(Hash) ? args.last : (args << {}).last)[:database] = @database
proxy_update(@model.first_from_view(name, *args))
end
|
#get(id) ⇒ Object
Also known as:
find
119
120
121
|
# File 'lib/couchrest/model/proxyable.rb', line 119
def get(id)
proxy_update(@model.get(id, @database))
end
|
#has_view?(view) ⇒ Boolean
126
127
128
|
# File 'lib/couchrest/model/proxyable.rb', line 126
def has_view?(view)
@model.has_view?(view)
end
|
#last(opts = {}) ⇒ Object
115
116
117
|
# File 'lib/couchrest/model/proxyable.rb', line 115
def last(opts = {})
proxy_update(@model.last({:database => @database}.merge(opts)))
end
|
#new(attrs = {}, options = {}, &block) ⇒ Object
76
77
78
|
# File 'lib/couchrest/model/proxyable.rb', line 76
def new(attrs = {}, options = {}, &block)
proxy_block_update(:new, attrs, options, &block)
end
|
#save_design_doc(db = nil) ⇒ Object
149
150
151
|
# File 'lib/couchrest/model/proxyable.rb', line 149
def save_design_doc(db = nil)
@model.save_design_doc(db || @database)
end
|
#view(name, query = {}, &block) ⇒ Object
134
135
136
|
# File 'lib/couchrest/model/proxyable.rb', line 134
def view(name, query={}, &block)
proxy_update_all(@model.view(name, {:database => @database}.merge(query), &block))
end
|
#view_by(*args) ⇒ Object
130
131
132
|
# File 'lib/couchrest/model/proxyable.rb', line 130
def view_by(*args)
@model.view_by(*args)
end
|