chloroplast 0.1.0 → 0.1.1

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 (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/chloroplast/dsl.rb +58 -0
  3. metadata +2 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e109e0bfc7bb41d71d5325e4733d77581071c3b
4
- data.tar.gz: 4cb8c639af9d62188fd9b4c13938513782963dd2
3
+ metadata.gz: 3512292d97c8f2d803b015249d9118497862d8ed
4
+ data.tar.gz: fb8325ac11feadd13a413d44dcd700cc9f1f53c3
5
5
  SHA512:
6
- metadata.gz: 7fb3d29a2b1e6e76bc3fad53699250d6a58c18275da87fd551c6d63a8b339504e0129bbb5a06d1a4f60ee66240b2582b5bc0617be53ebb34e7b25d07286ebe0e
7
- data.tar.gz: b60a98c083a1f5d3e37a83daefb22ee7a72d87bb0c266ea3de85956e6e3bb8b91ae4dfcc2166a5d8e6364ca6d57f93eaa49624362ea5d3f118dd6e56731fe6d4
6
+ metadata.gz: 2cdf9641c9afd5f2f40e21aaae4942d13ab39e3fea43bf597bc1a30078c99b35575123780eea5bee5323d30a3d376baae5610c1697284ea3b85a62bcb7de6aef
7
+ data.tar.gz: 81e969aca9bc30f7f25dfef9b3f6c3f939bbaefa641ff26ca19db6313f00a98f342d8e10643cff40b6028801a9c4e55e2a8cac89adbda339880e5e4bfed400ea
@@ -0,0 +1,58 @@
1
+ require 'chloroplast'
2
+
3
+ # Chloroplast DSL allows creating a table using a binary operator as table
4
+ # separator. For example,
5
+ #
6
+ # table = Chloroplast.new \
7
+ # | :name | :age | :sex \
8
+ # | 'John' | 30 | :male \
9
+ # | 'Mary' | 40 | :female \
10
+ #
11
+ # This code uses formatting and backslashes to show the row boundaries, but
12
+ # the DSL is not aware of them. Instead, the DSL requires table headers
13
+ # to be of the class Symbol, and assumes all symbols at the beginning of
14
+ # the table to be the header row. Then it places subsequent cells into
15
+ # rows based on the header row length.
16
+ class Chloroplast
17
+
18
+ # Allows using a binary operator as table cell separator
19
+ def method_missing(name, *arguments, &block)
20
+ if @cell_separator
21
+ super
22
+
23
+ else
24
+ if arguments.length != 1 || block
25
+ raise SyntaxError, 'Cell separator must be binary operator'
26
+ end
27
+
28
+ value = arguments.first
29
+ singleton_class.send :alias_method, name, :cell_separator
30
+ @cell_separator = name
31
+ cell_separator value
32
+ end
33
+ end
34
+
35
+ # Handling of the cell separator
36
+ def cell_separator(value)
37
+ possibly_header = value.is_a? Symbol
38
+
39
+ if @headers.empty?
40
+ unless possibly_header
41
+ raise SyntaxError, 'The table must start with a header row of symbols'
42
+ end
43
+ add_header value
44
+
45
+ elsif cells_empty?
46
+ if possibly_header
47
+ add_header value
48
+ else
49
+ append_cell value
50
+ end
51
+ else
52
+ append_cell value
53
+ end
54
+
55
+ self
56
+ end
57
+ end
58
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chloroplast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaohan Chen
@@ -18,6 +18,7 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - lib/chloroplast/dsl.rb
21
22
  - lib/chloroplast.rb
22
23
  - spec/chloroplast_spec.rb
23
24
  - spec/chloroplast_dsl_spec.rb