alf-core 0.13.1 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (200) hide show
  1. data/CHANGELOG.md +77 -0
  2. data/examples/operators/page.alf +4 -0
  3. data/lib/alf-adapter-fs/alf/adapter/folder.rb +4 -0
  4. data/lib/alf-adapter-fs/alf/adapter/folder/connection.rb +3 -2
  5. data/lib/alf-adapter/alf/adapter/connection.rb +1 -1
  6. data/lib/alf-algebra/alf/algebra/operand.rb +2 -2
  7. data/lib/alf-algebra/alf/algebra/operand/fake.rb +3 -3
  8. data/lib/alf-algebra/alf/algebra/operand/named.rb +1 -1
  9. data/lib/alf-algebra/alf/algebra/operand/proxy.rb +14 -0
  10. data/lib/alf-algebra/alf/algebra/operator.rb +4 -1
  11. data/lib/alf-algebra/alf/algebra/operator/clip.rb +7 -4
  12. data/lib/alf-algebra/alf/algebra/operator/frame.rb +22 -0
  13. data/lib/alf-algebra/alf/algebra/operator/hierarchize.rb +22 -0
  14. data/lib/alf-algebra/alf/algebra/operator/page.rb +22 -0
  15. data/lib/alf-algebra/alf/algebra/operator/project.rb +4 -9
  16. data/lib/alf-algebra/alf/algebra/operator/rank.rb +2 -2
  17. data/lib/alf-algebra/alf/algebra/support.rb +2 -0
  18. data/lib/alf-algebra/alf/algebra/support/signature.rb +2 -1
  19. data/lib/alf-algebra/alf/algebra/support/with_ordering.rb +11 -0
  20. data/lib/alf-core/ext/domain/heading_based.rb +16 -4
  21. data/lib/alf-core/facade.rb +5 -1
  22. data/lib/alf-core/version.rb +2 -2
  23. data/lib/alf-database/alf/database/connection.rb +2 -2
  24. data/lib/alf-database/alf/database/options.rb +0 -3
  25. data/lib/alf-engine/alf/engine.rb +4 -1
  26. data/lib/alf-engine/alf/engine/aggregate.rb +2 -1
  27. data/lib/alf-engine/alf/engine/autonum.rb +2 -1
  28. data/lib/alf-engine/alf/engine/clip.rb +2 -1
  29. data/lib/alf-engine/alf/engine/coerce.rb +2 -1
  30. data/lib/alf-engine/alf/engine/cog.rb +23 -0
  31. data/lib/alf-engine/alf/engine/compact.rb +3 -2
  32. data/lib/alf-engine/alf/engine/compact/set.rb +2 -1
  33. data/lib/alf-engine/alf/engine/compact/uniq.rb +2 -1
  34. data/lib/alf-engine/alf/engine/compiler.rb +60 -33
  35. data/lib/alf-engine/alf/engine/concat.rb +2 -1
  36. data/lib/alf-engine/alf/engine/defaults.rb +2 -1
  37. data/lib/alf-engine/alf/engine/filter.rb +4 -2
  38. data/lib/alf-engine/alf/engine/generator.rb +2 -1
  39. data/lib/alf-engine/alf/engine/group/hash.rb +4 -3
  40. data/lib/alf-engine/alf/engine/hierarchize.rb +87 -0
  41. data/lib/alf-engine/alf/engine/infer_heading.rb +2 -1
  42. data/lib/alf-engine/alf/engine/join.rb +5 -1
  43. data/lib/alf-engine/alf/engine/join/hash.rb +9 -2
  44. data/lib/alf-engine/alf/engine/leaf.rb +6 -1
  45. data/lib/alf-engine/alf/engine/materialize/array.rb +2 -1
  46. data/lib/alf-engine/alf/engine/materialize/hash.rb +2 -1
  47. data/lib/alf-engine/alf/engine/quota/cesure.rb +2 -1
  48. data/lib/alf-engine/alf/engine/rank/cesure.rb +2 -1
  49. data/lib/alf-engine/alf/engine/rename.rb +2 -1
  50. data/lib/alf-engine/alf/engine/semi/hash.rb +7 -1
  51. data/lib/alf-engine/alf/engine/set_attr.rb +4 -3
  52. data/lib/alf-engine/alf/engine/sort.rb +3 -2
  53. data/lib/alf-engine/alf/engine/sort/in_memory.rb +2 -1
  54. data/lib/alf-engine/alf/engine/summarize/cesure.rb +2 -1
  55. data/lib/alf-engine/alf/engine/summarize/hash.rb +3 -2
  56. data/lib/alf-engine/alf/engine/{cesure.rb → support/cesure.rb} +0 -0
  57. data/lib/alf-engine/alf/engine/take.rb +53 -0
  58. data/lib/alf-engine/alf/engine/to_array.rb +6 -5
  59. data/lib/alf-engine/alf/engine/type_safe.rb +2 -1
  60. data/lib/alf-engine/alf/engine/ungroup.rb +2 -1
  61. data/lib/alf-engine/alf/engine/unwrap.rb +2 -1
  62. data/lib/alf-engine/alf/engine/wrap.rb +2 -1
  63. data/lib/alf-io/alf/reader.rb +1 -1
  64. data/lib/alf-io/alf/reader/ruby.rb +23 -0
  65. data/lib/alf-io/alf/renderer/text.rb +22 -9
  66. data/lib/alf-lang/alf/lang/lispy.rb +41 -29
  67. data/lib/alf-lang/alf/lang/oo/algebra_methods.rb +17 -1
  68. data/lib/alf-optimizer/alf/optimizer/restrict.rb +7 -1
  69. data/lib/alf-predicate/alf/predicate.rb +11 -2
  70. data/lib/alf-relation/alf/relation.rb +21 -1
  71. data/lib/alf-relvar/alf/relvar.rb +21 -1
  72. data/lib/alf-relvar/alf/relvar/base.rb +11 -6
  73. data/lib/alf-relvar/alf/relvar/fake.rb +3 -2
  74. data/lib/alf-relvar/alf/relvar/virtual.rb +1 -2
  75. data/lib/alf-support/alf/support/bindable.rb +1 -1
  76. data/lib/alf-support/alf/support/config.rb +91 -18
  77. data/lib/alf-support/alf/support/tuple_scope.rb +8 -4
  78. data/lib/alf-types/alf/types.rb +2 -0
  79. data/lib/alf-types/alf/types/attr_list.rb +3 -1
  80. data/lib/alf-types/alf/types/heading.rb +4 -1
  81. data/lib/alf-types/alf/types/keys.rb +0 -4
  82. data/lib/alf-types/alf/types/ordering.rb +104 -18
  83. data/lib/alf-types/alf/types/selection.rb +18 -0
  84. data/lib/alf-types/alf/types/selector.rb +59 -0
  85. data/lib/alf-types/alf/types/tuple_computation.rb +2 -2
  86. data/lib/alf-types/alf/types/tuple_expression.rb +18 -2
  87. data/lib/alf-viewpoint/alf/viewpoint.rb +23 -8
  88. data/lib/alf-viewpoint/alf/viewpoint/metadata.rb +73 -0
  89. data/spec/integration/relation/test_extend.rb +20 -0
  90. data/spec/integration/relation/test_to_array.rb +1 -1
  91. data/spec/integration/test_examples.rb +18 -2
  92. data/spec/shared/a_cog.rb +36 -0
  93. data/spec/spec_helper.rb +8 -0
  94. data/spec/unit/alf-algebra/operand/test_proxy.rb +54 -0
  95. data/spec/unit/alf-algebra/operator/hierarchize/test_heading.rb +23 -0
  96. data/spec/unit/alf-algebra/operator/project/test_key_preserving.rb +3 -0
  97. data/spec/unit/alf-algebra/operator/project/test_keys.rb +54 -17
  98. data/spec/unit/alf-algebra/operator/rank/test_keys.rb +2 -2
  99. data/spec/unit/alf-algebra/operator/restrict/test_keys.rb +2 -2
  100. data/spec/unit/alf-algebra/operator/test_page.rb +30 -0
  101. data/spec/unit/alf-algebra/support/test_relational.rb +3 -0
  102. data/spec/unit/alf-algebra/support/with_ordering/test_total_ordering.rb +61 -0
  103. data/spec/unit/alf-database/connection/test_reconnect.rb +1 -1
  104. data/spec/unit/alf-database/options/test_new.rb +4 -4
  105. data/spec/unit/alf-database/shared_examples/a_facade_on_database_options.rb +1 -2
  106. data/spec/unit/alf-engine/cog/test_to_cog.rb +14 -0
  107. data/spec/unit/alf-engine/compiler/test_on_frame.rb +70 -0
  108. data/spec/unit/alf-engine/compiler/test_on_hierarchize.rb +33 -0
  109. data/spec/unit/alf-engine/compiler/test_on_page.rb +128 -0
  110. data/spec/unit/alf-engine/compiler/test_unsupported.rb +48 -0
  111. data/spec/unit/alf-engine/hierarchize/test_renamer.rb +18 -0
  112. data/spec/unit/alf-engine/test_filter.rb +43 -11
  113. data/spec/unit/alf-engine/test_hierarchize.rb +71 -0
  114. data/spec/unit/alf-engine/test_set_attr.rb +13 -0
  115. data/spec/unit/alf-engine/test_take.rb +46 -0
  116. data/spec/unit/alf-engine/test_to_array.rb +45 -28
  117. data/spec/unit/alf-io/reader/{input.rb → input.rash} +0 -0
  118. data/spec/unit/alf-io/reader/input.ruby +4 -0
  119. data/spec/unit/alf-io/reader/test_rash.rb +1 -1
  120. data/spec/unit/alf-io/reader/test_ruby.rb +25 -0
  121. data/spec/unit/alf-predicate/predicate/test_evaluate.rb +51 -13
  122. data/spec/unit/alf-relation/relation/class/test_recursive_type.rb +42 -0
  123. data/spec/unit/alf-relation/relation/test_equality.rb +19 -1
  124. data/spec/unit/alf-relation/relation/test_relops.rb +14 -2
  125. data/spec/unit/alf-relation/relation/test_to_hash.rb +31 -0
  126. data/spec/unit/alf-relvar/base/test_connection.rb +8 -2
  127. data/spec/unit/alf-relvar/base/test_delete.rb +2 -2
  128. data/spec/unit/alf-relvar/base/test_heading.rb +2 -2
  129. data/spec/unit/alf-relvar/base/test_insert.rb +3 -3
  130. data/spec/unit/alf-relvar/base/test_keys.rb +2 -2
  131. data/spec/unit/alf-relvar/base/test_lock.rb +2 -2
  132. data/spec/unit/alf-relvar/base/test_name.rb +3 -2
  133. data/spec/unit/alf-relvar/base/test_oo_lang.rb +2 -1
  134. data/spec/unit/alf-relvar/base/test_to_cog.rb +3 -3
  135. data/spec/unit/alf-relvar/base/test_to_lispy.rb +3 -2
  136. data/spec/unit/alf-relvar/base/test_to_relvar.rb +2 -1
  137. data/spec/unit/alf-relvar/base/test_to_s.rb +3 -2
  138. data/spec/unit/alf-relvar/base/test_update.rb +5 -4
  139. data/spec/unit/alf-relvar/virtual/test_connection.rb +3 -2
  140. data/spec/unit/alf-relvar/virtual/test_delete.rb +3 -4
  141. data/spec/unit/alf-relvar/virtual/test_expr.rb +3 -2
  142. data/spec/unit/alf-relvar/virtual/test_heading.rb +5 -4
  143. data/spec/unit/alf-relvar/virtual/test_insert.rb +2 -2
  144. data/spec/unit/alf-relvar/virtual/test_keys.rb +5 -4
  145. data/spec/unit/alf-relvar/virtual/test_oo_lang.rb +7 -2
  146. data/spec/unit/alf-relvar/virtual/test_to_cog.rb +4 -3
  147. data/spec/unit/alf-relvar/virtual/test_to_lispy.rb +3 -6
  148. data/spec/unit/alf-relvar/virtual/test_to_relvar.rb +2 -1
  149. data/spec/unit/alf-relvar/virtual/test_to_s.rb +3 -3
  150. data/spec/unit/alf-relvar/virtual/test_update.rb +4 -5
  151. data/spec/unit/alf-support/config/test_dup.rb +58 -0
  152. data/spec/unit/alf-support/config/test_freeze.rb +37 -0
  153. data/spec/unit/alf-support/config/test_hash_get.rb +41 -0
  154. data/spec/unit/alf-support/config/test_hash_set.rb +61 -0
  155. data/spec/unit/alf-support/config/test_new.rb +10 -0
  156. data/spec/unit/alf-support/config/test_option.rb +15 -0
  157. data/spec/unit/alf-support/config/test_option_setter.rb +72 -0
  158. data/spec/unit/alf-support/config/test_options.rb +22 -0
  159. data/spec/unit/alf-support/tuple_scope/test_hash_get.rb +20 -0
  160. data/spec/unit/alf-support/tuple_scope/test_respond_to.rb +6 -0
  161. data/spec/unit/alf-support/tuple_scope/test_to_s.rb +22 -0
  162. data/spec/unit/alf-types/keys/test_select.rb +57 -8
  163. data/spec/unit/alf-types/ordering/test_coerce.rb +46 -35
  164. data/spec/unit/alf-types/ordering/test_dive.rb +41 -0
  165. data/spec/unit/alf-types/ordering/test_hash_get.rb +20 -0
  166. data/spec/unit/alf-types/ordering/test_merge.rb +61 -0
  167. data/spec/unit/alf-types/ordering/test_reverse.rb +40 -0
  168. data/spec/unit/alf-types/ordering/test_selectors.rb +24 -0
  169. data/spec/unit/alf-types/ordering/test_sorter.rb +62 -8
  170. data/spec/unit/alf-types/ordering/test_to_attr_list.rb +16 -4
  171. data/spec/unit/alf-types/ordering/test_total.rb +56 -0
  172. data/spec/unit/alf-types/selection/test_coerce.rb +38 -0
  173. data/spec/unit/alf-types/selection/test_select.rb +34 -0
  174. data/spec/unit/alf-types/selector/test_coerce.rb +36 -0
  175. data/spec/unit/alf-types/selector/test_composite_q.rb +20 -0
  176. data/spec/unit/alf-types/selector/test_dive.rb +32 -0
  177. data/spec/unit/alf-types/selector/test_select.rb +40 -0
  178. data/spec/unit/alf-types/selector/test_simple_q.rb +20 -0
  179. data/spec/unit/alf-types/selector/test_to_a.rb +20 -0
  180. data/spec/unit/alf-types/selector/test_to_lispy.rb +20 -0
  181. data/spec/unit/alf-types/selector/test_to_ruby_literal.rb +20 -0
  182. data/spec/unit/alf-types/tuple_expression/test_evaluate.rb +38 -0
  183. data/spec/unit/alf-viewpoint/metadata/test_add_members.rb +38 -0
  184. data/spec/unit/alf-viewpoint/metadata/test_all_members.rb +33 -0
  185. data/spec/unit/alf-viewpoint/metadata/test_depends.rb +50 -0
  186. data/spec/unit/alf-viewpoint/metadata/test_dup.rb +32 -0
  187. data/spec/unit/alf-viewpoint/metadata/test_expand.rb +31 -0
  188. data/spec/unit/alf-viewpoint/metadata/test_expects.rb +38 -0
  189. data/spec/unit/alf-viewpoint/metadata/test_initialize.rb +40 -0
  190. data/spec/unit/alf-viewpoint/metadata/test_to_module.rb +37 -0
  191. data/spec/unit/alf-viewpoint/test_depends.rb +18 -0
  192. data/spec/unit/alf-viewpoint/test_expects.rb +18 -0
  193. data/spec/unit/alf-viewpoint/test_members.rb +33 -0
  194. data/spec/unit/alf-viewpoint/test_metadata.rb +27 -0
  195. data/spec/unit/alf-viewpoint/test_native_const.rb +6 -0
  196. metadata +125 -14
  197. data/spec/unit/alf-database/options/test_default_viewpoint.rb +0 -25
  198. data/spec/unit/alf-types/keys/test_compact.rb +0 -21
  199. data/spec/unit/alf-types/ordering/test_plus.rb +0 -27
  200. data/spec/unit/alf-viewpoint/test_namespace.rb +0 -23
