boundy 0.3.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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/lib/boundy.rb +19 -0
  3. data/lib/boundy/bound.rb +65 -0
  4. data/lib/boundy/bound/comparator.rb +67 -0
  5. data/lib/boundy/bound/constrainer.rb +29 -0
  6. data/lib/boundy/bound/infinite.rb +46 -0
  7. data/lib/boundy/bound/infinite/above.rb +12 -0
  8. data/lib/boundy/bound/infinite/below.rb +13 -0
  9. data/lib/boundy/comparator.rb +19 -0
  10. data/lib/boundy/comparators.rb +28 -0
  11. data/lib/boundy/domain.rb +115 -0
  12. data/lib/boundy/domain/anterior.rb +45 -0
  13. data/lib/boundy/domain/builder.rb +31 -0
  14. data/lib/boundy/domain/comparator.rb +26 -0
  15. data/lib/boundy/domain/constrainer.rb +21 -0
  16. data/lib/boundy/domain/finite.rb +64 -0
  17. data/lib/boundy/domain/posterior.rb +43 -0
  18. data/lib/boundy/formatter.rb +9 -0
  19. data/lib/boundy/formatter/sql.rb +26 -0
  20. data/lib/boundy/formatter/sql/domain.rb +20 -0
  21. data/lib/boundy/formatter/sql/domain/plugin.rb +22 -0
  22. data/lib/boundy/formatter/sql/factory.rb +14 -0
  23. data/lib/boundy/formatter/sql/plugin.rb +38 -0
  24. data/lib/boundy/formatters/bound.rb +16 -0
  25. data/lib/boundy/formatters/domain.rb +12 -0
  26. data/lib/boundy/formatters/fixnum.rb +20 -0
  27. data/lib/boundy/formatters/sql/bound.rb +22 -0
  28. data/lib/boundy/formatters/sql/domain.rb +28 -0
  29. data/lib/boundy/formatters/sql/domain/anterior.rb +23 -0
  30. data/lib/boundy/formatters/sql/domain/finite.rb +24 -0
  31. data/lib/boundy/formatters/sql/domain/posterior.rb +23 -0
  32. data/lib/boundy/formatters/time.rb +42 -0
  33. data/lib/boundy/range/comparator.rb +38 -0
  34. data/lib/boundy/range/constrainer.rb +17 -0
  35. data/lib/boundy/time/comparator.rb +37 -0
  36. data/test/integration/boundy_test.rb +37 -0
  37. data/test/integration/domain_test.rb +18 -0
  38. data/test/integration/formatter/sql_test.rb +21 -0
  39. data/test/integration/test_helper.rb +14 -0
  40. data/test/unit/boundy/bound/comparator_test.rb +37 -0
  41. data/test/unit/boundy/bound/constrainer_test.rb +123 -0
  42. data/test/unit/boundy/bound/infinite/above_test.rb +36 -0
  43. data/test/unit/boundy/bound/infinite/below_test.rb +36 -0
  44. data/test/unit/boundy/bound/infinite_test.rb +0 -0
  45. data/test/unit/boundy/bound_test.rb +86 -0
  46. data/test/unit/boundy/comparator_test.rb +0 -0
  47. data/test/unit/boundy/domain/anterior_test.rb +103 -0
  48. data/test/unit/boundy/domain/comparator_test.rb +75 -0
  49. data/test/unit/boundy/domain/constrainer_test.rb +11 -0
  50. data/test/unit/boundy/domain/posterior_test.rb +75 -0
  51. data/test/unit/boundy/domain_test.rb +50 -0
  52. data/test/unit/boundy/formatter/sql/domain/plugin_test.rb +16 -0
  53. data/test/unit/boundy/formatter/sql/plugin_test.rb +50 -0
  54. data/test/unit/boundy/formatter/sql_test.rb +26 -0
  55. data/test/unit/boundy/formatters/bound_test.rb +43 -0
  56. data/test/unit/boundy/formatters/domain_test.rb +12 -0
  57. data/test/unit/boundy/formatters/sql/domain/anterior_test.rb +29 -0
  58. data/test/unit/boundy/formatters/sql/domain/finite_test.rb +35 -0
  59. data/test/unit/boundy/formatters/sql/domain/posterior_test.rb +30 -0
  60. data/test/unit/boundy/formatters/time_test.rb +27 -0
  61. data/test/unit/boundy/range/comparator_test.rb +44 -0
  62. data/test/unit/boundy/range/constrainer_test.rb +11 -0
  63. data/test/unit/boundy/time/comparator_test.rb +40 -0
  64. data/test/unit/test_helper.rb +14 -0
  65. metadata +154 -0
