chloroplast 0.1.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.
- checksums.yaml +7 -0
- data/lib/chloroplast.rb +52 -0
- data/spec/chloroplast_dsl_spec.rb +30 -0
- data/spec/chloroplast_spec.rb +41 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4e109e0bfc7bb41d71d5325e4733d77581071c3b
|
|
4
|
+
data.tar.gz: 4cb8c639af9d62188fd9b4c13938513782963dd2
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7fb3d29a2b1e6e76bc3fad53699250d6a58c18275da87fd551c6d63a8b339504e0129bbb5a06d1a4f60ee66240b2582b5bc0617be53ebb34e7b25d07286ebe0e
|
|
7
|
+
data.tar.gz: b60a98c083a1f5d3e37a83daefb22ee7a72d87bb0c266ea3de85956e6e3bb8b91ae4dfcc2166a5d8e6364ca6d57f93eaa49624362ea5d3f118dd6e56731fe6d4
|
data/lib/chloroplast.rb
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Chloroplast allows creating objects using a tabular format.
|
|
2
|
+
# The table's headers become objects' attribute names, and each row
|
|
3
|
+
# of the table defines one object's attribute values. Usually, you
|
|
4
|
+
# will want to use the DSL to create tables.
|
|
5
|
+
class Chloroplast
|
|
6
|
+
|
|
7
|
+
# Array of objects defined by the table
|
|
8
|
+
attr_reader :objects
|
|
9
|
+
|
|
10
|
+
# Create an empty table
|
|
11
|
+
def initialize
|
|
12
|
+
@cell_separator = nil
|
|
13
|
+
@headers = []
|
|
14
|
+
@struct = nil
|
|
15
|
+
@objects = []
|
|
16
|
+
@current_row = []
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Returns true iff the table contains no cells other than headers
|
|
20
|
+
def cells_empty?
|
|
21
|
+
@objects.empty? && @current_row.empty?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Add a table header
|
|
25
|
+
def add_header(header)
|
|
26
|
+
unless cells_empty?
|
|
27
|
+
raise RuntimeError, 'Headers cannot be added after adding cells'
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if @headers.include? header
|
|
31
|
+
raise ArgumentError, 'There cannot be duplicate headers'
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
@headers.push header
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Add a table cell, and starting a new row and creating an object if appropriate
|
|
38
|
+
def append_cell(value)
|
|
39
|
+
if @headers.empty?
|
|
40
|
+
raise RuntimeError, 'Headers must be defined before adding cells'
|
|
41
|
+
|
|
42
|
+
else
|
|
43
|
+
@struct ||= Struct.new(*@headers)
|
|
44
|
+
@current_row.push value
|
|
45
|
+
if @current_row.length == @headers.length
|
|
46
|
+
@objects.push @struct.new(*@current_row)
|
|
47
|
+
@current_row = []
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'chloroplast/dsl'
|
|
2
|
+
|
|
3
|
+
describe Chloroplast do
|
|
4
|
+
describe 'with DSL' do
|
|
5
|
+
it 'can be initialized with DSL' do
|
|
6
|
+
table = Chloroplast.new \
|
|
7
|
+
| :name | :age | :sex \
|
|
8
|
+
| 'John' | 30 | :male \
|
|
9
|
+
| 'Mary' | 40 | :female \
|
|
10
|
+
|
|
11
|
+
expect(table.objects[1].name).to be == 'Mary'
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'raises NoMethodError when any missing method beside the separator is used' do
|
|
15
|
+
table = Chloroplast.new \
|
|
16
|
+
| :foo | :bar | :baz \
|
|
17
|
+
| 1 | 2 | 3 \
|
|
18
|
+
|
|
19
|
+
expect {table.foo}.to raise_error(NoMethodError)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'raises error when header row is missing' do
|
|
23
|
+
expect do
|
|
24
|
+
Chloroplast.new \
|
|
25
|
+
| 1 | 2 | 3 \
|
|
26
|
+
| 4 | 5 | 6 \
|
|
27
|
+
end.to raise_error(/header/i)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'chloroplast'
|
|
2
|
+
|
|
3
|
+
describe Chloroplast do
|
|
4
|
+
it 'is empty when first initialized' do
|
|
5
|
+
expect(Chloroplast.new.objects).to be_empty
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it 'can be initialized' do
|
|
9
|
+
table = Chloroplast.new
|
|
10
|
+
table.add_header :name
|
|
11
|
+
table.add_header :age
|
|
12
|
+
table.add_header :sex
|
|
13
|
+
table.append_cell 'John'
|
|
14
|
+
table.append_cell 30
|
|
15
|
+
table.append_cell :male
|
|
16
|
+
table.append_cell 'Mary'
|
|
17
|
+
table.append_cell 40
|
|
18
|
+
table.append_cell :female
|
|
19
|
+
|
|
20
|
+
expect(table.objects.find {|person| person.name == 'John'}.age).to be == 30
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'raises error when adding header after cells' do
|
|
24
|
+
table = Chloroplast.new
|
|
25
|
+
table.add_header :name
|
|
26
|
+
table.append_cell 'John'
|
|
27
|
+
expect {table.add_header :age}.to raise_error(/header/i)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'raises error when adding cells before any headers' do
|
|
31
|
+
table = Chloroplast.new
|
|
32
|
+
expect {table.add_cell 'John'}.to raise_error(/header/i)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 'raises error when adding a duplicate header' do
|
|
36
|
+
table = Chloroplast.new
|
|
37
|
+
table.add_header :name
|
|
38
|
+
expect {table.add_header :name}.to raise_error(/duplicate/i)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: chloroplast
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Yaohan Chen
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-08-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Chloroplast provides a DSL for creating objects using a tabular format.
|
|
14
|
+
The table’s headers become objects’ attribute names, and each row of the table defines
|
|
15
|
+
one object’s attribute values.
|
|
16
|
+
email: yaohan.chen@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- lib/chloroplast.rb
|
|
22
|
+
- spec/chloroplast_spec.rb
|
|
23
|
+
- spec/chloroplast_dsl_spec.rb
|
|
24
|
+
homepage: https://github.com/hagabaka/chloroplast
|
|
25
|
+
licenses:
|
|
26
|
+
- GPL
|
|
27
|
+
metadata: {}
|
|
28
|
+
post_install_message:
|
|
29
|
+
rdoc_options: []
|
|
30
|
+
require_paths:
|
|
31
|
+
- lib
|
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - '>='
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - '>='
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
requirements: []
|
|
43
|
+
rubyforge_project:
|
|
44
|
+
rubygems_version: 2.0.3
|
|
45
|
+
signing_key:
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: A DSL for creating objects using a tabular format
|
|
48
|
+
test_files: []
|