@@ -9,7 +9,8 @@ module Alf
9
9
  # @return [Enumerable] The operand
10
10
  attr_reader :operand
11
11
 
12
- def initialize(operand)
12
+ def initialize(operand, expr = nil)
13
+ super(expr)
13
14
  @operand = operand
14
15
  end
15
16
 
@@ -1,3 +1,7 @@
1
- module Alf::Engine::Join
1
+ module Alf
2
+ module Engine
3
+ module Join
4
+ end
5
+ end
2
6
  end
3
7
  require_relative 'join/hash'
@@ -4,6 +4,7 @@ module Alf
4
4
  # Provides hash-based join.
5
5
  #
6
6
  class Join::Hash
7
+ include Join
7
8
  include Cog
8
9
 
9
10
  # @return [Enumerable] The left operand
@@ -13,18 +14,24 @@ module Alf
13
14
  attr_reader :right
14
15
 
15
16
  # Creates a Join::Hash instance
16
- def initialize(left, right)
17
+ def initialize(left, right, expr = nil)
18
+ super(expr)
17
19
  @left = left
18
20
  @right = right
19
21
  end
20
22
 
23
+ # Returns left and right operands in an array
24
+ def operands
25
+ [ left, right ]
26
+ end
27
+
21
28
  # (see Cog#each)
