transaction_ql 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d6e961a003ba2ada81d14b0fcc1169c2cd6c192c
4
+ data.tar.gz: f54b9b09e66487d011683df78731038b44c649f3
5
+ SHA512:
6
+ metadata.gz: 400647595a9c31b89f212c6c07aab9c323882067fb5db3e049a668fcfcb5f8721a44356ae4e4a2ac3bdd5b1a519fa88263e4703e3ba406db0b9b7f2ccef0365b
7
+ data.tar.gz: fd9c6e594a3628478bfd2dba2abbaab7587f88e196eb02b554e020ab223a7bcdde8fbe82b4dd87e01ce82b547ab5b23950d70a3ddd09dbd013ecbdc3b17980f2
@@ -0,0 +1,2 @@
1
+ require 'transaction_ql/filter'
2
+ require 'transaction_ql/expressions'
@@ -0,0 +1,93 @@
1
+ module TransactionQL
2
+ class Expression
3
+ def matches?(hash)
4
+ false
5
+ end
6
+ end
7
+
8
+ class NumericExpression < Expression
9
+ def initialize(column, other, operator)
10
+ @column = column
11
+ @other = other
12
+ @operator = operator
13
+ end
14
+
15
+ def matches?(hash)
16
+ case @other
17
+ when Numeric
18
+ hash.fetch(@column).send @operator, @other
19
+ when String
20
+ if !hash[@other].is_a? Numeric
21
+ raise "Column `#{@other}` is not numeric!"
22
+ end
23
+ hash.fetch(@column).send @operator, hash.fetch(@other)
24
+ else
25
+ raise 'Unsupported right hand type.'
26
+ end
27
+ end
28
+ end
29
+
30
+ class All < Expression
31
+ attr_reader :expressions
32
+ def initialize(expressions)
33
+ @expressions = expressions
34
+ end
35
+
36
+ def matches?(hash)
37
+ @expressions.map { |exp| exp.matches? hash }.all?
38
+ end
39
+ end
40
+
41
+ class Any < Expression
42
+ attr_reader :expressions
43
+ def initialize(expressions)
44
+ @expressions = expressions
45
+ end
46
+
47
+ def matches?(hash)
48
+ @expressions.map { |exp| exp.matches? hash }.any?
49
+ end
50
+ end
51
+
52
+ class Not < Expression
53
+ attr_reader :expressions
54
+ def initialize(expressions)
55
+ @expressions = expressions
56
+ end
57
+
58
+ def matches?(hash)
59
+ result = @expressions.map { |exp| exp.matches? hash }.any?
60
+ return !(result)
61
+ end
62
+ end
63
+
64
+ class Match < Expression
65
+ def initialize(column, regexp)
66
+ @column = column
67
+ @regexp = regexp
68
+ end
69
+
70
+ def matches?(hash)
71
+ match = @regexp.match hash.fetch(@column)
72
+ return !match.nil?
73
+ end
74
+ end
75
+
76
+ class Greater < NumericExpression
77
+ def initialize(column, other)
78
+ super column, other, :>
79
+ end
80
+ end
81
+
82
+ class Smaller < NumericExpression
83
+ def initialize(column, other)
84
+ super column, other, :<
85
+ end
86
+ end
87
+
88
+ class Equal < NumericExpression
89
+ def initialize(column, other)
90
+ super column, other, :==
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,80 @@
1
+ require 'transaction_ql/expressions'
2
+
3
+ module TransactionQL
4
+ class Filter
5
+ attr_reader :name, :query
6
+
7
+ def initialize(name, &block)
8
+ @name = name
9
+ @query = All.new []
10
+
11
+ @block_depth = 0
12
+ @last_blocks = []
13
+ @timeframe = []
14
+
15
+ instance_eval(&block)
16
+ end
17
+
18
+ def matches?(hash)
19
+ @query.matches? hash
20
+ end
21
+
22
+ def match(column, regex)
23
+ add_expression Match.new(column, regex)
24
+ end
25
+
26
+ def greater(column, other)
27
+ add_expression Greater.new(column, other)
28
+ end
29
+
30
+ def smaller(column, other)
31
+ add_expression Smaller.new(column, other)
32
+ end
33
+
34
+ def equal(column, other)
35
+ add_expression Equal.new(column, other)
36
+ end
37
+
38
+ def greater_eq(column, other)
39
+ add_expression Any.new [
40
+ Greater.new(column, other),
41
+ Equal.new(column, other)
42
+ ]
43
+ end
44
+
45
+ def smaller_eq(column, other)
46
+ add_expression Any.new [
47
+ Smaller.new(column, other),
48
+ Equal.new(column, other)
49
+ ]
50
+ end
51
+
52
+ def any(&block)
53
+ last_block = process_inner(&block)
54
+ add_expression Any.new(last_block)
55
+ end
56
+
57
+ def invert(&block)
58
+ last_block = process_inner(&block)
59
+ add_expression Not.new(last_block)
60
+ end
61
+
62
+ private
63
+
64
+ def process_inner(&block)
65
+ @last_blocks.push []
66
+ @block_depth += 1
67
+ instance_eval(&block)
68
+ @block_depth -= 1
69
+ @last_blocks.pop
70
+ end
71
+
72
+ def add_expression(expression)
73
+ if @block_depth > 0
74
+ @last_blocks[-1] << expression
75
+ else
76
+ @query.expressions << expression
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: transaction_ql
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Stan Janssen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Embedded DSL created to filter/categorise bank transactions.
14
+ email: mail@janssen.io
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/transaction_ql.rb
20
+ - lib/transaction_ql/expressions.rb
21
+ - lib/transaction_ql/filter.rb
22
+ homepage: https://github.com/janssen-io/transaction-ql
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.12
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Embedded DSL to filter bank transactions.
46
+ test_files: []