forked from marcel/aws-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtures.rb
89 lines (76 loc) · 2.55 KB
/
fixtures.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'yaml'
module AWS
module S3
# When this file is loaded, for each fixture file, a module is created within the Fixtures module
# with the same name as the fixture file. For each fixture in that fixture file, a singleton method is
# added to the module with the name of the given fixture, returning the value of the fixture.
#
# For example:
#
# A fixture in <tt>buckets.yml</tt> named <tt>empty_bucket_list</tt> with value <tt><foo>hi!</foo></tt>
# would be made available like so:
#
# Fixtures::Buckets.empty_bucket_list
# => "<foo>hi!</foo>"
#
# Alternatively you can treat the fixture module like a hash
#
# Fixtures::Buckets[:empty_bucket_list]
# => "<foo>hi!</foo>"
#
# You can find out all available fixtures by calling
#
# Fixtures.fixtures
# => ["Buckets"]
#
# And all the fixtures contained in a given fixture by calling
#
# Fixtures::Buckets.fixtures
# => ["bucket_list_with_more_than_one_bucket", "bucket_list_with_one_bucket", "empty_bucket_list"]
module Fixtures
class << self
def create_fixtures
files.each do |file|
create_fixture_for(file)
end
end
def create_fixture_for(file)
fixtures = YAML.load_file(path(file))
fixture_module = Module.new
fixtures.each do |name, value|
fixture_module.module_eval(<<-EVAL, __FILE__, __LINE__)
def #{name}
#{value.inspect}
end
module_function :#{name}
EVAL
end
fixture_module.module_eval(<<-EVAL, __FILE__, __LINE__)
module_function
def fixtures
#{fixtures.keys.sort.inspect}
end
def [](name)
send(name) if fixtures.include?(name.to_s)
end
EVAL
const_set(module_name(file), fixture_module)
end
def fixtures
constants.sort
end
private
def files
Dir.glob(File.dirname(__FILE__) + '/fixtures/*.yml').map {|fixture| File.basename(fixture)}
end
def module_name(file_name)
File.basename(file_name, '.*').capitalize
end
def path(file_name)
File.join(File.dirname(__FILE__), 'fixtures', file_name)
end
end
create_fixtures
end
end
end