22
29
  def _each(&block)
23
30
  index = nil
24
31
  left.each do |left_tuple|
25
32
  index ||= Materialize::Hash.new(right, lambda{|t|
26
33
  AttrList.new(left_tuple.keys & t.keys)
27
- }, false).prepare
34
+ }, false, expr).prepare
28
35
  index[left_tuple, true].each do |right_tuple|
29
36
  yield left_tuple.merge(right_tuple)
30
37
  end
@@ -10,7 +10,8 @@ module Alf
10
10
  attr_reader :operand
11
11
 
12
12
  # Creates a Concat instance
13
- def initialize(operand)
13
+ def initialize(operand, expr = nil)
14
+ super(expr)
14
15
  @operand = operand
15
16
  end
16
17
 
@@ -19,6 +20,10 @@ module Alf
19
20
  operand.each(&block)
20
21
  end
21
22
 
23
+ def to_s
24
+ "Engine::Leaf(#{operand})"
25
+ end
26
+
22
27
  end # class Leaf
23
28
  end # module Engine
24
29
  end # module Alf
@@ -37,7 +37,8 @@ module Alf
37
37
  attr_reader :ordering
38
38
 
39
39
  # Creates a Materialize::Array instance
40
- def initialize(operand, ordering = nil)
40
+ def initialize(operand, ordering = nil, expr = nil)
41
+ super(expr)
41
42
  @operand = operand
