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



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

#queryObject

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`

Parameters:

  • name (#to_sym)

    name of method

  • return_self (Boolean) (defaults to: false)

    should defined method return self or result



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_proxyClass

Returns query_proxy (‘q`) class.

Returns:

  • (Class)

    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`

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



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

Parameters:

  • methods (Array(#to_sym))

    names of methods defined



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

Parameters:

  • block (#to_proc)

Returns:



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

Parameters:

  • method (#to_sym)

    any instance public method name

  • args (Array(Object))

    method call params

  • block (#to_proc)

    block would be sent to method

Returns:

  • (Object)

    query



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`

Parameters:

  • block (#to_proc)

    block to whitch instance would be yielded

Returns:

  • (Object)

    query



70
71
72
73
# File 'lib/xquery/abstract.rb', line 70

def with(&block)
  block.call(self)
  query
end