chop 0.7.0 → 0.8.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: 94a2a2f159e4a080262c2e468d7d9ce0cd47c581
4
- data.tar.gz: ff743ac73daf8117ce661f9d54b13ae0505dfb2f
3
+ metadata.gz: 5e7cb04d85d09ab8d21ecdc9d07c59972a4daba7
4
+ data.tar.gz: 72c5efe46c120810aeadcb6958f870b983a631cd
5
5
  SHA512:
6
- metadata.gz: 8d7ee5f0b4ebd55566dd3f307b4f3f96cf3fd6b0b71319a1a14915a2dafe4f907dfd6a33ede4f24523e3d0119611b80fa9e2b0e1e3f4f3ac9217472aeb067b1c
7
- data.tar.gz: 40d30162f232c7b34cbfa4e4c0b2a4ea65b9d0964e4cb882532a2cea3898d6914b7296302d551fb8664ee09a8313bd65a0818fc5cdffed397dbfbfde62957e0e
6
+ metadata.gz: a72bf7733a5db76a20212921d0f5cbd8f1ab57c7abd2ed6545474e26c96d20faa15330025fb26de45fc5df7a4933e441e669abb16042645f81e58367228ae10f
7
+ data.tar.gz: 8d044df36e430cd64032f42917dff0abf449ae76eaa7f1bf2d8151bd816bf176c667b854885ea7028929b10f7189e33e01f6afd678edb3dd882bb034aedeb843
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .ruby-version
11
+ .ruby-gemset
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Chop
2
2
 
3
- Slice and dice your Cucumber tables with ease!
3
+ Slice and dice your Cucumber tables with ease! Assumes usage of ActiveRecord and Capybara.
4
4
 
5
5
  ## Installation
6
6
 
@@ -12,13 +12,93 @@ end
12
12
 
13
13
  ## Usage
14
14
 
15
- TODO: Write more usage instructions here. See the source for usage in the meantime.
15
+ Chop monkeypatches Cucumber tables with two new methods:
16
16
 
17
- * `Chop::Builder`: Create ActiveRecord instances from a Cucumber table.
17
+ * `#build!`: Creates ActiveRecord instances.
18
+ * `#diff!`: Enhances existing method to also accept a CSS selector. Currently supports diffing `<table>`, `<dl>`, and `<ul>`.
18
19
 
19
- * `Chop::Table`: Diff a Cucumber table with a <table>.
20
- * `Chop::DefinitionList`: Diff a Cucumber table with a <dl>.
21
- * `Chop::UnorderedList`: Diff a Cucumber table with a <ul>.
20
+ Both these methods accept blocks for customization.
21
+
22
+ ### Block methods for `#build!`:
23
+
24
+ Transform the attributes hash derived from the table before passing to `ActiveRecord.create!`.
25
+
26
+ High-level declarative transformations:
27
+
28
+ * `#file`: Replaces a file path with a file handle. Looks in `features/support/fixtures` by default.
29
+ * `#files`: Replaces a space-delimited list of file paths with an array of file handles. Looks in `features/support/fixtures` by default.
30
+ * `#has_one/#belongs_to`: Replaces an entity name with that entity. Uses `.find_by_name` by default.
31
+ * `#has_many`: Replaces a comma-delimited list of entity names with an array of those entities. Uses `.find_by_name` by default.
32
+ * `#underscore_keys`: Converts all hash keys to underscored versions.
33
+
34
+ All these methods are implemented in terms of the following low-level methods, useful for when you need more control over the transformation:
35
+ * `#field`: performs transformations on a specific field value.
36
+ * `#transformation`: performs transformations on the attributes hash.
37
+
38
+ ### Block methods for `#diff!`:
39
+
40
+ TODO: Pending API overhaul.
41
+
42
+ ### Example
43
+
44
+ ```gherkin
45
+ # features/manage_industries.feature
46
+
47
+ Given the following industries exist:
48
+ | name | wall_background_file | table_background_file |
49
+ | Another industry | wall.jpg | table.jpg |
50
+ | Example industry | wall.jpg | table.jpg |
51
+
52
+ Given the following stories exist:
53
+ | industry | image_file | headline |
54
+ | Example industry | example.jpg | Example headline |
55
+ | Example industry | example.jpg | Another headline |
56
+
57
+ Given I am on the home page
58
+ Then I should see the following industries:
59
+ | ANOTHER INDUSTRY |
60
+ | EXAMPLE INDUSTRY |
61
+ And I should see the following "EXAMPLE INDUSTRY" stories:
62
+ | IMAGE | HEADLINE |
63
+ | example.jpg | Example headline |
64
+ | example.jpg | Another headline |
65
+ ```
66
+
67
+ ```ruby
68
+ # features/step_definition/industry_steps.rb
69
+
70
+ Given "the following industries exist:" do |table|
71
+ table.create! ConversationTable::Industry do
72
+ file :wall_background_file, :table_background_file
73
+ end
74
+ end
75
+
76
+ Given "the following stories exist:" do |table|
77
+ table.create! ConversationTable::Story do
78
+ belongs_to :industry, ConversationTable::Industry
79
+ file :image_file
80
+ end
81
+ end
82
+
83
+ Then "I should see the following industries:" do |table|
84
+ table.diff! "dl"
85
+ end
86
+
87
+ Then /^I should see the following "(.+?)" stories:$/ do |industry_name, table|
88
+ within "dfn", text: industry_name do
89
+ table.diff! "table"
90
+ end
91
+ end
92
+ ```
93
+
94
+ ### Don't like monkeypatching?
95
+
96
+ Load `chop` before `cucumber` in your Gemfile, and call the two methods directly on the `Chop` module, passing the cucumber table in as the first argument.
97
+
98
+ ```ruby
99
+ Chop.build! table, Users
100
+ Chop.diff! table, "table"
101
+ ```
22
102
 
