Module: MagicLamp

Defined in:
lib/magic_lamp.rb,
lib/magic_lamp/engine.rb,
lib/magic_lamp/version.rb,
lib/magic_lamp/callbacks.rb,
lib/magic_lamp/constants.rb,
lib/magic_lamp/configuration.rb,
lib/magic_lamp/render_catcher.rb,
lib/magic_lamp/fixture_creator.rb,
lib/magic_lamp/defaults_manager.rb,
app/helpers/magic_lamp/application_helper.rb,
app/controllers/magic_lamp/lint_controller.rb,
app/controllers/magic_lamp/fixtures_controller.rb,
app/controllers/magic_lamp/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper, Callbacks Classes: ApplicationController, Configuration, DefaultsManager, Engine, FixtureCreator, FixturesController, LintController, RenderCatcher

Constant Summary collapse

VERSION =
"1.9.0"
APPLICATION =
"application"
APPLICATION_MATCHER =
Regexp.compile("(\/#{APPLICATION}|#{APPLICATION}\/|#{APPLICATION})")
EMPTY_STRING =
""
FORWARD_SLASH =
"/"
LAMP =
"_lamp"
REGISTER_FIXTURE_ALIASES =
%i[register fixture rub wish].freeze
SPEC =
"spec"
STARS =
"**"
TEST =
"test"
Genie =
Engine
AlreadyRegisteredFixtureError =
Class.new(StandardError)
AmbiguousFixtureNameError =
Class.new(StandardError)
ArgumentError =
Class.new(StandardError)
AttemptedRedirectError =
Class.new(StandardError)
DoubleRenderError =
Class.new(StandardError)
EmptyFixtureError =
Class.new(StandardError)
UnregisteredFixtureError =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



19
20
21
# File 'lib/magic_lamp.rb', line 19

def configuration
  @configuration
end

.registered_fixturesObject

Returns the value of attribute registered_fixtures.



19
20
21
# File 'lib/magic_lamp.rb', line 19

def registered_fixtures
  @registered_fixtures
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



41
42
43
44
# File 'lib/magic_lamp.rb', line 41

def configure(&block)
  raise_missing_block_error(block, __method__)
  yield(configuration)
end

.define(options = {}, &block) ⇒ Object



46
47
48
49
50
51
# File 'lib/magic_lamp.rb', line 46

def define(options = {}, &block)
  raise_missing_block_error(block, __method__)
  defaults_manager = DefaultsManager.new(configuration, options)
  defaults_manager.instance_eval(&block)
  defaults_manager
end

.generate_all_fixturesObject



106
107
108
109
110
111
# File 'lib/magic_lamp.rb', line 106

def generate_all_fixtures
  load_lamp_files
  registered_fixtures.keys.each_with_object({}) do |fixture_name, fixtures|
    fixtures[fixture_name] = generate_fixture(fixture_name)
  end
end

.generate_fixture(fixture_name) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/magic_lamp.rb', line 98

def generate_fixture(fixture_name)
  unless registered?(fixture_name)
    raise UnregisteredFixtureError, "'#{fixture_name}' is not a registered fixture"
  end
  controller_class, block, extensions = registered_fixtures[fixture_name].values_at(:controller, :render_block, :extend)
  FixtureCreator.new(configuration).generate_template(controller_class, extensions, &block)
end

.lint_configObject



63
64
65
66
67
68
69
70
# File 'lib/magic_lamp.rb', line 63

def lint_config
  self.registered_fixtures = {}
  errors = {}
  add_error_if_error(errors, :config_file_load) { load_config }
  add_callback_error_if_error(errors, :before_each)
  add_callback_error_if_error(errors, :after_each)
  errors
end

.lint_fixturesObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/magic_lamp.rb', line 72

def lint_fixtures
  self.registered_fixtures = {}
  file_errors = {}
  fixture_errors = {}

  lamp_files.each do |lamp_file|
    add_error_if_error(file_errors, lamp_file) { load lamp_file }
  end

  registered_fixtures.each do |fixture_name, fixture_info|
    begin
      generate_fixture(fixture_name)
    rescue => e
      fixture_errors[fixture_name] = fixture_info.merge(error: compose_error(e))
    end
  end

  { files: file_errors, fixtures: fixture_errors }
end

.load_configObject



57
58
59
60
61
# File 'lib/magic_lamp.rb', line 57

def load_config
  FactoryGirl.reload if defined?(FactoryGirl)
  self.configuration = Configuration.new
  load_all(config_files)
end

.load_lamp_filesObject



92
93
94
95
96
# File 'lib/magic_lamp.rb', line 92

def load_lamp_files
  self.registered_fixtures = {}
  load_config
  load_all(lamp_files)
end

.register_fixture(options = {}, &render_block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/magic_lamp.rb', line 21

def register_fixture(options = {}, &render_block)
  raise_missing_block_error(render_block, __method__)

  options[:controller] ||= ::ApplicationController
  options[:namespace] ||= options[:controller].controller_name
  options[:extend] = Array(options[:extend])
  options[:render_block] = render_block
  fixture_name = namespaced_fixture_name_or_raise(options)

  if registered?(fixture_name)
    raise AlreadyRegisteredFixtureError, "a fixture called '#{fixture_name}' has already been registered"
  end

  registered_fixtures[fixture_name] = options
end

.registered?(fixture_name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/magic_lamp.rb', line 53

def registered?(fixture_name)
  registered_fixtures.key?(fixture_name)
end