Class: XQuery::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/xquery/abstract.rb

Overview

abstract superclass, should be inherited, not used

Direct Known Subclasses

Generic

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Abstract

Returns a new instance of Abstract.

Parameters:

  • query (Object)

    generic query



66
67
68
69
# File 'lib/xquery/abstract.rb', line 66

def initialize(query)
  self.query = query
  @query_proxy = self.class.query_proxy.new(self)
end

Instance Attribute Details

#queryObject

contains current state of wrapped query



63
64
65
# File 'lib/xquery/abstract.rb', line 63

def query
  @query
end

Class Method Details

.alias_on_q(name, return_self = false) ⇒ Object

Aliases method to __method and q.method

Parameters:

  • name (#to_sym)

    name of method

  • return_self (Boolean) (defaults to: false)

    should defined method return self or result



42
43
44
45
46
47
48
49
50
# File 'lib/xquery/abstract.rb', line 42

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



58
59
60
# File 'lib/xquery/abstract.rb', line 58

def self.inherited(child)
  child.instance_variable_set(:@query_proxy, Class.new(query_proxy))
end

.query_proxyClass

Returns query_proxy (‘q`) class.

Returns:

  • (Class)

    query_proxy (‘q`) class



53
54
55
# File 'lib/xquery/abstract.rb', line 53

def self.query_proxy
  @query_proxy ||= Class.new(QueryProxy)
end

.with(*args, &block) ⇒ Object Also known as: execute

yields instance inside block. I suggest to name it ‘q`

Parameters:

  • args (Array(Object))

    array of arguments would be passed to

  • block (#to_proc)

    block to witch instance would be yielded



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

Parameters:

  • name (#to_sym)

    name of method on query

  • as (#to_sym) (defaults to: name)

    name of method defined



28
29
30
31
# File 'lib/xquery/abstract.rb', line 28

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

same as wrap_method, but hanldes multiply methods

Parameters:

  • methods (Array(#to_sym))

    names of methods defined



35
36
37
# File 'lib/xquery/abstract.rb', line 35

def self.wrap_methods(*methods)
  methods.each(&method(:wrap_method))
end

Instance Method Details

#apply(&block) ⇒ XQuery::Abstract

yields query inside block

Parameters:

  • block (#to_proc)

Returns:



74
75
76
77
# File 'lib/xquery/abstract.rb', line 74

def apply(&block)
  self.query = block.call(query)
  self
end