42
43
  @ordering = ordering
43
44
  @materialized = nil
@@ -50,7 +50,8 @@ module Alf
50
50
  attr_reader :allbut
51
51
 
52
52
  # Creates a Materialize::Hash instance
53
- def initialize(operand, key, allbut = false)
53
+ def initialize(operand, key, allbut = false, expr = nil)
54
+ super(expr)
54
55
  @operand = operand
55
56
  @key = key
56
57
  @allbut = allbut
@@ -13,7 +13,8 @@ module Alf
13
13
  attr_reader :summarization
14
14
 
15
15
  # Creates an Quota::Cesure instance
16
- def initialize(operand, by, summarization)
16
+ def initialize(operand, by, summarization, expr = nil)
17
+ super(expr)
17
18
  @operand = operand
18
19
  @by = by.to_attr_list
19
20
  @summarization = summarization
@@ -13,7 +13,8 @@ module Alf
13
13
  attr_reader :as
14
14
 
15
15
  # Creates an Rank::Cesure instance
16
- def initialize(operand, by, as)
16
+ def initialize(operand, by, as, expr = nil)
17
+ super(expr)
17
18
  @operand = operand
18
19
  @by = by.to_attr_list
19
20
  @as = as
@@ -23,7 +23,8 @@ module Alf
23
23
  attr_reader :renaming
