chop 0.15.0 → 0.16.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58ec1d010f7e641bb853d3334ffcfea2422131f1
4
- data.tar.gz: 96c010fae67db9a77ff16365cc5d724faf6b28e3
3
+ metadata.gz: f361d4f1d6428136b9187e57b495e8a7a2d1b8cb
4
+ data.tar.gz: 7405d53d647c0af2468fc92cbeaf8f860cb8a921
5
5
  SHA512:
6
- metadata.gz: f24073305f6ee2fb53ccc7e1bca8f350b48a135c9775034ae918cc2a2774b919d4d14959db8fc8c85c90a951395c2f620aacfeb0784c5b323cfae26332abb781
7
- data.tar.gz: 97592730bd93f1fa3d8fecac2ee7c58b08181df1d6fffc372e0bbc977760c90ad0e05cc5876cf33b26cbfcf1e967addf4706264d8c38103a4c746068e1d9f548
6
+ metadata.gz: 5a991f6f01794ef5dc5cf033713daeaae096c09b358a602302893491329a23a55e2c5fbaf592a920903370e77972862aea944e2c7036d1a071612f2883573a6f
7
+ data.tar.gz: 3ebb468ac5ad8ce0aeacb38611ee92b4cc2932fd809d9b61ec7142e802468987d70729af4c45c81c7054a04f1b902a4eb0acbaed965f71a09eed4a08f8336ab5
data/README.md CHANGED
@@ -142,7 +142,7 @@ end
142
142
  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.
143
143
 
144
144
  ```ruby
145
- Chop.create! table, Users
145
+ Chop.create! Users, table
146
146
  Chop.diff! table, "table"
147
147
  Chop.fill_in! table
148
148
  ```
data/lib/chop/create.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  require "active_support/core_ext/string/inflections"
2
2
  require "active_support/core_ext/object/blank"
3
+ require "active_support/hash_with_indifferent_access"
3
4
 
4
5
  module Chop
5
- class Create < Struct.new(:table, :klass, :block)
6
- def self.create! table, klass, &block
7
- new(table, klass, block).create!
6
+ class Create < Struct.new(:klass, :table, :block)
7
+ def self.create! klass, table, &block
8
+ new(klass, table, block).create!
8
9
  end
9
10
 
10
11
  attr_accessor :transformations
@@ -18,7 +19,10 @@ module Chop
18
19
 
19
20
  def create! cucumber_table = table
20
21
  cucumber_table.hashes.map do |attributes|
21
- transformations.each { |transformation| transformation.call(attributes) }
22
+ attributes = HashWithIndifferentAccess.new(attributes)
23
+ attributes = transformations.reduce(attributes) do |attrs, transformation|
24
+ transformation.call(attrs)
25
+ end
22
26
  if klass.is_a?(Hash)
23
27
  if factory = klass[:factory_girl]
24
28
  FactoryGirl.create factory, attributes
@@ -37,8 +41,9 @@ module Chop
37
41
 
38
42
  def rename mappings
39
43
  transformation do |attributes|
40
- mappings.each do |old, new|
41
- attributes[new.to_s] = attributes.delete(old.to_s) if attributes.key?(old.to_s)
44
+ mappings.reduce(attributes) do |attrs, (old, new)|
45
+ attrs[new] = attrs.delete(old) if attrs.key?(old)
46
+ attrs
42
47
  end
43
48
  end
44
49
  end
@@ -49,16 +54,15 @@ module Chop
49
54
  attribute = attribute.values.first
50
55
  end
51
56
  transformation do |attributes|
52
- attributes[attribute.to_s] = yield(attributes.fetch(attribute.to_s, default))
57
+ attributes.merge attribute => yield(attributes.fetch(attribute, default))
53
58
  end
54
59
  end
55
60
 
56
61
  def underscore_keys
57
62
  transformation do |attributes|
58
- new_attributes = attributes.inject({}) do |hash, (key, value)|
63
+ attributes.reduce(HashWithIndifferentAccess.new) do |hash, (key, value)|
59
64
  hash.merge key.parameterize.underscore => value
60
65
  end
61
- attributes.replace new_attributes
62
66
  end
63
67
  end
64
68
 
@@ -8,7 +8,7 @@ module Chop
8
8
 
