Module: Fixtury

Defined in:
lib/fixtury.rb,
lib/fixtury/hooks.rb,
lib/fixtury/store.rb,
lib/fixtury/errors.rb,
lib/fixtury/schema.rb,
lib/fixtury/locator.rb,
lib/fixtury/railtie.rb,
lib/fixtury/version.rb,
lib/fixtury/reference.rb,
lib/fixtury/definition.rb,
lib/fixtury/dependency.rb,
lib/fixtury/schema_node.rb,
lib/fixtury/configuration.rb,
lib/fixtury/path_resolver.rb,
lib/fixtury/minitest_hooks.rb,
lib/fixtury/dependency_store.rb,
lib/fixtury/mutation_observer.rb,
lib/fixtury/definition_executor.rb,
lib/fixtury/locator_backend/common.rb,
lib/fixtury/locator_backend/memory.rb,
lib/fixtury/locator_backend/global_id.rb

Overview

Top level namespace of the gem. The accessors provided on the Fixtury namespace are meant to be shared across the entire application. The Fixtury::Schema instance is the primary interface for defining and accessing fixtures and can be accessed via Fixtury.schema.

Defined Under Namespace

Modules: Errors, LocatorBackend, MinitestHooks, MutationObserver, SchemaNode Classes: Configuration, Definition, DefinitionExecutor, Dependency, DependencyStore, Hooks, Locator, PathResolver, Railtie, Reference, Schema, Store

Constant Summary collapse

LOG_LEVELS =
{
  (LOG_LEVEL_NONE = :none) => 0,
  (LOG_LEVEL_INFO = :info) => 1,
  (LOG_LEVEL_DEBUG = :debug) => 2,
  (LOG_LEVEL_ALL = :all) => 3,
}.freeze
DEFAULT_LOG_LEVEL =
LOG_LEVEL_INFO
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.configuration {|@configuration| ... } ⇒ Object

Yields:



42
43
44
45
46
# File 'lib/fixtury.rb', line 42

def self.configuration
  @configuration ||= ::Fixtury::Configuration.new
  yield @configuration if block_given?
  @configuration
end

.configure(&block) ⇒ Object



48
49
50
# File 'lib/fixtury.rb', line 48

def self.configure(&block)
  self.configuration(&block)
end

.define(&block) ⇒ Object

Shortcut for opening the top level schema.



54
55
56
57
# File 'lib/fixtury.rb', line 54

def self.define(&block)
  schema.define(&block)
  schema
end

.hooksObject

Global hooks accessor. Fixtury will call these hooks at various points in the lifecycle of a fixture or setup.



60
61
62
# File 'lib/fixtury.rb', line 60

def self.hooks
  @hooks ||= ::Fixtury::Hooks.new
end

.load_all_fixturesObject

Ensure all definitions are loaded and then load all known fixtures.



96
97
98
99
# File 'lib/fixtury.rb', line 96

def self.load_all_fixtures(...)
  load_all_schemas(...)
  store.load_all
end

.load_all_schemas(mechanism = :require) ⇒ Object

Require each schema file to ensure that all definitions are loaded.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fixtury.rb', line 83

def self.load_all_schemas(mechanism = :require)
  configuration.fixture_files.each do |filepath|
    if mechanism == :require
      require filepath
    elsif mechanism == :load
      load filepath
    else
      raise ArgumentError, "unknown load mechanism: #{mechanism}"
    end
  end
end

.log(text = nil, level: LOG_LEVEL_DEBUG, name: nil, newline: true) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fixtury.rb', line 118

def self.log(text = nil, level: LOG_LEVEL_DEBUG, name: nil, newline: true)
  desired_level = LOG_LEVELS.fetch(configuration.log_level) { DEFAULT_LOG_LEVEL }
  return if desired_level == LOG_LEVEL_NONE

  message_level = LOG_LEVELS.fetch(level) { LOG_LEVEL_DEBUG }
  return unless desired_level >= message_level

  msg = +"[fixtury"
  msg << "|#{name}" if name
  msg << "]"
  msg << " #{text}" if text
  msg << " #{yield}" if block_given?
  msg << "\n" if newline

  # TODO: logger
  print msg
  msg
end

.resetObject

Remove all references from the active store and reset the dependency file.



102
103
104
105
# File 'lib/fixtury.rb', line 102

def self.reset
  configuration.reset
  store.reset
end

.reset_if_changedObject

Perform a reset if any of the tracked files have changed.



108
109
110
111
112
113
114
115
116
# File 'lib/fixtury.rb', line 108

def self.reset_if_changed
  changes = configuration.changes
  if changes
    log("changes found, resetting | #{changes}", level: LOG_LEVEL_INFO)
    reset
  else
    log("no changes, skipping reset", level: LOG_LEVEL_INFO)
  end
end

.schemaObject

The default top level schema. Fixtury::Schema instances can be completely self-contained but most usage would be through this shared definition.



66
67
68
# File 'lib/fixtury.rb', line 66

def self.schema
  @schema ||= ::Fixtury::Schema.new
end

.startObject

Load all known fixture files configured in Configuration. Reset the store references if a dependency file has changed.



77
78
79
80
# File 'lib/fixtury.rb', line 77

def self.start
  load_all_schemas
  reset_if_changed
end

.storeObject

Default store for fixtures. This is a shared store that can be used across the application.



71
72
73
# File 'lib/fixtury.rb', line 71

def self.store
  @store ||= ::Fixtury::Store.new
end