Module: RSpec::SleepingKingStudios::Concerns::ExampleConstants

Defined in:
lib/rspec/sleeping_king_studios/concerns/example_constants.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assign_constant(namespace, constant_name, constant_value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 10

def self.assign_constant namespace, constant_name, constant_value
  prior_value = DEFAULT_VALUE

  if namespace.const_defined?(constant_name)
    prior_value = namespace.const_get(constant_name)
  end # if

  namespace.const_set(constant_name, constant_value)

  yield
ensure
  if prior_value == DEFAULT_VALUE
    namespace.send :remove_const, constant_name
  else
    namespace.const_set(constant_name, prior_value)
  end # if-else
end

.guard_existing_constant!(namespace, constant_name) ⇒ Object

class method assign_constant

Raises:

  • (NameError)


28
29
30
31
32
33
34
35
36
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 28

def self.guard_existing_constant! namespace, constant_name
  return unless namespace.const_defined?(constant_name)

  message =
    "constant #{constant_name} is already defined with value "\
    "#{namespace.const_get(constant_name).inspect}"

  raise NameError, message
end

.resolve_base_class(value) ⇒ Object

class method guard_existing_constant!



38
39
40
41
42
43
44
45
46
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 38

def self.resolve_base_class value
  value = value.fetch(:base_class, nil) if value.is_a?(Hash)

  return Object if value.nil?

  return Object.const_get(value) if value.is_a?(String)

  value
end

.resolve_namespace(module_names) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 48

def self.resolve_namespace module_names
  last_defined = nil

  resolved =
    module_names.reduce(Object) do |ns, module_name|
      next ns.const_get(module_name) if ns.const_defined?(module_name)

      last_defined ||= { :namespace => ns, :module_name => module_name }

      ns.const_set(module_name, Module.new)
    end # reduce

  yield resolved
ensure
  if last_defined
    last_defined[:namespace].send(:remove_const, last_defined[:module_name])
  end # if
end

Instance Method Details

#example_class(class_name, base_class = nil, &block) ⇒ Object

class method resolve_namespace



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 67

def example_class class_name, base_class = nil, &block
  class_name = class_name.to_s if class_name.is_a?(Symbol)

  example_constant(class_name) do
    klass = Class.new(ExampleConstants.resolve_base_class(base_class))

    klass.define_singleton_method(:name) { class_name }
    klass.singleton_class.send(:alias_method, :inspect, :name)
    klass.singleton_class.send(:alias_method, :to_s,    :name)

    instance_exec(klass, &block) if block_given?

    klass
  end # example_constant
end

#example_constant(qualified_name, constant_value = DEFAULT_VALUE, force: false, &block) ⇒ Object

method example_class



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rspec/sleeping_king_studios/concerns/example_constants.rb', line 83

def example_constant qualified_name, constant_value = DEFAULT_VALUE, force: false, &block
  around(:example) do |wrapped_example|
    example = wrapped_example.example

    resolved_value =
      if constant_value == DEFAULT_VALUE
        block ? example.instance_exec(&block) : nil
      else
        constant_value
      end # if

    module_names  = qualified_name.to_s.split('::')
    constant_name = module_names.pop

    ExampleConstants.resolve_namespace(module_names) do |namespace|
      ExampleConstants.guard_existing_constant!(namespace, constant_name) unless force

      ExampleConstants.assign_constant(namespace, constant_name, resolved_value) do
        wrapped_example.call
      end # assign_constant
    end # resolve_namespace
  end # before example
end