Module: Fixturama

Defined in:
lib/fixturama.rb,
lib/fixturama/changes.rb

Overview

A set of helpers to prettify specs with fixtures

Defined Under Namespace

Modules: Config Classes: Changes, FixtureError, Loader

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start_ids_from(value) ⇒ Fixturama

Set the initial value for database-generated IDs

Parameters:

  • value (#to_i)

Returns:



21
22
23
24
# File 'lib/fixturama.rb', line 21

def self.start_ids_from(value)
  Config.start_ids_from(value)
  self
end

Instance Method Details

#call_fixture(path, options) ⇒ RSpec::Core::Example Also known as: seed_fixture, stub_fixture

Stub different objects and seed the database from a fixture

Parameters:

  • path (#to_s)

    The path to the fixture file

  • options (Hash<Symbol, _>)

    The list of options to be accessible in the fixture

Returns:

  • (RSpec::Core::Example)

    the current example



52
53
54
55
56
57
58
# File 'lib/fixturama.rb', line 52

def call_fixture(path, **options)
  items = Array load_fixture(path, **options)
  items.each { |item| changes.add(item) }
  tap { changes.call(self) }
rescue FixtureError => err
  raise err.with_file(path)
end

#load_fixture(path, options) ⇒ Object

Load data from a fixture

Parameters:

  • path (#to_s)

    The path to the fixture file

  • options (Hash<Symbol, _>)

    The list of options to be accessible in the fixture

Returns:

  • (Object)


44
45
46
# File 'lib/fixturama.rb', line 44

def load_fixture(path, **options)
  Loader.new(path, options).call
end

#read_fixture(path, options) ⇒ String

Read the text content of the fixture

Parameters:

  • path (#to_s)

    The path to the fixture file

  • options (Hash<Symbol, _>)

    The list of options to be accessible in the fixture

Returns:

  • (String)


32
33
34
35
36
37
38
# File 'lib/fixturama.rb', line 32

def read_fixture(path, **options)
  content  = File.read(path)
  hashie   = Hashie::Mash.new(options)
  bindings = hashie.instance_eval { binding }

  ERB.new(content).result(bindings)
end