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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f361d4f1d6428136b9187e57b495e8a7a2d1b8cb
4
- data.tar.gz: 7405d53d647c0af2468fc92cbeaf8f860cb8a921
3
+ metadata.gz: 59d92389e8d22766ebf98088ebd74d94e8688985
4
+ data.tar.gz: c9faf716a53e8723cbf06557536a40874acf63b1
5
5
  SHA512:
6
- metadata.gz: 5a991f6f01794ef5dc5cf033713daeaae096c09b358a602302893491329a23a55e2c5fbaf592a920903370e77972862aea944e2c7036d1a071612f2883573a6f
7
- data.tar.gz: 3ebb468ac5ad8ce0aeacb38611ee92b4cc2932fd809d9b61ec7142e802468987d70729af4c45c81c7054a04f1b902a4eb0acbaed965f71a09eed4a08f8336ab5
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 instances. Also supports FactoryGirl.
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 oield value.
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!`.
@@ -5,8 +5,10 @@ require "chop/definition_list"
5
5
  require "chop/unordered_list"
6
6
  require "chop/form"
7
7
  require "chop/dsl"
8
+ require "chop/config"
8
9
 
9
10
  module Chop
10
11
  extend DSL
12
+ extend Config
11
13
  end
12
14
 
@@ -0,0 +1,7 @@
1
+ module Chop
2
+ module Config
3
+ def register_creation_strategy *args, &block
4
+ Chop::Create.register_creation_strategy *args, &block
5
+ end
6
+ end
7
+ end
@@ -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
- attr_accessor :transformations
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
- if klass.is_a?(Hash)
27
- if factory = klass[:factory_girl]
28
- FactoryGirl.create factory, attributes
29
- else
30
- raise "Unknown building strategy"
31
- end
32
- else
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
@@ -1,3 +1,3 @@
1
1
  module Chop
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
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.16.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-23 00:00:00.000000000 Z
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