24
24
 
25
25
  # Creates a Rename instance
26
- def initialize(operand, renaming)
26
+ def initialize(operand, renaming, expr = nil)
27
+ super(expr)
27
28
  @operand = operand
28
29
  @renaming = renaming
29
30
  end
@@ -16,12 +16,18 @@ module Alf
16
16
  attr_reader :predicate
17
17
 
18
18
  # Creates a Semi::Hash instance
19
- def initialize(left, right, predicate)
19
+ def initialize(left, right, predicate, expr = nil)
20
+ super(expr)
20
21
  @left = left
21
22
  @right = right
22
23
  @predicate = predicate
23
24
  end
24
25
 
26
+ # Returns left and right operands in an array
27
+ def operands
28
+ [ left, right ]
29
+ end
30
+
25
31
  # (see Cog#each)
26
32
  def _each
27
33
  index = nil
@@ -28,7 +28,8 @@ module Alf
28
28
  attr_reader :computation
29
29
 
30
30
  # Creates a SetAttr instance
31
- def initialize(operand, computation)
31
+ def initialize(operand, computation, expr = nil)
32
+ super(expr)
32
33
  @operand = operand
33
34
  @computation = computation
34
35
  end
@@ -37,8 +38,8 @@ module Alf
37
38
  def _each
