chop 0.16.0 → 0.17.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 +4 -4
- data/README.md +11 -2
- data/lib/chop.rb +2 -0
- data/lib/chop/config.rb +7 -0
- data/lib/chop/create.rb +54 -9
- data/lib/chop/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59d92389e8d22766ebf98088ebd74d94e8688985
|
4
|
+
data.tar.gz: c9faf716a53e8723cbf06557536a40874acf63b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96cd9da2dad570c6ea2cd5dc499241defd524dd46112cb45b64ce34049f032734a80362e08ebc3a5b50b08ddaa5951f26bd1f8583b976b9787190045bcff45ef
|
7
|
+
data.tar.gz: 478895292a06647dfa5ee2df7dbef98986bef6c68febe0447d31fe8d79bde787a1a0b1526674182ee2cc40ffe795efa34a0e945aaeac243a3c3d33e0deba5348
|
data/README.md
CHANGED
@@ -17,12 +17,16 @@ end
|
|
17
17
|
|
18
18
|
Chop monkeypatches Cucumber tables with three new methods:
|
19
19
|
|
20
|
-
* `#create!`: Creates ActiveRecord
|
20
|
+
* `#create!`: Creates entities. Built-in support for ActiveRecord (default) and FactoryGirl, at present.
|
21
21
|
* `#diff!`: Enhances existing method to also accept a CSS selector. Currently supports diffing `<table>`, `<dl>`, and `<ul>`.
|
22
22
|
* `#fill_in!`: Fills in a form on the current page.
|
23
23
|
|
24
24
|
All these methods accept blocks for customization.
|
25
25
|
|
26
|
+
### Configuration methods for `Chop`:
|
27
|
+
|
28
|
+
* `.register_creation_strategy`: Provide a key and a block to register alternate strategies for `.create!`, e.g. FactoryGirl.
|
29
|
+
|
26
30
|
### Block methods for `#create!`:
|
27
31
|
|
28
32
|
Transform the attributes hash derived from the table before passing to `ActiveRecord.create!`.
|
@@ -35,11 +39,16 @@ High-level declarative transformations:
|
|
35
39
|
* `#has_many`: Replaces a comma-delimited list of entity names with an array of those entities. Uses `.find_by_name` by default.
|
36
40
|
* `#underscore_keys`: Converts all hash keys to underscored versions.
|
37
41
|
* `#rename`: Renames one or more fields.
|
42
|
+
* `#delete`: Deletes one or more fields.
|
38
43
|
|
39
44
|
All these methods are implemented in terms of the following low-level methods, useful for when you need more control over the transformation:
|
40
|
-
* `#field`: performs transformations on a specific
|
45
|
+
* `#field`: performs transformations on a specific field value.
|
41
46
|
* `#transformation`: performs transformations on the attributes hash.
|
42
47
|
|
48
|
+
Lifecycle hooks:
|
49
|
+
* `#after`: Temporarily removes zero or more fields, and then after record creation, yields them to the block, along with the created record.
|
50
|
+
* `#create`: Override the default creation strategy.
|
51
|
+
|
43
52
|
### Block methods for `#diff!`:
|
44
53
|
|
45
54
|
Transform the table of Capybara nodes before converting them to text and passing to `diff!`.
|
data/lib/chop.rb
CHANGED
data/lib/chop/config.rb
ADDED
data/lib/chop/create.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "active_support/core_ext/string/inflections"
|
2
2
|
require "active_support/core_ext/object/blank"
|
3
3
|
require "active_support/hash_with_indifferent_access"
|
4
|
+
require "active_support/core_ext/class/attribute"
|
4
5
|
|
5
6
|
module Chop
|
6
7
|
class Create < Struct.new(:klass, :table, :block)
|
@@ -8,11 +9,28 @@ module Chop
|
|
8
9
|
new(klass, table, block).create!
|
9
10
|
end
|
10
11
|
|
11
|
-
|
12
|
+
class_attribute :creation_strategies
|
13
|
+
self.creation_strategies = {}
|
14
|
+
|
15
|
+
def self.register_creation_strategy key, &block
|
16
|
+
creation_strategies[key] = block
|
17
|
+
end
|
18
|
+
|
19
|
+
register_creation_strategy nil do |klass, attributes|
|
20
|
+
klass.create! attributes
|
21
|
+
end
|
22
|
+
|
23
|
+
register_creation_strategy :factory_girl do |factory, attributes|
|
24
|
+
FactoryGirl.create factory, attributes
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_accessor :transformations, :deferred_attributes, :after_hooks
|
12
28
|
|
13
29
|
def initialize(*, &other_block)
|
14
30
|
super
|
15
31
|
self.transformations = []
|
32
|
+
self.deferred_attributes = HashWithIndifferentAccess.new
|
33
|
+
self.after_hooks = []
|
16
34
|
instance_eval &block if block.respond_to?(:call)
|
17
35
|
instance_eval &other_block if block_given?
|
18
36
|
end
|
@@ -23,22 +41,35 @@ module Chop
|
|
23
41
|
attributes = transformations.reduce(attributes) do |attrs, transformation|
|
24
42
|
transformation.call(attrs)
|
25
43
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
klass.create! attributes
|
44
|
+
|
45
|
+
strategy, factory = klass.is_a?(Hash) ? klass.to_a.first : [nil, klass]
|
46
|
+
args = [factory, attributes]
|
47
|
+
record = creation_strategies[strategy].call(*args.compact)
|
48
|
+
|
49
|
+
after_hooks.each do |after_hook|
|
50
|
+
after_hook.call(record, attributes.merge(deferred_attributes))
|
34
51
|
end
|
52
|
+
record
|
35
53
|
end
|
36
54
|
end
|
37
55
|
|
56
|
+
def create &block
|
57
|
+
self.creation_strategies = Proc.new { block }
|
58
|
+
end
|
59
|
+
|
38
60
|
def transformation &block
|
39
61
|
transformations << block
|
40
62
|
end
|
41
63
|
|
64
|
+
def delete *keys
|
65
|
+
transformation do |attributes|
|
66
|
+
keys.reduce(attributes) do |attrs, key|
|
67
|
+
attributes.delete(key)
|
68
|
+
attributes
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
42
73
|
def rename mappings
|
43
74
|
transformation do |attributes|
|
44
75
|
mappings.reduce(attributes) do |attrs, (old, new)|
|
@@ -110,6 +141,20 @@ module Chop
|
|
110
141
|
end
|
111
142
|
alias_method :belongs_to, :has_one
|
112
143
|
|
144
|
+
def after *keys, &block
|
145
|
+
defer *keys
|
146
|
+
after_hooks << block
|
147
|
+
end
|
148
|
+
|
149
|
+
def defer *keys
|
150
|
+
transformation do |attributes|
|
151
|
+
keys.each do |key|
|
152
|
+
self.deferred_attributes[key] = attributes.delete(key)
|
153
|
+
end
|
154
|
+
attributes
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
113
158
|
private
|
114
159
|
|
115
160
|
def extract_options! keys
|
data/lib/chop/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chop
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.17.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-03-
|
11
|
+
date: 2017-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- bin/setup
|
127
127
|
- chop.gemspec
|
128
128
|
- lib/chop.rb
|
129
|
+
- lib/chop/config.rb
|
129
130
|
- lib/chop/create.rb
|
130
131
|
- lib/chop/definition_list.rb
|
131
132
|
- lib/chop/diff.rb
|