23
103
  ## Development
24
104
 
@@ -5,5 +5,32 @@ require "chop/definition_list"
5
5
  require "chop/unordered_list"
6
6
 
7
7
  module Chop
8
+ class << self
9
+ def create! table, klass, &block
10
+ Builder.build! table, klass, &block
11
+ end
12
+
13
+ def diff! table, selector, &block
14
+ class_name = Capybara.current_session.find(selector).tag_name.capitalize
15
+ klass = const_get(class_name)
16
+ klass.diff! table, &block
17
+ end
18
+ end
19
+ end
20
+
21
+ if defined?(Cucumber::MultilineArgument::DataTable)
22
+ Cucumber::MultilineArgument::DataTable.prepend Module.new {
23
+ def create! klass, &block
24
+ Chop.create! self, klass, &block
25
+ end
26
+
27
+ def diff! other_table, options={}, &block
28
+ if other_table.is_a?(String) && !other_table.include?("|")
29
+ Chop.diff! self, other_table, &block
30
+ else
31
+ super
32
+ end
33
+ end
34
+ }
8
35
  end
9
36
 
@@ -9,7 +9,6 @@ module Chop
9
9
  def initialize(*)
10
10
  super
11
11
  self.transformations = []
12
- underscore_keys
13
12
  instance_eval &block if block.respond_to?(:call)
14
13
  end
15
14
 
@@ -24,9 +23,9 @@ module Chop
24
23
  transformations << block
25
24
  end
26
25
 
27
- def field attribute
26
+ def field attribute, default: ""
28
27
  transformation do |attributes|
29
- attributes[attribute.to_s] = yield(attributes.fetch(attribute.to_s, ""))
28
+ attributes[attribute.to_s] = yield(attributes.fetch(attribute.to_s, default))
30
29
  end
31
30
  end
32
31
 
@@ -39,35 +38,35 @@ module Chop
39
38
  end
40
39
  end
41
40
 
42
- def file *keys
41
+ def file *keys, path: "features/support/fixtures"
43
42
  keys.each do |key|
44
- field key do |path|
45
- File.open("features/support/fixtures/#{path}") if path.present?
43
+ field key do |file|
44
+ File.open(File.join(path, file)) if file.present?
46
45
  end
47
46
  end
48
47
  end
49
48
 
50
- def files *keys
49
+ def files *keys, path: "features/support/fixtures", delimiter: " "
51
50
  keys.each do |key|
52
51
  field key do |paths|
53
- paths.split(" ").map do |path|
54
- File.open("features/support/fixtures/#{path}")
52
+ paths.split(delimiter).map do |file|
53
+ File.open(File.join(path, file))
55
54
  end
56
55
  end
57
56
  end
58
57
  end
59
58
 
60
- def has_many key, class_name=nil, delimiter: ", ", name_field: :name
59
+ def has_many key, klass=nil, delimiter: ", ", name_field: :name
61
60
  field key do |names|
62
61
  names.split(delimiter).map do |name|
63
- class_name.find_by!(name_field => name)
62
+ klass.find_by!(name_field => name)
64
63
  end
65
64
  end
66
65
  end
67
66
 
68
- def has_one key, class_name=nil, name_field: :name
67
+ def has_one key, klass=nil, name_field: :name
69
68
  field key do |name|
70
- class_name.find_by!(name_field => name)
69
+ klass.find_by!(name_field => name)
71
70
  end
72
71
  end
73
72
  alias_method :belongs_to, :has_one
@@ -47,5 +47,7 @@ module Chop
47
47
  end
48
48
  end
49
49
  end
50
+
51
+ Dl = DefinitionList
50
52
  end
51
53
 
@@ -47,5 +47,7 @@ module Chop
47
47
  end
48
48
  end
49
49
  end
50
+
51
+ Ul = UnorderedList
50
52
  end
51
53
 
@@ -1,3 +1,3 @@
1
1
  module Chop
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.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.7.0
4
+ version: 0.8.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-02-16 00:00:00.000000000 Z
11
+ date: 2017-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  version: '0'
96
96
  requirements: []
97
97
  rubyforge_project:
98
- rubygems_version: 2.6.8
98
+ rubygems_version: 2.4.8
99
99
  signing_key:
100
100
  specification_version: 4
101
101
  summary: Slice and dice your cucumber tables with ease!