38
39
  scope = tuple_scope
39
40
  operand.each do |tuple|
40
- computed = @computation.evaluate(scope.__set_tuple(tuple))
41
- yield tuple.merge(computed)
41
+ t, c = scope.__set_tuple(tuple), expr && expr.connection
42
+ yield tuple.merge(@computation.evaluate(t, c))
42
43
  end
43
44
  end
44
45
 
@@ -13,14 +13,15 @@ module Alf
13
13
  attr_reader :ordering
14
14
 
15
15
  # Creates an Autonum instance
16
- def initialize(operand, ordering)
16
+ def initialize(operand, ordering, expr = nil)
17
+ super(expr)
17
18
  @operand = operand
18
19
  @ordering = ordering
19
20
  end
20
21
 
21
22
  # (see Cog#each)
22
23
  def _each(&block)
23
- Sort::InMemory.new(operand, ordering).each(&block)
24
+ Sort::InMemory.new(operand, ordering, expr).each(&block)
24
25
  end
25
26
 
26
27
  end # class Sort
@@ -25,7 +25,8 @@ module Alf
25
25
  attr_reader :ordering
26
26
 
27
27
  # Creates an Autonum instance
28
- def initialize(operand, ordering)
28
+ def initialize(operand, ordering, expr = nil)
29
+ super(expr)
29
30
  @operand = operand
30
31
  @ordering = ordering
31
32
  end
@@ -16,7 +16,8 @@ module Alf
16
16
  attr_reader :allbut
17
17
 
18
18
  # Creates an Summarize::Cesure instance
19
- def initialize(operand, by, summarization, allbut)
19
+ def initialize(operand, by, summarization, allbut, expr = nil)
20
+ super(expr)
20
21
  @operand = operand
21
22
  @by = by
22
23
  @summarization = summarization
@@ -16,7 +16,8 @@ module Alf
16
16
  attr_reader :allbut
17
17
 
18
18
  # Creates an Summarize::Hash instance
19
- def initialize(operand, by, summarization, allbut)
19
+ def initialize(operand, by, summarization, allbut, expr = nil)
20
+ super(expr)
20
21
  @operand = operand
21
22
  @by = by
22
23
  @summarization = summarization
@@ -25,7 +26,7 @@ module Alf
25
26
 
26
27
  # (see Cog#each)
27
28
  def _each
28
- index = Materialize::Hash.new(operand, by, allbut)
29
+ index = Materialize::Hash.new(operand, by, allbut, expr)
29
30
  index.each_pair do |k, v|
30
31
  yield k.merge(summarization.summarize(v))
31
32
  end
@@ -0,0 +1,53 @@
1
+ module Alf
2
+ module Engine
3
+ #
4
+ # Take only a frame of the input relation according to an (offset, limit)
5
+ # parameter.
6
+ #
7
+ # Example:
8
+ #
9
+ # res = [
10
+ # {:name => "Jones", :price => 12.0, :id => 1},
11
+ # {:name => "Smith", :price => 10.0, :id => 2},
12
+ # {:name => "Blake", :price => 14.0, :id => 3}
13
+ # {:name => "Adams", :price => 13.0, :id => 4}
14
+ # ]
15
+ # Aggregate.new(res, 1, 2).to_a
16
+ # # => [
17
+ # # {:name => "Smith", :price => 10.0, :id => 2},
18
+ # # {:name => "Blake", :price => 14.0, :id => 3}
19
+ # # ]
20
+ #
21
+ class Take
22
+ include Cog
23
+
24
+ # @return [Enumerable] The operand
25
+ attr_reader :operand
26
+
27
+ # @return [Integer] The frame offset
28
+ attr_reader :offset
29
+
30
+ # @return [Integer] The frame limit
31
+ attr_reader :limit
32
+
33
+ # Creates a Take instance
34
+ def initialize(operand, offset, limit, expr = nil)
35
+ super(expr)
36
+ @operand = operand
37
+ @offset = offset
38
+ @limit = limit
39
+ end
40
+
41
+ # (see Cog#each)
42
+ def _each
43
+ i = -1
44
+ operand.each do |tuple|
45
+ next if (i += 1) < offset
46
+ return if i >= offset+limit
47
+ yield(tuple)
48
+ end
49
+ end
50
+
51
+ end # class Take
52
+ end # module Engine
53
+ end # module Alf
@@ -26,7 +26,8 @@ module Alf
26
26
  attr_reader :ordering