9
9
  def column index, &block
10
10
  transformation do |rows|
11
- rows.map!.with_index do |row, row_index|
11
+ rows.map.with_index do |row, row_index|
12
12
  row[index] = block.call(row[index])
13
13
  row
14
14
  end
data/lib/chop/diff.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/core_ext/class/attribute"
4
+ require "active_support/hash_with_indifferent_access"
4
5
 
5
6
  module Chop
6
7
  class Diff < Struct.new(:selector, :table, :session, :block)
@@ -49,13 +50,14 @@ module Chop
49
50
  end
50
51
  end
51
52
  row[index] = yield(row[index])
53
+ row
52
54
  end
53
55
  else
54
56
  if block.arity.zero?
55
57
  @new_header = yield
56
58
  else
57
59
  header_transformation do |row|
58
- row.replace yield(row)
60
+ yield(row)
59
61
  end
60
62
  end
61
63
  end
@@ -68,11 +70,11 @@ module Chop
68
70
  def hash_transformation &block
69
71
  transformation do |rows|
70
72
  header = rows[0]
71
- keys = header.to_a.map { |cell| cell.text.parameterize.underscore.to_sym }
73
+ keys = header.to_a.map { |cell| cell.text.parameterize.underscore }
72
74
  body = rows[1..-1]
73
- hashes = body.map { |row| Hash[keys.zip(row)] }
75
+ hashes = body.map { |row| HashWithIndifferentAccess[keys.zip(row)] }
74
76
  yield hashes
75
- rows.replace [header] + hashes.map(&:values)
77
+ [header] + hashes.map(&:values)
76
78
  end
77
79
  end
78
80
 
@@ -117,9 +119,9 @@ module Chop
117
119
  rows = normalize(rows)
118
120
 
119
121
  header = @new_header ? normalize([@new_header]).first : rows.shift || []
120
- header_transformations.each do |transformation|
121
- transformation.call(header)
122
- header = normalize([header]).first
122
+ header = header_transformations.reduce(header) do |header, transformation|
123
+ header = transformation.call(header)
124
+ normalize([header]).first
123
125
  end
124
126
 
125
127
  if header
@@ -127,9 +129,9 @@ module Chop
127
129
  rows = normalize(rows)
128
130
  end
129
131
 
130
- transformations.each do |transformation|
131
- transformation.call(rows)
132
- rows = normalize(rows)
132
+ rows = transformations.reduce(rows) do |rows, transformation|
133
+ rows = transformation.call(rows)
134
+ normalize(rows)
133
135
  end
134
136
 
135
137
  rows.map do |row|
data/lib/chop/dsl.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Chop
2
2
  module DSL
3
- def create! table, klass, &block
4
- Create.create! table, klass, &block
3
+ def create! klass, table, &block
4
+ Create.create! klass, table, &block
5
5
  end
6
6
 
7
7
  def diff! selector, table, session: Capybara.current_session, &block
@@ -19,10 +19,10 @@ end
19
19
  if defined?(Cucumber::MultilineArgument::DataTable)
20
20
  Cucumber::MultilineArgument::DataTable.prepend Module.new {
21
21
  def create! klass, &block
22
- Chop.create! self, klass, &block
22
+ Chop.create! klass, self, &block
23
23
  end
24
24
 
25
- def diff! other_table, options={}, &block
25
+ def diff! other_table="table", options={}, &block
26
26
  if other_table.is_a?(String) && !other_table.include?("|")
27
27
  Chop.diff! other_table, self, &block
28
28
  else
data/lib/chop/form.rb CHANGED
@@ -16,7 +16,10 @@ module Chop
16
16
  class Field < Struct.new(:session, :label, :value, :path, :field)
17
17
  def self.for session, label, value, path
18
18
  field = session.find_field(label)
19
- descendants.map do |klass|
19
+ candidates = descendants.sort_by do |a|
20
+ a == Chop::Form::Default ? 1 : -1 # ensure Default comes last
21
+ end
22
+ candidates.map do |klass|
20
23
  klass.new(session, label, value, path, field)
21
24
  end.find(&:matches?)
22
25
  end
data/lib/chop/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Chop
2
- VERSION = "0.15.0"
2
+ VERSION = "0.16.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.16.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel