xquery 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/home/jelf/docs/xquery/lib/xquery.rb +10 -0
- data/home/jelf/docs/xquery/lib/xquery/abstract.rb +104 -0
- data/home/jelf/docs/xquery/lib/xquery/errors.rb +18 -0
- data/home/jelf/docs/xquery/lib/xquery/generic.rb +35 -0
- data/home/jelf/docs/xquery/lib/xquery/query_proxy.rb +15 -0
- data/home/jelf/docs/xquery/lib/xquery/version.rb +4 -0
- metadata +10 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b2ebbc8254056710b841ed9b8ad053fcd8884d8
|
4
|
+
data.tar.gz: 49b26a7409bbf2f897fcd70ae67de9635cb938e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c2a9116c4bf2e4f1ffbd8c093cb0cb3fbb01f46c7fd9adc244a15fd8cbb6c64f18a19a1e23f74331ee287c94b134712446951ba4ca5f71b6d4dac82d70d11e6
|
7
|
+
data.tar.gz: fee367d59345833fe0cc4d77c01a35480b6da979c3e3d293b4e653d0f7b86adc590bce7626b2e8e3448738a5f2fadfac3fc2dfa36d8c779a0dc2c499d6c2b394
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
# XQuery is designed to replace boring method call chains and allow to easier
|
4
|
+
# convert it in a builder classes
|
5
|
+
# see [README.md] for more information
|
6
|
+
module XQuery
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'xquery/version'
|
10
|
+
require 'xquery/generic'
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'active_support/core_ext/class/attribute.rb'
|
2
|
+
|
3
|
+
require 'xquery/query_proxy'
|
4
|
+
require 'xquery/errors'
|
5
|
+
|
6
|
+
module XQuery
|
7
|
+
# abstract superclass, should be inherited, not used
|
8
|
+
class Abstract
|
9
|
+
class_attribute :query_superclass
|
10
|
+
|
11
|
+
# yields instance inside block. I suggest to name it `q`
|
12
|
+
# @param args [Array(Object)] array of arguments would be passed to
|
13
|
+
# @param block [#to_proc] block to witch instance would be yielded
|
14
|
+
def self.with(*args, &block)
|
15
|
+
instance = new(*args)
|
16
|
+
block.call(instance)
|
17
|
+
instance.query
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
alias_method :execute, :with
|
22
|
+
end
|
23
|
+
|
24
|
+
# Defines `method`, `__method` and `q.method`.
|
25
|
+
# Both of wich changes query to query.method
|
26
|
+
# @param name [#to_sym] name of method on query
|
27
|
+
# @param as [#to_sym] name of method defined
|
28
|
+
def self.wrap_method(name, as: name)
|
29
|
+
define_method(as) { |*args, &block| _update_query(name, *args, &block) }
|
30
|
+
alias_on_q(as, true)
|
31
|
+
end
|
32
|
+
|
33
|
+
# same as wrap_method, but hanldes multiply methods
|
34
|
+
# @param methods [Array(#to_sym)] names of methods defined
|
35
|
+
def self.wrap_methods(*methods)
|
36
|
+
methods.each(&method(:wrap_method))
|
37
|
+
end
|
38
|
+
|
39
|
+
# Aliases method to __method and q.method
|
40
|
+
# @param name [#to_sym] name of method
|
41
|
+
# @param return_self [Boolean] should defined method return self or result
|
42
|
+
def self.alias_on_q(name, return_self = false)
|
43
|
+
alias_method("__#{name}", name)
|
44
|
+
private("__#{name}")
|
45
|
+
|
46
|
+
query_proxy.send(:define_method, name) do |*args, &block|
|
47
|
+
result = instance.send("__#{name}", *args, &block)
|
48
|
+
return_self ? self : result
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# @return [Class] query_proxy (`q`) class
|
53
|
+
def self.query_proxy
|
54
|
+
@query_proxy ||= Class.new(QueryProxy)
|
55
|
+
end
|
56
|
+
|
57
|
+
# inherited classes should also have their query_proxies inherited
|
58
|
+
def self.inherited(child)
|
59
|
+
child.instance_variable_set(:@query_proxy, Class.new(query_proxy))
|
60
|
+
end
|
61
|
+
|
62
|
+
# contains current state of wrapped query
|
63
|
+
attr_reader :query
|
64
|
+
|
65
|
+
# @param query [Object] generic query
|
66
|
+
def initialize(query)
|
67
|
+
self.query = query
|
68
|
+
@query_proxy = self.class.query_proxy.new(self)
|
69
|
+
end
|
70
|
+
|
71
|
+
# yields query inside block
|
72
|
+
# @param block [#to_proc]
|
73
|
+
# @return [XQuery::Abstract] self
|
74
|
+
def apply(&block)
|
75
|
+
self.query = block.call(query)
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# @private_api
|
82
|
+
# updates query by calling method on it and storing the result
|
83
|
+
# @return [XQuery::Abstract] self
|
84
|
+
def _update_query(method, *args, &block)
|
85
|
+
apply { |x| x.public_send(method, *args, &block) }
|
86
|
+
end
|
87
|
+
|
88
|
+
# added constraints check
|
89
|
+
# @raise XQuery::QuerySuperclassChanged
|
90
|
+
def query=(x)
|
91
|
+
unless x.is_a?(query_superclass)
|
92
|
+
fail QuerySuperclassChanged.new(x, query_superclass)
|
93
|
+
end
|
94
|
+
|
95
|
+
@query = x
|
96
|
+
end
|
97
|
+
|
98
|
+
# @return [XQuery::QueryProxy] object could be used
|
99
|
+
# to access method wrappers unchanged
|
100
|
+
def q
|
101
|
+
@query_proxy
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module XQuery
|
2
|
+
# raised when superclass of query changed
|
3
|
+
class QuerySuperclassChanged < StandardError
|
4
|
+
# query on which constraint failed
|
5
|
+
attr_reader :result
|
6
|
+
|
7
|
+
# expected superclass of query
|
8
|
+
attr_reader :expectation
|
9
|
+
|
10
|
+
# @param result [Object] query on which constraint failed
|
11
|
+
# @param expectation [Class] expected superclass
|
12
|
+
def initialize(result, expectation)
|
13
|
+
@result = result
|
14
|
+
@expectation = expectation
|
15
|
+
super("Expected #{result.inspect} to be an instance of #{expectation}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'xquery/abstract'
|
2
|
+
|
3
|
+
module XQuery
|
4
|
+
# delegates all operations to model
|
5
|
+
class Generic < Abstract
|
6
|
+
# never used but defined in case of need
|
7
|
+
self.query_superclass = Object
|
8
|
+
|
9
|
+
# all missing methods would be delegated to query
|
10
|
+
# and processed as wrappers process them
|
11
|
+
def method_missing(name, *args, &block)
|
12
|
+
super unless respond_to_missing?(name)
|
13
|
+
_update_query(name, *args, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
# respond to all public model methods
|
17
|
+
def respond_to_missing?(name, *)
|
18
|
+
query.respond_to?(name, true)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# q object refers to self, not proxy
|
24
|
+
def q
|
25
|
+
self
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Allows you to do all query magic on a generic object
|
31
|
+
# @param model [Object] any object
|
32
|
+
# @param block [#to_proc] block where instance will be yielded
|
33
|
+
def XQuery(model, &block)
|
34
|
+
XQuery::Generic.with(model, &block)
|
35
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module XQuery
|
2
|
+
# This proxy can be used to access raw wrappers inside classes,
|
3
|
+
# inherited from XQuery::Abstract
|
4
|
+
class QueryProxy
|
5
|
+
# @param instance [Xquery::Abstract] instance to delegate methods
|
6
|
+
def initialize(instance)
|
7
|
+
self.instance = instance
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
# instance of Xquery::Abstract
|
13
|
+
attr_accessor :instance
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xquery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jelf
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -112,6 +112,7 @@ description: |
|
|
112
112
|
# XQuery
|
113
113
|
XQuery is designed to replace boring method call chains and allow to easier
|
114
114
|
convert it in a builder classes
|
115
|
+
[![Build Status](https://travis-ci.org/JelF/xquery.svg?branch=master)](https://travis-ci.org/JelF/xquery)
|
115
116
|
## Usage of `XQuery` function
|
116
117
|
`Xquery` is a shortcat to `Xquery::Generic.with`
|
117
118
|
|
@@ -197,7 +198,13 @@ email:
|
|
197
198
|
executables: []
|
198
199
|
extensions: []
|
199
200
|
extra_rdoc_files: []
|
200
|
-
files:
|
201
|
+
files:
|
202
|
+
- "/home/jelf/docs/xquery/lib/xquery.rb"
|
203
|
+
- "/home/jelf/docs/xquery/lib/xquery/abstract.rb"
|
204
|
+
- "/home/jelf/docs/xquery/lib/xquery/errors.rb"
|
205
|
+
- "/home/jelf/docs/xquery/lib/xquery/generic.rb"
|
206
|
+
- "/home/jelf/docs/xquery/lib/xquery/query_proxy.rb"
|
207
|
+
- "/home/jelf/docs/xquery/lib/xquery/version.rb"
|
201
208
|
homepage: https://github.com/JelF/xquery
|
202
209
|
licenses:
|
203
210
|
- WTFPL
|