27
27
 
28
28
  # Creates an ToArray instance
29
- def initialize(operand, ordering)
29
+ def initialize(operand, ordering, expr = nil)
30
+ super(expr)
30
31
  @operand = operand
31
32
  @ordering = ordering
32
33
  end
@@ -43,15 +44,15 @@ module Alf
43
44
  def recurse(tuple)
44
45
  case tuple
45
46
  when Hash
46
- Hash[tuple.map{|k,v| [k, reorder(v)] }]
47
+ Hash[tuple.map{|k,v| [ k, reorder(k,v) ] }]
47
48
  when Tuple
48
- tuple.remap{|k,v| reorder(v) }
49
+ tuple.remap{|k,v| reorder(k, v) }
49
50
  end
50
51
  end
51
52
 
52
- def reorder(value)
53
+ def reorder(key, value)
53
54
  if RelationLike===value
54
- ToArray.new(value, ordering).to_a
55
+ ToArray.new(value, ordering.dive(key)).to_a
55
56
  else
56
57
  value
57
58
  end
@@ -10,7 +10,8 @@ module Alf
10
10
  attr_reader :checker
11
11
 
12
12
  # Creates an Coerce instance
13
- def initialize(operand, checker)
13
+ def initialize(operand, checker, expr = nil)
14
+ super(expr)
14
15
  @operand = operand
15
16
  @checker = checker
16
17
  end
@@ -10,7 +10,8 @@ module Alf
10
10
  attr_reader :attribute
11
11
 
12
12
  # Creates a Ungroup instance
13
- def initialize(operand, attribute)
13
+ def initialize(operand, attribute, expr = nil)
14
+ super(expr)
14
15
  @operand = operand
15
16
  @attribute = attribute
16
17
  end
@@ -13,7 +13,8 @@ module Alf
13
13
  attr_reader :attribute
14
14
 
15
15
  # Creates a SetAttr instance
16
- def initialize(operand, attribute)
16
+ def initialize(operand, attribute, expr = nil)
17
+ super(expr)
17
18
  @operand = operand
18
19
  @attribute = attribute
19
20
  end
@@ -19,7 +19,8 @@ module Alf
19
19
  attr_reader :allbut
20
20
 
21
21
  # Creates a SetAttr instance
22
- def initialize(operand, attributes, as, allbut)
22
+ def initialize(operand, attributes, as, allbut, expr = nil)
23
+ super(expr)
23
24
  @operand = operand
24
25
  @attributes = attributes
25
26
  @as = as
@@ -20,7 +20,6 @@ module Alf
20
20
  # r = Reader.foo([a path or a IO object])
21
21
  #
22
22
  class Reader
23
- include Alf::Engine::Cog
24
23
  include Enumerable
25
24
 
26
25
  class << self
@@ -187,3 +186,4 @@ end # module Alf
187
186
  require_relative 'reader/rash'
188
187
  require_relative 'reader/csv'
189
188
  require_relative 'reader/json'
189
+ require_relative 'reader/ruby'
@@ -0,0 +1,23 @@
1
+ module Alf
2
+ class Reader
3
+ #
4
+ # Specialization of the Reader contract for .rb files.
5
+ #
6
+ # A .rb file/stream contains some ruby code that evaluates to a Relation-like
7
+ # object, typically an Array of hashes.
8
+ #
9
+ class Ruby < Reader
10
+
11
+ def self.mime_type
12
+ nil
13
+ end
14
+
15
+ def each(&bl)
16
+ return to_enum unless block_given?
17
+ ::Kernel.eval(input_text).each(&bl)
18
+ end
19
+
20
+ Reader.register(:ruby, [".rb", ".ruby"], self)
21
+ end # class Ruby
22
+ end # class Reader
23
+ end # module Alf
@@ -124,20 +124,33 @@ module Alf
124
124
  end
125
125
 
126
126
  def sep
