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

Returns:

  • (Object)

    the current value of query



10
11
12
# File 'lib/xquery/abstract.rb', line 10

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



43
44
45
46
47
48
49
50
51
# 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(child) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

inherited classes should also have their query_proxies inherited

Parameters:

  • child (Class)

    class inheriting this



61
62
63
# File 'lib/xquery/abstract.rb', line 61

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



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

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

.with(*args) {|XQuery::Abstract| ... } ⇒ Object

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

Parameters:

  • args (Array(Object))

    array of arguments would be passed to ‘new`

Yields:

Returns:

  • resulting query



21
22
23
# File 'lib/xquery/abstract.rb', line 21

def self.with(*args)
  new(*args).with { |instance| yield(instance) }
end

.wrap_method(name, as: name) ⇒ Object

Defines ‘method`, `__method` and `q.method`. Both of witch changes query to query.method

Parameters:

  • name (#to_sym)

    name of method on query

  • as (Hash) (defaults to: name)

    a customizable set of options

Options Hash (as:):

  • name (#to_sym)

    of method defined



29
30
31
32
# 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

.wrap_methods(*methods) ⇒ Object

Aame as wrap_method, but hanldes multiply methods

Parameters:

  • methods (Array(#to_sym))

    names of methods defined



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

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

Instance Method Details

#apply { ... } ⇒ XQuery::Abstract

Yields query inside block

Yields:

  • query

Returns:



94
95
96
97
# File 'lib/xquery/abstract.rb', line 94

def apply
  self.query = yield(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



86
87
88
89
# File 'lib/xquery/abstract.rb', line 86

def execute(method, *args, &block)
  public_send(method, *args, &block)
  query
end

#with {|XQuery::Abstract| ... } ⇒ Object

Yields iteself inside block. I suggest to name it ‘q`

Yields:

Returns:

  • (Object)

    query



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

def with
  yield(self)
  query
end