Class: XQuery::Abstract
- Inherits:
-
Object
- Object
- XQuery::Abstract
- Defined in:
- lib/xquery/abstract.rb
Overview
Abstract superclass, should be inherited, not used
Direct Known Subclasses
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
contains current state of wrapped query.
Class Method Summary collapse
-
.alias_on_q(name, return_self = false) ⇒ Object
Aliases method to ‘#__method` and `q.method`.
-
.inherited(child) ⇒ Object
inherited classes should also have their query_proxies inherited.
-
.query_proxy ⇒ Class
Query_proxy (‘q`) class.
-
.with(*args, &block) ⇒ Object
Yields instance inside block.
-
.wrap_method(name, as: name) ⇒ Object
Defines ‘method`, `__method` and `q.method`.
-
.wrap_methods(*methods) ⇒ Object
Aame as wrap_method, but hanldes multiply methods.
Instance Method Summary collapse
-
#apply(&block) ⇒ XQuery::Abstract
Yields query inside block.
-
#execute(method, *args, &block) ⇒ Object
Executes specified method, returns query.
-
#initialize(query) ⇒ Abstract
constructor
A new instance of Abstract.
-
#with(&block) ⇒ Object
Yields iteself inside block.
Constructor Details
#initialize(query) ⇒ Abstract
Returns a new instance of Abstract.
62 63 64 65 |
# File 'lib/xquery/abstract.rb', line 62 def initialize(query) self.query = query @query_proxy = self.class.query_proxy.new(self) end |
Instance Attribute Details
#query ⇒ Object
contains current state of wrapped query
59 60 61 |
# File 'lib/xquery/abstract.rb', line 59 def query @query end |
Class Method Details
.alias_on_q(name, return_self = false) ⇒ Object
Aliases method to ‘#__method` and `q.method`
38 39 40 41 42 43 44 45 46 |
# File 'lib/xquery/abstract.rb', line 38 def self.alias_on_q(name, return_self = false) alias_method("__#{name}", name) private("__#{name}") query_proxy.send(:define_method, name) do |*args, &block| result = instance.send("__#{name}", *args, &block) return_self ? self : result end end |
.inherited(child) ⇒ Object
inherited classes should also have their query_proxies inherited
54 55 56 |
# File 'lib/xquery/abstract.rb', line 54 def self.inherited(child) child.instance_variable_set(:@query_proxy, Class.new(query_proxy)) end |
.query_proxy ⇒ Class
Returns query_proxy (‘q`) class.
49 50 51 |
# File 'lib/xquery/abstract.rb', line 49 def self.query_proxy @query_proxy ||= Class.new(QueryProxy) end |
.with(*args, &block) ⇒ Object
Yields instance inside block. I suggest to name it ‘q`
14 15 16 17 18 |
# File 'lib/xquery/abstract.rb', line 14 def self.with(*args, &block) instance = new(*args) block.call(instance) instance.query end |
.wrap_method(name, as: name) ⇒ Object
Defines ‘method`, `__method` and `q.method`. Both of wich changes query to query.method
24 25 26 27 |
# File 'lib/xquery/abstract.rb', line 24 def self.wrap_method(name, as: name) define_method(as) { |*args, &block| _update_query(name, *args, &block) } alias_on_q(as, true) end |
.wrap_methods(*methods) ⇒ Object
Aame as wrap_method, but hanldes multiply methods
31 32 33 |
# File 'lib/xquery/abstract.rb', line 31 def self.wrap_methods(*methods) methods.each(&method(:wrap_method)) end |
Instance Method Details
#apply(&block) ⇒ XQuery::Abstract
Yields query inside block
90 91 92 93 |
# File 'lib/xquery/abstract.rb', line 90 def apply(&block) self.query = block.call(query) self end |
#execute(method, *args, &block) ⇒ Object
Executes specified method, returns query. Feel free to redefine this method in case it’s only public api method in your class
82 83 84 85 |
# File 'lib/xquery/abstract.rb', line 82 def execute(method, *args, &block) public_send(method, *args, &block) query end |
#with(&block) ⇒ Object
Yields iteself inside block. I suggest to name it ‘q`
70 71 72 73 |
# File 'lib/xquery/abstract.rb', line 70 def with(&block) block.call(self) query end |