127
- @sep ||= '+-' << sizes.map{|s| '-' * s}.join('-+-') << '-+' << "\n"
127
+ @sep ||= '+-' << sizes.map{|s| '-' * s}.join('-+-') << '-+'
128
+ end
129
+
130
+ def each_line(pretty = options[:pretty])
131
+ if pretty
132
+ trim = options[:trim_at]
133
+ each_line(false) do |line|
134
+ yield(line[0..trim])
135
+ end
136
+ else
137
+ yield(sep)
138
+ yield(header.rendering_lines(sizes).first)
139
+ yield(sep)
140
+ rows.each do |row|
141
+ row.rendering_lines(sizes).each do |line|
142
+ yield(line)
143
+ end
144
+ end
145
+ yield(sep)
146
+ end
128
147
  end
129
148
 
130
149
  def each
131
150
  return to_enum unless block_given?
132
- yield(sep)
133
- yield header.rendering_lines(sizes).first << "\n"
134
- yield(sep)
135
- rows.each do |row|
136
- row.rendering_lines(sizes).each do |line|
137
- yield(line << "\n")
138
- end
151
+ each_line do |line|
152
+ yield(line.strip << "\n")
139
153
  end
140
- yield(sep)
141
154
  end
142
155
 
143
156
  def to_s
@@ -1,43 +1,55 @@
1
1
  module Alf
2
2
  module Lang
3
- class Lispy < Support::Scope
4
-
5
- module OwnMethods
6
-
7
- def parse(expr = nil, path = nil, line = nil, &block)
8
- if (expr && block) || (expr.nil? and block.nil?)
9
- raise ArgumentError, "Either `expr` or `block` should be specified"
10
- end
11
- expr = evaluate(expr, path, line, &block) if block or expr.is_a?(String)
12
- expr = __send__(expr) if expr.is_a?(Symbol)
13
- expr = expr.bind(connection) if expr.is_a?(Support::Bindable) && connection
14
- expr
3
+ class Lispy
4
+ include Functional
5
+ include Predicates
6
+
7
+ # Creates a language instance
8
+ def initialize(helpers = [ ], connection = nil)
9
+ @connection = connection
10
+ @extensions = helpers
11
+ helpers.each do |helper|
12
+ helper.send(:extend_object, self)
15
13
  end
14
+ end
16
15
 
17
- attr_reader :connection
16
+ attr_reader :connection
18
17
 
19
- def connection!
20
- connection.tap{|c| ::Kernel.raise(UnboundError, "#{self} not bound") unless c }
21
- end
18
+ def connection!
19
+ connection.tap{|c| ::Kernel.raise(UnboundError, "#{self} not bound") unless c }
20
+ end
21
+
22
+ def evaluate(expr = nil, path=nil, line=nil, &bl)
23
+ return instance_exec(&bl) if bl
24
+ ::Kernel.eval expr, __eval_binding, *[path, line].compact
25
+ end
22
26
 
23
- def to_s
24
- mods = @extensions
25
- .map(&:name)
26
- .compact
27
- .reject{|x| x =~ /^Alf::|OwnMethods/}
28
- .join(',')
29
- "Lispy(#{mods})"
27
+ def parse(expr = nil, path = nil, line = nil, &block)
28
+ if (expr && block) || (expr.nil? and block.nil?)
29
+ raise ArgumentError, "Either `expr` or `block` should be specified"
30
30
  end
31
- alias_method :inspect, :to_s
31
+ expr = evaluate(expr, path, line, &block) if block or expr.is_a?(String)
32
+ expr = __send__(expr) if expr.is_a?(Symbol)
33
+ expr = expr.bind(connection) if expr.is_a?(Support::Bindable) && connection
34
+ expr
35
+ end
32
36
 
33
- end # OwnMethods
37
+ def to_s
38
+ "Lispy(#{@extensions.map(&:name).compact.join(',')})"
39
+ end
34
40
 
35
- # Creates a language instance
36
- def initialize(helpers = [ ], connection = nil)
37
- @connection = connection
38
- super [ OwnMethods, Functional, Predicates ] + helpers
41
+ def inspect
42
+ "Lispy(#{@extensions.inspect})"
39
43
  end
40
44
 
45
+ private
46
+
47
+ def __eval_binding
48
+ ::Kernel.binding
49
+ end
50
+
51
+ public
52
+
41
53
  # Resolve DUM and DEE in ruby 1.9.2 context
42
54
  def self.const_missing(name)
43
55
  case name.to_s