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.6.2"
APPLICATION =
"application"
APPLICATION_MATCHER =
Regexp.compile("(\/#{APPLICATION}|#{APPLICATION}\/|#{APPLICATION})")
EMPTY_STRING =
""
FORWARD_SLASH =
"/"
LAMP =
"_lamp"
REGISTER_FIXTURE_ALIASES =
[:register, :fixture, :rub, :wish]
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)
UnregisteredFixtureError =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



17
18
19
# File 'lib/magic_lamp.rb', line 17

def configuration
  @configuration
end

.registered_fixturesObject

Returns the value of attribute registered_fixtures.



17
18
19
# File 'lib/magic_lamp.rb', line 17

def registered_fixtures
  @registered_fixtures
end

Class Method Details

.configure(&block) ⇒ Object



39
40
41
42
# File 'lib/magic_lamp.rb', line 39

def configure(&block)
  raise_missing_block_error(block, __method__)
  block.call(configuration)
end

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



44
45
46
47
48
49
# File 'lib/magic_lamp.rb', line 44

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



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

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



96
97
98
99
100
101
102
# File 'lib/magic_lamp.rb', line 96

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



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

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



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

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



55
56
57
58
59
# File 'lib/magic_lamp.rb', line 55

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

.load_lamp_filesObject



90
91
92
93
94
# File 'lib/magic_lamp.rb', line 90

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

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



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

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)


51
52
53
# File 'lib/magic_lamp.rb', line 51

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