@@ -0,0 +1,24 @@
1
+ require 'boundy/formatter/sql/domain/plugin'
2
+ require 'boundy/domain/finite'
3
+
4
+ module Boundy::Formatters::Sql
5
+ class Domain
6
+ class Finite
7
+ def self.type
8
+ Boundy::Domain::Finite
9
+ end
10
+
11
+ include Boundy::Formatter::Sql::Domain::Plugin
12
+
13
+ def initialize(domain, name)
14
+ @name = name
15
+ @from_formatter = Boundy::Formatter::Sql.new(domain.from, name)
16
+ @to_formatter = Boundy::Formatter::Sql.new(domain.to, name)
17
+ end
18
+
19
+ def to_s
20
+ "#{@name} >= '#{@from_formatter.to_s}' AND #{@name} <= '#{@to_formatter.to_s}'"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ require 'boundy/formatter/sql/domain/plugin'
2
+ require 'boundy/domain/posterior'
3
+
4
+ module Boundy::Formatters::Sql
5
+ class Domain
6
+ class Posterior
7
+ def self.type
8
+ Boundy::Domain::Posterior
9
+ end
10
+
11
+ include Boundy::Formatter::Sql::Domain::Plugin
12
+
13
+ def initialize(domain, name)
14
+ @name = name
15
+ @formatter = Boundy::Formatter::Sql.new(domain.to, name)
16
+ end
17
+
18
+ def to_s
19
+ "#{@name} <= '#{@formatter.to_s}'"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,42 @@
1
+ require 'boundy/formatter/sql/plugin'
2
+
3
+ require 'boundy/formatters/bound'
4
+ require 'boundy/formatters/time'
5
+
6
+ module Boundy
7
+ module Formatters
8
+ class Time
9
+ class Factory
10
+ def initialize(format)
11
+ @format = format
12
+ end
13
+
14
+ def build(time, name)
15
+ Boundy::Formatters::Time.new(@format, time)
16
+ end
17
+ end
18
+
19
+ def self.type
20
+ ::Time
21
+ end
22
+
23
+
24
+ class << self
25
+ def factory
26
+ @factory = Factory.new("%Y-%m-%d %H:%M:%S")
27
+ end
28
+ attr_writer :factory
29
+ end
30
+
31
+ include Boundy::Formatter::Sql::Plugin
32
+
33
+ def initialize(format, time)
34
+ @formatted = time.strftime(format)
35
+ end
36
+
37
+ def to_s
38
+ @formatted
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,38 @@
1
+ require 'boundy/comparator'
2
+
3
+ module Boundy
4
+ class Range
5
+ class Comparator
6
+ def self.type
7
+ ::Range
8
+ end
9
+
10
+ include Boundy::Comparator
11
+
12
+ def initialize(domain, range)
13
+ if domain.nil?
14
+ raise
15
+ end
16
+
17
+ if range.nil?
18
+ raise
19
+ end
20
+
21
+ other = Boundy::Domain.new(range.begin, range.end)
22
+ @comparator = Boundy::Domain::Comparator.new(domain, other)
23
+ end
24
+
25
+ def after?
26
+ @comparator.after?
27
+ end
28
+
29
+ def before?
30
+ @comparator.before?
31
+ end
32
+
33
+ def within?
34
+ @comparator.within?
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,17 @@
1
+ require 'boundy/domain'
2
+ require 'boundy/domain/constrainer'
3
+
4
+ module Boundy
5
+ class Range
6
+ class Constrainer
7
+ def initialize(domain, range)
8
+ other = Boundy::Domain.new(range.begin, range.end)
9
+ @constrainer = Boundy::Domain::Constrainer.new(domain, other)
10
+ end
11
+
12
+ def constrain
13
+ @constrainer.constrain
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,37 @@
1
+ require 'boundy/comparator'
2
+
3
+ module Boundy
4
+ class Time
5
+ class Comparator
6
+ def self.types
7
+ [::Time, ActiveSupport::TimeWithZone]
8
+ end
9
+
10
+ include Boundy::Comparator
11
+
12
+ def initialize(bound, time)
13
+ if bound.nil?
14
+ raise
15
+ end
16
+
17
+ if time.nil?
18
+ raise
19
+ end
20
+ other = Boundy::Bound.new(time)
21
+ @comparator = Boundy::Bound::Comparator.new(bound, other)
22
+ end
23
+
24
+ def after?
25
+ @comparator.after?
26
+ end
27
+
28
+ def before?
29
+ @comparator.before?
30
+ end
31
+
32
+ def within?
33
+ @comparator.within?
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ require 'boundy/bound'
4
+ require 'boundy/domain'
5
+
6
+ require 'boundy/formatters/sql/bound'
7
+ require 'boundy/formatters/time'
8
+ require 'boundy/formatter/sql'
9
+
10
+ class Boundy::Test < Test::Unit::TestCase
11
+ test "foo" do
12
+ now = Time.new(2013,9,1)
13
+
14
+ from = Boundy::Bound.new(now - 5.day)
15
+ to = Boundy::Bound.new(now - 1.day)
16
+
17
+ domain = Boundy::Domain.new(from,to)
18
+
19
+ formatter = Boundy::Formatter::Sql.new(domain, "foo")
20
+
21
+ assert_equal "foo >= '2013-08-27 00:00:00' AND foo <= '2013-08-31 00:00:00'", formatter.to_s
22
+ end
23
+
24
+ test 'bar' do
25
+
26
+ now = Time.new(2013,9,1)
27
+
28
+ from = Boundy::Bound.new(now - 5.day)
29
+ to = Boundy::Bound.new(now - 1.day)
30
+
31
+ domain = Boundy::Domain.new(from,to)
32
+
33
+ other_from = Boundy::Bound.new(now - 2.day)
34
+ other_domain = Boundy::Domain::Posterior.new(other_from)
35
+
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ require 'boundy/bound'
4
+ require 'boundy/domain'
5
+
6
+ class Boundy::Domain::Test < Test::Unit::TestCase
7
+ test "domain" do
8
+ now = Time.new(2013,9,1)
9
+
10
+ from = Boundy::Bound.new(now - 5.day)
11
+ to = Boundy::Bound.new(now - 1.day)
12
+
13
+ domain = Boundy::Domain.new(from,to)
14
+
15
+ other_from = Boundy::Bound.new(now - 2.day)
16
+ other_domain = Boundy::Domain::Posterior.new(other_from)
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ require 'boundy/bound'
4
+ require 'boundy/domain'
5
+
6
+ require 'boundy/formatters/bound'
7
+ require 'boundy/formatters/fixnum'
8
+ require 'boundy/formatters/time'
9
+ require 'boundy/formatter/sql'
10
+
11
+ class Boundy::Formatter::Sql::Test < Test::Unit::TestCase
12
+ test "foo" do
13
+ lower = Boundy::Bound.new(1)
14
+ upper = Boundy::Bound.new(10)
15
+ domain = Boundy::Domain.new(lower, upper)
16
+
17
+ formatter = Boundy::Formatter::Sql.new(domain, "domain")
18
+
19
+ assert_equal formatter.to_s, "domain >= '1' AND domain <= '10'"
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ $:.push('lib')
2
+
3
+ if ENV["ENABLE_SIMPLE_COV"]
4
+ require 'simplecov'
5
+ # require File.expand_path('../simplecov_helper', __FILE__)
6
+ SimpleCov.start do
7
+ add_filter "test"
8
+ add_group 'API', "lib"
9
+ end
10
+ end
11
+
12
+ require 'active_support/time'
13
+ require 'test/unit'
14
+ require 'mocha/setup'
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+ require 'boundy/bound/comparator'
4
+
5
+ class Boundy::Bound::ComparatorTest < Test::Unit::TestCase
6
+ time = 5.days.ago
7
+ bound = Boundy::Bound.new(time)
8
+
9
+ bounds = [
10
+ {:truthiness => :before?, :time => time - 2.days},
11
+ {:truthiness => :within?, :time => time},
12
+ {:truthiness => :after?, :time => time + 2.days}
13
+ ]
14
+
15
+ methods = [:before?, :after?, :within?]
16
+
17
+ cases = bounds.product(methods).map do |p|
18
+ c = p[0]
19
+ m = p[1]
20
+ t = c[:truthiness]
21
+
22
+ expected = (c[:truthiness] == m)
23
+ {:time => c[:time],
24
+ :method => m,
25
+ :truthiness => t,
26
+ :expected => expected
27
+ }
28
+ end
29
+
30
+ cases.each do |c|
31
+ test "#{c[:method]}_#{c[:truthiness]}" do
32
+ comp = Boundy::Bound::Comparator.new(bound, Boundy::Bound.new(c[:time]))
33
+
34
+ assert_equal c[:expected], comp.method(c[:method]).call
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,123 @@
1
+ require File.expand_path('../../../test_helper', __FILE__)
2
+
3
+
4
+ require 'boundy/bound'
5
+ require 'boundy/bound/constrainer'
6
+ require 'boundy/bound/infinite'
7
+
8
+ class Boundy::Bound::ConstrainerTest < Test::Unit::TestCase
9
+ setup do
10
+ @now = Time.now
11
+
12
+ @later = @now - 2.days
13
+ @earlier = @now - 5.days
14
+ end
15
+
16
+ test '#max right finite' do
17
+ later = Boundy::Bound.new(@later)
18
+ earlier = Boundy::Bound.new(@earlier)
19
+
20
+ result = Boundy::Bound::Constrainer.new(earlier, later).max
21
+
22
+ assert_equal later, result
23
+ end
24
+
25
+ test '#max left finite' do
26
+ later = Boundy::Bound.new(@later)
27
+ earlier =Boundy::Bound.new(@earlier)
28
+
29
+ result = Boundy::Bound::Constrainer.new(later, earlier).max
30
+
31
+ assert_equal later, result
32
+ end
33
+
34
+ test '#max right infinite below' do
35
+ later = Boundy::Bound.new(@earlier)
36
+ earlier = Boundy::Bound::Infinite::Below.new
37
+
38
+ result = Boundy::Bound::Constrainer.new(earlier,later).max
39
+
40
+ assert_equal later, result
41
+ end
42
+
43
+ test '#max left infinite below' do
44
+ later = Boundy::Bound.new(@earlier)
45
+ earlier = Boundy::Bound::Infinite::Below.new
46
+
47
+ result = Boundy::Bound::Constrainer.new(later, earlier).max
48
+
49
+ assert_equal later, result
50
+ end
51
+
52
+ test '#max left infinite above' do
53
+ earlier = Boundy::Bound.new(@earlier)
54
+ later = Boundy::Bound::Infinite::Above.new
55
+
56
+ result = Boundy::Bound::Constrainer.new(earlier,later).max
57
+
58
+ assert_equal later, result
59
+ end
60
+
61
+ test '#max right infinite above' do
62
+ earlier = Boundy::Bound.new(@earlier)
63
+ later = Boundy::Bound::Infinite::Above.new
64
+
65
+ result = Boundy::Bound::Constrainer.new(later, earlier).max
66
+
67
+ assert_equal later, result
68
+ end
69
+
70
+ test '#min right finite' do
71
+ later = Boundy::Bound.new(@later)
72
+ earlier =Boundy::Bound.new(@earlier)
73
+
74
+ result = Boundy::Bound::Constrainer.new(earlier, later).min
75
+
76
+ assert_equal earlier, result
77
+ end
78
+
79
+ test '#min left finite' do
80
+ later = Boundy::Bound.new(@later)
81
+ earlier =Boundy::Bound.new(@earlier)
82
+
83
+ result = Boundy::Bound::Constrainer.new(later, earlier).min
84
+
85
+ assert_equal earlier, result
86
+ end
87
+
88
+ test '#min right infinite below' do
89
+ later = Boundy::Bound.new(@earlier)
90
+ earlier = Boundy::Bound::Infinite::Below.new
91
+
92
+ result = Boundy::Bound::Constrainer.new(earlier,later).min
93
+
94
+ assert_equal earlier, result
95
+ end
96
+
97
+ test '#min left infinite below' do
98
+ later = Boundy::Bound.new(@earlier)
99
+ earlier = Boundy::Bound::Infinite::Below.new
100
+
101
+ result = Boundy::Bound::Constrainer.new(later, earlier).min
102
+
103
+ assert_equal earlier, result
104
+ end
105
+
106
+ test '#min left infinite above' do
107
+ earlier = Boundy::Bound.new(@earlier)
108
+ later = Boundy::Bound::Infinite::Above.new
109
+
110
+ result = Boundy::Bound::Constrainer.new(earlier,later).min
111
+
112
+ assert_equal earlier, result
113
+ end
114
+
115
+ test '#min right infinite above' do
116
+ earlier = Boundy::Bound.new(@earlier)
117
+ later = Boundy::Bound::Infinite::Above.new
118
+
119
+ result = Boundy::Bound::Constrainer.new(later, earlier).min
120
+
121
+ assert_equal earlier, result
122
+ end
123
+ end