tablecloth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/tablecloth/coercions.rb +160 -0
- data/lib/tablecloth/table_definition.rb +32 -0
- data/lib/tablecloth/transformer.rb +35 -0
- data/lib/tablecloth/version.rb +3 -0
- data/lib/tablecloth.rb +9 -0
- data/tablecloth.gemspec +29 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ecb69928c14ee9cd7458b14224c98c49dd88a2d5
|
4
|
+
data.tar.gz: ed405a07a24b51548461db9329633482a1d3dce8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 477dff3cdc860145d65a7db27796850f3ce601f3a9a3f1c0e44a866047a8981d12da843b19af126b24e2a8d49d62f570f5a73571884a035c61bfe56c65ba73be
|
7
|
+
data.tar.gz: b55833cca6a926141afab4e39f732a8bee6344d2b63c9d81bedb23436848e88a90a0f6b170da196f0ad795d50541842e4cdf8ea2e6584fc0ee54d2591aa0ea9f
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Tim Cooper
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Tablecloth
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tablecloth`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'tablecloth'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install tablecloth
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
``` cucumber
|
26
|
+
Scenario: Something interesting involing a table transformation
|
27
|
+
Given the world functions as expected
|
28
|
+
When we have a list of orders like:
|
29
|
+
| Order ID | Transaction ID | Amount |
|
30
|
+
| 1 | 124 | $50 |
|
31
|
+
| | 456 | $100 |
|
32
|
+
| 2 | 789 | $75 |
|
33
|
+
Then something magical should happen
|
34
|
+
```
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
# register transformations
|
38
|
+
Transproc.register(:to_money) do |value, world|
|
39
|
+
Money.new(value.gsub("$", "").to_i * 100, world.currency)
|
40
|
+
end
|
41
|
+
|
42
|
+
table(/^table:Order ID,Transaction ID,Amount$/) do
|
43
|
+
column :order_id, type: :integer, from: "Order ID", retain_previous_value: true
|
44
|
+
column :transaction_id, type: :integer, from: "Transaction ID"
|
45
|
+
column :amount, type: :money, from: "Amount"
|
46
|
+
end
|
47
|
+
|
48
|
+
TableStep(/we have a list of orders like:/) do |table|
|
49
|
+
table # => [
|
50
|
+
# {order_id: 1, transaction_id: 124, amount: money(50_00)},
|
51
|
+
# {order_id: 1, transaction_id: 456, amount: money(100_00)},
|
52
|
+
# {order_id: 2, transaction_id: 789, amount: money(75_00)},
|
53
|
+
# ]
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Development
|
58
|
+
|
59
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
60
|
+
|
61
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it ( https://github.com/coop/tablecloth/fork )
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tablecloth"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
# https://github.com/solnic/transproc/blob/e2fbc12a51c8dc4fcb38d43a581134fcbedf5afa/lib/transproc/coercions.rb
|
2
|
+
|
3
|
+
require "date"
|
4
|
+
require "time"
|
5
|
+
require "bigdecimal"
|
6
|
+
require "bigdecimal/util"
|
7
|
+
|
8
|
+
module Tablecloth
|
9
|
+
# Coercion functions for common types
|
10
|
+
#
|
11
|
+
# @api public
|
12
|
+
module Coercions
|
13
|
+
extend Transproc::Functions
|
14
|
+
|
15
|
+
TRUE_VALUES = [true, 1, "1", "on", "t", "true", "y", "yes"].freeze
|
16
|
+
FALSE_VALUES = [false, 0, "0", "off", "f", "false", "n", "no"].freeze
|
17
|
+
|
18
|
+
BOOLEAN_MAP = Hash[
|
19
|
+
TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])
|
20
|
+
].freeze
|
21
|
+
|
22
|
+
# Coerce value into a string
|
23
|
+
#
|
24
|
+
# @example
|
25
|
+
# Transproc(:to_string)[1]
|
26
|
+
# # => "1"
|
27
|
+
#
|
28
|
+
# @param [Object] value The input value
|
29
|
+
#
|
30
|
+
# @return [String]
|
31
|
+
#
|
32
|
+
# @api public
|
33
|
+
def to_string(value, _world)
|
34
|
+
value.to_s
|
35
|
+
end
|
36
|
+
|
37
|
+
# Coerce value into a symbol
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# Transproc(:to_symbol)["foo"]
|
41
|
+
# # => :foo
|
42
|
+
#
|
43
|
+
# @param [Object] value The input value
|
44
|
+
#
|
45
|
+
# @return [Symbol]
|
46
|
+
#
|
47
|
+
# @api public
|
48
|
+
def to_symbol(value, _world)
|
49
|
+
value.to_sym
|
50
|
+
end
|
51
|
+
|
52
|
+
# Coerce value into a integer
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# Transproc(:to_integer)["1"]
|
56
|
+
# # => 1
|
57
|
+
#
|
58
|
+
# @param [Object] value The input value
|
59
|
+
#
|
60
|
+
# @return [Integer]
|
61
|
+
#
|
62
|
+
# @api public
|
63
|
+
def to_integer(value, _world)
|
64
|
+
value.to_i
|
65
|
+
end
|
66
|
+
|
67
|
+
# Coerce value into a float
|
68
|
+
#
|
69
|
+
# @example
|
70
|
+
# Transproc(:to_float)["1.2"]
|
71
|
+
# # => 1.2
|
72
|
+
#
|
73
|
+
# @param [Object] value The input value
|
74
|
+
#
|
75
|
+
# @return [Float]
|
76
|
+
#
|
77
|
+
# @api public
|
78
|
+
def to_float(value, _world)
|
79
|
+
value.to_f
|
80
|
+
end
|
81
|
+
|
82
|
+
# Coerce value into a decimal
|
83
|
+
#
|
84
|
+
# @example
|
85
|
+
# Transproc(:to_decimal)[1.2]
|
86
|
+
# # => #<BigDecimal:7fca32acea50,"0.12E1",18(36)>
|
87
|
+
#
|
88
|
+
# @param [Object] value The input value
|
89
|
+
#
|
90
|
+
# @return [Decimal]
|
91
|
+
#
|
92
|
+
# @api public
|
93
|
+
def to_decimal(value, _world)
|
94
|
+
value.to_d
|
95
|
+
end
|
96
|
+
|
97
|
+
# Coerce value into a boolean
|
98
|
+
#
|
99
|
+
# @example
|
100
|
+
# Transproc(:to_boolean)["true"]
|
101
|
+
# # => true
|
102
|
+
# Transproc(:to_boolean)["f"]
|
103
|
+
# # => false
|
104
|
+
#
|
105
|
+
# @param [Object] value The input value
|
106
|
+
#
|
107
|
+
# @return [TrueClass,FalseClass]
|
108
|
+
#
|
109
|
+
# @api public
|
110
|
+
def to_boolean(value, _world)
|
111
|
+
BOOLEAN_MAP.fetch(value)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Coerce value into a date
|
115
|
+
#
|
116
|
+
# @example
|
117
|
+
# Transproc(:to_date)["2015-04-14"]
|
118
|
+
# # => #<Date: 2015-04-14 ((2457127j,0s,0n),+0s,2299161j)>
|
119
|
+
#
|
120
|
+
# @param [Object] value The input value
|
121
|
+
#
|
122
|
+
# @return [Date]
|
123
|
+
#
|
124
|
+
# @api public
|
125
|
+
def to_date(value, _world)
|
126
|
+
Date.parse(value)
|
127
|
+
end
|
128
|
+
|
129
|
+
# Coerce value into a time
|
130
|
+
#
|
131
|
+
# @example
|
132
|
+
# Transproc(:to_time)["2015-04-14 12:01:45"]
|
133
|
+
# # => 2015-04-14 12:01:45 +0200
|
134
|
+
#
|
135
|
+
# @param [Object] value The input value
|
136
|
+
#
|
137
|
+
# @return [Time]
|
138
|
+
#
|
139
|
+
# @api public
|
140
|
+
def to_time(value, _world)
|
141
|
+
Time.parse(value)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Coerce value into a datetime
|
145
|
+
#
|
146
|
+
# @example
|
147
|
+
# Transproc(:to_datetime)["2015-04-14 12:01:45", nothing]
|
148
|
+
# # => #<DateTime: 2015-04-14T12:01:45+00:00 ((2457127j,43305s,0n),+0s,2299161j)>
|
149
|
+
#
|
150
|
+
# @param [Object] value The input value
|
151
|
+
# @param [World] world Cucumber world object
|
152
|
+
#
|
153
|
+
# @return [DateTime]
|
154
|
+
#
|
155
|
+
# @api public
|
156
|
+
def to_datetime(value, _world)
|
157
|
+
DateTime.parse(value)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Tablecloth
|
2
|
+
class TableDefinition
|
3
|
+
Column = Struct.new(:name, :type, :from, :retain_previous_value) do
|
4
|
+
def retain_previous_value?
|
5
|
+
retain_previous_value
|
6
|
+
end
|
7
|
+
end
|
8
|
+
UnknownColumn = Class.new(StandardError)
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@columns = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def column(name, type:, from: name, retain_previous_value: false)
|
15
|
+
@columns[name] = Column.new(name, type, from, retain_previous_value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def column_names
|
19
|
+
@columns.map(&:first)
|
20
|
+
end
|
21
|
+
|
22
|
+
def mapping
|
23
|
+
@columns.each_with_object({}) do |(name, column), hash|
|
24
|
+
hash[column.from] = name
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def [](name)
|
29
|
+
@columns.fetch(name) { raise(UnknownColumn, name) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "transproc"
|
2
|
+
|
3
|
+
module Tablecloth
|
4
|
+
class Transformer
|
5
|
+
def initialize(definition, *args)
|
6
|
+
@definition = definition
|
7
|
+
@args = args
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(value)
|
11
|
+
previous_values = {}
|
12
|
+
|
13
|
+
value.map do |row|
|
14
|
+
row.each_with_object({}) do |(key, value), hash|
|
15
|
+
column = @definition[key]
|
16
|
+
previous_value = previous_values[key]
|
17
|
+
result = if previous_value && value == ""
|
18
|
+
previous_value
|
19
|
+
else
|
20
|
+
visit(column).call(value)
|
21
|
+
end
|
22
|
+
|
23
|
+
previous_values[key] = result if column.retain_previous_value?
|
24
|
+
hash[key] = result
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def visit(column)
|
32
|
+
Transproc(:"to_#{column.type}", *@args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/tablecloth.rb
ADDED
data/tablecloth.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tablecloth/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tablecloth"
|
8
|
+
spec.version = Tablecloth::VERSION
|
9
|
+
spec.authors = ["Tim Cooper"]
|
10
|
+
spec.email = ["coop@latrobest.com"]
|
11
|
+
|
12
|
+
spec.summary = "DRY-er cucumber tables"
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.homepage = "https://github.com/coop/tablecloth"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "transproc"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
25
|
+
spec.add_development_dependency "cucumber"
|
26
|
+
spec.add_development_dependency "money"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tablecloth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Cooper
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: transproc
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.9'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: cucumber
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: money
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: DRY-er cucumber tables
|
98
|
+
email:
|
99
|
+
- coop@latrobest.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/tablecloth.rb
|
114
|
+
- lib/tablecloth/coercions.rb
|
115
|
+
- lib/tablecloth/table_definition.rb
|
116
|
+
- lib/tablecloth/transformer.rb
|
117
|
+
- lib/tablecloth/version.rb
|
118
|
+
- tablecloth.gemspec
|
119
|
+
homepage: https://github.com/coop/tablecloth
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubyforge_project:
|
139
|
+
rubygems_version: 2.4.5
|
140
|
+
signing_key:
|
141
|
+
specification_version: 4
|
142
|
+
summary: DRY-er cucumber tables
|
143
|
+
test_files: []
|