class XQuery::Abstract
Abstract
superclass, should be inherited, not used @attr query
contains current state of wrapped query
Attributes
Public Class Methods
Aliases method to `#__method` and `q.method` @param name [#to_sym] name of method @param return_self [Boolean] should defined method return self or result
# File lib/xquery/abstract.rb, line 43 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 classes should also have their query_proxies inherited @api private @param child [Class] class inheriting this
# File lib/xquery/abstract.rb, line 61 def self.inherited(child) child.instance_variable_set(:@query_proxy, Class.new(query_proxy)) end
@param query [Object] generic query
# File lib/xquery/abstract.rb, line 66 def initialize(query) self.query = query @query_proxy = self.class.query_proxy.new(self) end
@return [Class] query_proxy
(`q`) class
# File lib/xquery/abstract.rb, line 54 def self.query_proxy @query_proxy ||= Class.new(QueryProxy) end
Yields instance inside block. I suggest to name it `q` @param args [Array(Object
)]
array of arguments would be passed to `new`
@yield [XQuery::Abstract] new intance @return resulting query
# File lib/xquery/abstract.rb, line 21 def self.with(*args) new(*args).with { |instance| yield(instance) } end
Defines `method`, `__method` and `q.method`. Both of witch changes query to query.method @param name [#to_sym] name of method on query @option as [#to_sym] name of method defined
# File lib/xquery/abstract.rb, line 29 def self.wrap_method(name, as: name) define_method(as) { |*args, &block| _update_query(name, *args, &block) } alias_on_q(as, true) end
Aame as wrap_method
, but hanldes multiply methods @param methods [Array(to_sym)] names of methods defined
# File lib/xquery/abstract.rb, line 36 def self.wrap_methods(*methods) methods.each(&method(:wrap_method)) end
Public Instance Methods
Yields query inside block @yield query @return [XQuery::Abstract] self
# File lib/xquery/abstract.rb, line 94 def apply self.query = yield(query) self end
Executes specified method, returns query. Feel free to redefine this method in case it's only public api method in your class @param method [#to_sym] any instance public method name @param args [Array(Object
)] method call params @param block [#to_proc] block would be sent to method @return [Object] query
# File lib/xquery/abstract.rb, line 86 def execute(method, *args, &block) public_send(method, *args, &block) query end
Yields iteself inside block. I suggest to name it `q` @yield [XQuery::Abstract] itself @return [Object] query
# File lib/xquery/abstract.rb, line 74 def with yield(self) query end
Private Instance Methods
Updates query by calling method on it and storing the result @api private @return [XQuery::Abstract] self
# File lib/xquery/abstract.rb, line 104 def _update_query(method, *args, &block) apply { |x| x.public_send(method, *args, &block) } end
@return [XQuery::QueryProxy] object could be used
to access method wrappers unchanged
# File lib/xquery/abstract.rb, line 120 def q @query_proxy end
Added constraints check @raise XQuery::QuerySuperclassChanged
# File lib/xquery/abstract.rb, line 110 def query=(x) unless x.is_a?(query_superclass) raise QuerySuperclassChanged.new(x, query_superclass) end @query = x end