Module: Fixtury::TestHooks::ClassMethods

Defined in:
lib/fixtury/test_hooks.rb

Instance Method Summary collapse

Instance Method Details

#fixtury(*names, &definition) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fixtury/test_hooks.rb', line 21

def fixtury(*names, &definition)
  opts = names.extract_options!

  # define fixtures if blocks are given
  if block_given?
    raise ArgumentError, "A fixture cannot be defined in an anonymous class" if name.nil?

    namespace = fixtury_namespace

    ns = ::Fixtury.schema

    namespace.split("/").each do |ns_name|
      ns = ns.namespace(ns_name){}
    end

    names.map! do |fixture_name|
      ns.fixture(fixture_name, &definition)
      new_name = "/#{namespace}/#{fixture_name}"
      self.local_fixtury_dependencies += [new_name]
      new_name
    end

  # otherwise, just record the dependency
  else
    self.fixtury_dependencies += names.flatten.compact.map(&:to_s)
  end

  accessor_option = opts[:as]
  accessor_option = opts[:accessor] if accessor_option.nil? # old version, backwards compatability
  accessor_option = accessor_option.nil? ? true : accessor_option

  if accessor_option

    if accessor_option != true && names.length > 1
      raise ArgumentError, "A named :as option is only available when providing one fixture"
    end

    names.each do |fixture_name|
      method_name = accessor_option == true ? fixture_name.split("/").last : accessor_option
      ivar = :"@#{method_name}"

      class_eval <<-EV, __FILE__, __LINE__ + 1
        def #{method_name}
          return #{ivar} if defined?(#{ivar})

          value = fixtury("#{fixture_name}")
          #{ivar} = value
        end
      EV
    end
  end
end

#fixtury_namespaceObject



74
75
76
# File 'lib/fixtury/test_hooks.rb', line 74

def fixtury_namespace
  name.underscore
end