Module: ActiveCucumber

Defined in:
lib/active_cucumber.rb,
lib/active_cucumber/creator.rb,
lib/active_cucumber/cucumparer.rb,
lib/active_cucumber/cucumberator.rb,
lib/active_cucumber/active_record_builder.rb

Overview

The main namespace for this gem

Defined Under Namespace

Classes: ActiveRecordBuilder, Creator, Cucumberator, Cucumparer

Class Method Summary collapse

Class Method Details

.attributes_for(activerecord_class, cucumber_table, context: {}) ⇒ Hash

Returns the attributes to create an instance of the given ActiveRecord class that matches the given vertical Cucumber table.

Parameters:

  • activerecord_class (Class)

    the ActiveRecord class to create attributes for

  • cucumber_table (Cucumber::MultilineArgument::DataTable)

    a vertical Cucumber table

  • context (Hash) (defaults to: {})

    optional context values to inject into custom Creator classes

Returns:

  • (Hash)

    FactoryBot-compatible attributes hash

Raises:

  • (TypeError)

    if activerecord_class is not an ActiveRecord class



19
20
21
22
23
24
# File 'lib/active_cucumber.rb', line 19

def self.attributes_for(activerecord_class, cucumber_table, context: {})
  validate_activerecord_class!(activerecord_class)
  validate_context!(context)
  builder = ActiveRecordBuilder.new activerecord_class, context
  builder.attributes_for ActiveCucumber.vertical_table(cucumber_table)
end

.create_many(activerecord_class, cucumber_table, context: {}) ⇒ Array<ActiveRecord::Base>

Creates entries of the given ActiveRecord class specified by the given horizontal Cucumber table.

Parameters:

  • activerecord_class (Class)

    the ActiveRecord class to create records for

  • cucumber_table (Cucumber::MultilineArgument::DataTable)

    a horizontal Cucumber table

  • context (Hash) (defaults to: {})

    optional context values to inject into custom Creator classes

Returns:

  • (Array<ActiveRecord::Base>)

    array of created records

Raises:

  • (TypeError)

    if activerecord_class is not an ActiveRecord class

  • (ActiveRecord::RecordInvalid)

    if any record fails validation



35
36
37
38
39
40
# File 'lib/active_cucumber.rb', line 35

def self.create_many(activerecord_class, cucumber_table, context: {})
  validate_activerecord_class!(activerecord_class)
  validate_context!(context)
  builder = ActiveRecordBuilder.new activerecord_class, context
  builder.create_many ActiveCucumber.horizontal_table(cucumber_table)
end

.create_one(activerecord_class, cucumber_table, context: {}) ⇒ ActiveRecord::Base

Creates an entry of the given ActiveRecord class specified by the given vertical Cucumber table.

Parameters:

  • activerecord_class (Class)

    the ActiveRecord class to create a record for

  • cucumber_table (Cucumber::MultilineArgument::DataTable)

    a vertical Cucumber table

  • context (Hash) (defaults to: {})

    optional context values to inject into custom Creator classes

Returns:

  • (ActiveRecord::Base)

    the created record

Raises:

  • (TypeError)

    if activerecord_class is not an ActiveRecord class

  • (ActiveRecord::RecordInvalid)

    if the record fails validation



51
52
53
54
55
56
# File 'lib/active_cucumber.rb', line 51

def self.create_one(activerecord_class, cucumber_table, context: {})
  validate_activerecord_class!(activerecord_class)
  validate_context!(context)
  builder = ActiveRecordBuilder.new activerecord_class, context
  builder.create_record ActiveCucumber.vertical_table(cucumber_table)
end

.diff_all!(clazz, cucumber_table, context: {}) ⇒ void

Note:

Records are sorted by creation date before comparison

This method returns an undefined value.

Verifies that the database table for the given ActiveRecord class matches the given horizontal Cucumber table.

