Module: StubFactory

Included in:
Class
Defined in:
lib/stub_factory.rb,
lib/stub_factory/helpers.rb,
lib/stub_factory/version.rb,
lib/stub_factory/exceptions.rb

Defined Under Namespace

Modules: Helpers Classes: HelperError, TemplateError

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_helper(helper, klass) ⇒ Object

Helper Handling #######

Raises:



35
36
37
38
39
40
41
42
43
44
# File 'lib/stub_factory.rb', line 35

def define_helper(helper, klass)
  raise HelperError, "A helper for #{helper} has already been defined" if helper_defined?(helper)
  @helpers << helper.to_sym

  Helpers.class_eval <<-STR
    def stub_#{helper}(vars: {}, methods: {})
      #{klass}.new_stub(vars: vars, methods: methods, template: :#{helper})
    end
  STR
end

.define_template(template_name) ⇒ Object

Template Handing #######

Raises:



15
16
17
18
19
20
21
22
# File 'lib/stub_factory.rb', line 15

def define_template(template_name)
  tn = template_name.to_sym
  raise TemplateError, "Template already defined" if template_defined?(tn)
  raise TemplateError, "Templates need to be defined with a block" unless block_given?
  values = yield
  raise TemplateError, "Block must contain a Hash" unless values.kind_of?(Hash)
  @templates[tn] = values
end

.helper_defined?(helper) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/stub_factory.rb', line 46

def helper_defined?(helper)
  @helpers.include?(helper.to_sym)
end

.recursive_require(rel_paths) ⇒ Object

Requiring Files #######



52
53
54
55
56
# File 'lib/stub_factory.rb', line 52

def recursive_require(rel_paths)
  rel_paths.each do |rel_path|
    require_path(rel_path)
  end
end

.require_path(rel_path) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/stub_factory.rb', line 58

def require_path(rel_path)
  path = File.expand_path(rel_path)

  if File.exists?(path)
    require "#{path}" if File.file?(path) && path.match(/\/(stub_|template_)[^\/]*$/)
    Dir["#{path}/*"].each { |file| require_path(file) }
  end
end

.template_defined?(template) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/stub_factory.rb', line 24

def template_defined?(template)
  return unless template
  @templates.has_key?(template.to_sym)
end

.template_for(template) ⇒ Object



29
30
31
# File 'lib/stub_factory.rb', line 29

def template_for(template)
  @templates[template.to_sym]
end

.to_underscored_symbol(class_name) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/stub_factory.rb', line 111

def to_underscored_symbol(class_name)
  # Namespace::TestClass to :namespace__test_class
  res = class_name.to_s.dup
  res.gsub!(/([a-z])([A-Z])/) { "#{$1}_#{$2}" }
  res.gsub!(/::/, "__")
  res.downcase.to_sym
end

Instance Method Details

#new_stub(vars: {}, methods: {}, template: to_underscored_symbol(self.name)) ⇒ Object

Public Method #######



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

def new_stub(vars: {}, methods: {}, template: to_underscored_symbol(self.name))
  obj = allocate

  if StubFactory.template_defined?(template)
    vars = StubFactory.template_for(template).merge(vars)
  end

  set_instance_variables(obj, vars)
  override_methods(obj, methods)
  obj
end