-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathsmart_factory_wrapper.rb
122 lines (101 loc) · 2.64 KB
/
smart_factory_wrapper.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
require 'cypress_on_rails/configuration'
require 'cypress_on_rails/simple_rails_factory'
module CypressOnRails
class SmartFactoryWrapper
def self.instance
@instance ||= new(files: [], factory: SimpleRailsFactory)
end
def self.configure(files:, factory:, always_reload: true)
@instance = new(files: files, factory: factory, always_reload: always_reload)
end
def self.create(*args)
instance.create(*args)
end
def self.create_list(*args)
instance.create_list(*args)
end
def self.build(*args)
instance.build(*args)
end
def self.build_list(*args)
instance.build_list(*args)
end
def self.reload
instance.reload
end
# @return [Array]
attr_accessor :factory
attr_accessor :always_reload
def initialize(files:, factory:, always_reload: false,
kernel: Kernel, file_system: File, dir_system: Dir)
self.files = files
self.factory = factory
factory.definition_file_paths = []
self.always_reload = always_reload
@kernel = kernel
@file_system = file_system
@latest_mtime = nil
@dir_system = dir_system
end
def create(*options)
auto_reload
factory_name = options.shift
if options.last.is_a?(Hash)
args = options.pop
else
args = {}
end
factory.create(factory_name,*options.map(&:to_sym),args.symbolize_keys)
end
def create_list(*args)
auto_reload
factory.create_list(*args)
end
def build(*options)
auto_reload
factory_name = options.shift
if options.last.is_a?(Hash)
args = options.pop
else
args = {}
end
factory.build(factory_name, *options.map(&:to_sym), args.symbolize_keys)
end
def build_list(*args)
auto_reload
factory.build_list(*args)
end
def reload
@latest_mtime = current_latest_mtime
logger.info 'Loading Factories'
factory.reload
files.each do |file|
logger.debug "-- Loading: #{file}"
@kernel.load(file)
end
end
private
# @param [String,Array] arg
def files=(array)
array = [array] if array.is_a?(String)
@dir_array = array
end
# @return [Array<String>]
def files
Dir[*@dir_array]
end
def logger
CypressOnRails.configuration.logger
end
def current_latest_mtime
files.map{|file| @file_system.mtime(file) }.max
end
def auto_reload
return unless should_reload?
reload
end
def should_reload?
@always_reload || @latest_mtime.nil? || @latest_mtime < current_latest_mtime
end
end
end