Parameters:

  • clazz (Class)

    the ActiveRecord class to compare records from

  • cucumber_table (Cucumber::MultilineArgument::DataTable)

    a horizontal Cucumber table

  • context (Hash) (defaults to: {})

    optional context values to inject into custom Cucumberator classes

Raises:

  • (Cucumber::MultilineArgument::DataTable::Different)

    if tables don’t match



67
68
69
70
71
72
# File 'lib/active_cucumber.rb', line 67

def self.diff_all!(clazz, cucumber_table, context: {})
  validate_activerecord_class_or_collection!(clazz)
  validate_context!(context)
  cucumparer = Cucumparer.new clazz, cucumber_table, context
  cucumber_table.diff! cucumparer.to_horizontal_table
end

.diff_one!(object, cucumber_table, context: {}) ⇒ void

This method returns an undefined value.

Verifies that the given object matches the given vertical Cucumber table.

Parameters:

  • object (ActiveRecord::Base)

    the ActiveRecord instance to compare

  • cucumber_table (Cucumber::MultilineArgument::DataTable)

    a vertical Cucumber table

  • context (Hash) (defaults to: {})

    optional context values to inject into custom Cucumberator classes

Raises:

  • (Cucumber::MultilineArgument::DataTable::Different)

    if tables don’t match



81
82
83
84
85
86
# File 'lib/active_cucumber.rb', line 81

def self.diff_one!(object, cucumber_table, context: {})
  validate_activerecord_instance!(object)
  validate_context!(context)
  cucumparer = Cucumparer.new object.class, cucumber_table, context
  cucumber_table.diff! cucumparer.to_vertical_table(object)
end

.horizontal_table(table) ⇒ Array<Hash>

Returns the given horizontal Cucumber table in standardized format.

Parameters:

  • table (Cucumber::MultilineArgument::DataTable)

    a horizontal Cucumber table

Returns:

  • (Array<Hash>)

    array of hashes where keys are column headers and values are cell values



92
93
94
# File 'lib/active_cucumber.rb', line 92

def self.horizontal_table(table)
  table.hashes
end

.validate_activerecord_class!(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (TypeError)


105
106
107
108
109
# File 'lib/active_cucumber.rb', line 105

def self.validate_activerecord_class!(klass)
  return if klass.is_a?(Class) && klass < ActiveRecord::Base

  raise TypeError, "Expected an ActiveRecord class, got #{klass.inspect}"
end

.validate_activerecord_class_or_collection!(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (TypeError)


112
113
114
115
116
117
118
# File 'lib/active_cucumber.rb', line 112

def self.validate_activerecord_class_or_collection!(value)
  # Allow ActiveRecord class, array of instances, or ActiveRecord relation/association
  return if value.is_a?(Class) && value < ActiveRecord::Base
  return if value.is_a?(Array) || value.respond_to?(:all)

  raise TypeError, "Expected an ActiveRecord class or collection, got #{value.class}"
end

.validate_activerecord_instance!(object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (TypeError)


121
122
123
124
125
# File 'lib/active_cucumber.rb', line 121

def self.validate_activerecord_instance!(object)
  return if object.is_a?(ActiveRecord::Base)

  raise TypeError, "Expected an ActiveRecord instance, got #{object.class}"
end

.validate_context!(context) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

  • (TypeError)


128
129
130
131
132
# File 'lib/active_cucumber.rb', line 128

def self.validate_context!(context)
  return if context.is_a?(Hash)

  raise TypeError, "Expected context to be a Hash, got #{context.class}"
end

.vertical_table(table) ⇒ Hash

Returns the given vertical Cucumber table in standardized format.

Parameters:

  • table (Cucumber::MultilineArgument::DataTable)

    a vertical Cucumber table

Returns:

  • (Hash)

    hash where keys are from first column and values are from second column



100
101
102
# File 'lib/active_cucumber.rb', line 100

def self.vertical_table(table)
  table.rows_hash
end