class Chop::Create

Attributes

after_hooks[RW]
deferred_attributes[RW]
transformations[RW]

Public Class Methods

create!(klass, table, &block) click to toggle source
# File lib/chop/create.rb, line 8
def self.create! klass, table, &block
  new(klass, table, block).create!
end
new(*, &other_block) click to toggle source
Calls superclass method
# File lib/chop/create.rb, line 33
def initialize(*, &other_block)
  super
  self.transformations = []
  self.deferred_attributes = HashWithIndifferentAccess.new
  self.after_hooks = []
  instance_eval &block if block.respond_to?(:call)
  instance_eval &other_block if block_given?
end
register_creation_strategy(key, &block) click to toggle source
# File lib/chop/create.rb, line 15
def self.register_creation_strategy key, &block
  creation_strategies[key] = block
end

Public Instance Methods

after(*keys, &block) click to toggle source
# File lib/chop/create.rb, line 177
def after *keys, &block
  defer *keys
  after_hooks << block
end
belongs_to(key, klass=nil, name_field: :name)
Alias for: has_one
copy(mappings) click to toggle source
# File lib/chop/create.rb, line 77
def copy mappings
  transformation do |attributes|
    mappings.each do |from, to|
      attributes[to] = attributes[from]
    end
    attributes
  end
end
create(&block) click to toggle source
# File lib/chop/create.rb, line 60
def create &block
  self.creation_strategies = Proc.new { block }
end
create!(cucumber_table = table) click to toggle source
# File lib/chop/create.rb, line 42
def create! cucumber_table = table
  cucumber_table.hashes.map do |attributes|
    attributes = HashWithIndifferentAccess.new(attributes)
    attributes = transformations.reduce(attributes) do |attrs, transformation|
      transformation.call(attrs)
    end

    strategy, factory = klass.is_a?(Hash) ? klass.to_a.first : [nil, klass]
    args = [factory, attributes]
    record = creation_strategies[strategy].call(*args.compact)

    after_hooks.each do |after_hook|
      after_hook.call(record, attributes.merge(deferred_attributes))
    end
    record
  end
end
default(key, default_value = nil) { || ... } click to toggle source
# File lib/chop/create.rb, line 105
def default key, default_value = nil
  field(key, default: nil) { |value| value || default_value || yield }
end
defer(*keys) click to toggle source
# File lib/chop/create.rb, line 182
def defer *keys
  transformation do |attributes|
    keys.each do |key|
      self.deferred_attributes[key] = attributes.delete(key)
    end
    attributes
  end
end
delete(*keys) click to toggle source
# File lib/chop/create.rb, line 68
def delete *keys
  transformation do |attributes|
    keys.reduce(attributes) do |attrs, key|
      attributes.delete(key)
      attributes
    end
  end
end
field(attribute, default: "") { |fetch| ... } click to toggle source
# File lib/chop/create.rb, line 95
def field attribute, default: ""
  if attribute.is_a?(Hash)
    rename attribute
    attribute = attribute.values.first
  end
  transformation do |attributes|
    attributes.merge attribute => yield(attributes.fetch(attribute, default))
  end
end
file(*keys) click to toggle source
# File lib/chop/create.rb, line 117
def file *keys
  options = extract_options!(keys)
  path = options.fetch(:path, "features/support/fixtures")
  upload = options.fetch(:upload, false)

  handle_renames! keys

  keys.each do |key|
    field key do |file|
      if file.present?
        file_path = File.join(path, file)
        if upload
          Rack::Test::UploadedFile.new(file_path)
        else
          File.open(file_path)
        end
      end
    end
  end
end
files(*keys) click to toggle source
# File lib/chop/create.rb, line 138
def files *keys
  options = extract_options!(keys)
  path = options.fetch(:path, "features/support/fixtures")
  upload = options.fetch(:upload, false)
  delimiter = options.fetch(:delimiter, " ")

  handle_renames! keys

  keys.each do |key|
    field key do |paths|
      paths.split(delimiter).map do |file|
        file_path = File.join(path, file)
        if upload
          Rack::Test::UploadedFile.new(file_path)
        else
          File.open(file_path)
        end
      end
    end
  end
end
has_many(key, klass=nil, delimiter: ", ", name_field: :name) click to toggle source
# File lib/chop/create.rb, line 160
def has_many key, klass=nil, delimiter: ", ", name_field: :name
  klass ||= key.to_s.classify.constantize
  field key do |names|
    names.split(delimiter).map do |name|
      klass.find_by!(name_field => name)
    end
  end
end
has_one(key, klass=nil, name_field: :name) click to toggle source
# File lib/chop/create.rb, line 169
def has_one key, klass=nil, name_field: :name
  klass ||= key.to_s.classify.constantize
  field key do |name|
    klass.find_by!(name_field => name) if name.present?
  end
end
Also aliased as: belongs_to
rename(mappings) click to toggle source
# File lib/chop/create.rb, line 86
def rename mappings
  transformation do |attributes|
    mappings.reduce(attributes) do |attrs, (old, new)|
      attrs[new] = attrs.delete(old) if attrs.key?(old)
      attrs
    end
  end
end
transformation(&block) click to toggle source
# File lib/chop/create.rb, line 64
def transformation &block
  transformations << block
end
underscore_keys() click to toggle source
# File lib/chop/create.rb, line 109
def underscore_keys
  transformation do |attributes|
    attributes.reduce(HashWithIndifferentAccess.new) do |hash, (key, value)|
      hash.merge key.parameterize.underscore => value
    end
  end
end

Private Instance Methods

extract_options!(keys) click to toggle source
# File lib/chop/create.rb, line 193
def extract_options! keys
  if keys.last.is_a?(Hash) && keys.length > 1
    keys.pop
  else
    {}
  end
end
handle_renames!(keys) click to toggle source
# File lib/chop/create.rb, line 201
def handle_renames! keys
  if keys.first.is_a?(Hash) && keys.length == 1
    rename keys.first
    keys.replace keys.first.values
  end
end