Class: Fixture
- Inherits:
-
Object
show all
- Defined in:
- lib/fixture/fixture.rb
Constant Summary
collapse
- FIXTURE_FILE =
'fixtures.rb'
- FIXTURE_DIR =
'fixtures'
- @@prepared =
false
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass) ⇒ Fixture
Returns a new instance of Fixture.
2
3
4
|
# File 'lib/fixture/fixture.rb', line 2
def initialize(klass)
@klass = klass
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing ⇒ Object
17
18
19
20
21
|
# File 'lib/fixture/fixture.rb', line 17
def attribute( name, *args, &block)
val = args.first
val = block if block_given?
@candidate[:attributes][name] = val
end
|
Class Method Details
.assigneds ⇒ Object
33
34
35
|
# File 'lib/fixture/fixture.rb', line 33
def assigneds
@@assigneds ||= {}
end
|
.candidates ⇒ Object
37
38
39
|
# File 'lib/fixture/fixture.rb', line 37
def candidates
@@candidates ||= {}
end
|
.get(name) ⇒ Object
29
30
31
|
# File 'lib/fixture/fixture.rb', line 29
def get( name )
assigneds[name]
end
|
.load ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/fixture/fixture.rb', line 41
def load
prepare unless prepared?
candidates.each do |name, candidate|
candidate[:attributes].each do |attr, val|
candidate[:obj].send("#{attr}=", val.is_a?(Proc) ? val.call : val)
end
assigneds[name] = obj = candidate[:obj]
if obj.respond_to? :save!
obj.save!
elsif obj.respond_to? :save
obj.save
end
end
end
|
.manage(klass, &block) ⇒ Object
25
26
27
|
# File 'lib/fixture/fixture.rb', line 25
def manage( klass, &block )
self.new(klass).instance_eval &block
end
|
.prepare ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/fixture/fixture.rb', line 59
def prepare
%w(spec test).each do |dir|
require "#{dir}/#{FIXTURE_FILE}" if File.exists? "#{dir}/#{FIXTURE_FILE}"
Dir["#{dir}/#{FIXTURE_DIR}/*.rb"].each {|f| require f }
end
@@prepared = true
end
|
.prepared? ⇒ Boolean
67
68
69
|
# File 'lib/fixture/fixture.rb', line 67
def prepared?
!!@@prepared
end
|
Instance Method Details
#assign(name, &block) ⇒ Object
6
7
8
9
|
# File 'lib/fixture/fixture.rb', line 6
def assign( name, &block )
self.class.candidates[name] = @candidate = { obj: @klass.new, attributes: {} }
yield
end
|
#attribute(name, *args, &block) ⇒ Object
Also known as:
method_missing
11
12
13
14
15
|
# File 'lib/fixture/fixture.rb', line 11
def attribute( name, *args, &block)
val = args.first
val = block if block_given?
@candidate[:attributes][name] = val
end
|