Class: FactoryToys::FFactory
- Inherits:
-
Object
- Object
- FactoryToys::FFactory
show all
- Defined in:
- lib/factory_toys/f_factory.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(filename) ⇒ FFactory
Returns a new instance of FFactory.
12
13
14
15
16
17
18
19
20
|
# File 'lib/factory_toys/f_factory.rb', line 12
def initialize(filename)
@filename = filename
@header = ["# last update: #{File.mtime(@filename)}"]
@header << "# Auto Generated Features"
@header << "# Generated: #{Time.now.to_s}"
@header << "# Source File: #{@filename}"
@header << ''
check_output_length
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
6
7
8
9
10
|
# File 'lib/factory_toys/f_factory.rb', line 6
def method_missing(name, *args, &block)
value = eval("@#{name}")
return value if value
super(name, *args, &block)
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
4
5
6
|
# File 'lib/factory_toys/f_factory.rb', line 4
def data
@data
end
|
#filename ⇒ Object
Returns the value of attribute filename.
3
4
5
|
# File 'lib/factory_toys/f_factory.rb', line 3
def filename
@filename
end
|
Instance Method Details
#add_feature(feature_def) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/factory_toys/f_factory.rb', line 60
def add_feature(feature_def)
raise FactoryToys::MissingFeatureDefinitionError if feature_def.blank?
eval(feature_def)
@header << eval('feature')
@header << ""
end
|
#add_option(all_options, elements) ⇒ Object
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/factory_toys/f_factory.rb', line 142
def add_option(all_options, elements)
options = all_options.clone
all_options = []
elements.each do |element_value|
cur_options = options.clone
cur_options.map!{|row| [element_value] + row }
all_options += cur_options
end
return all_options
end
|
#add_scenario(feature, option, feature_name) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/factory_toys/f_factory.rb', line 82
def add_scenario(feature, option, feature_name)
eval(self.option_string(feature, option))
scenario_name = self.get_scenario_name(feature_name)
raise MissingScenarioError, scenario_name unless self.data[scenario_name]
eval(self.data[scenario_name])
@output += eval(scenario_name.to_s).split("\n")
@output << ''
check_output_length
end
|
#check_output_length ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/factory_toys/f_factory.rb', line 22
def check_output_length
@outputs ||= []
unless @output && @output.size < FactoryToys.max_feature_length
@outputs << @output if @output
@output = []
end
end
|
#get_option_types(option, reverse = true) ⇒ Object
153
154
155
156
157
|
# File 'lib/factory_toys/f_factory.rb', line 153
def get_option_types(option, reverse=true)
return [option] unless option.is_a?(Array)
return option unless reverse
option.reverse
end
|
#get_options(scenario) ⇒ Object
126
127
128
129
130
131
132
133
134
|
# File 'lib/factory_toys/f_factory.rb', line 126
def get_options(scenario)
all_options = [[]]
if scenario[:foreach]
self.get_option_types(scenario[:foreach]).each do |element|
all_options = process_options(all_options, scenario[element], element)
end
end
return all_options
end
|
#get_scenario_name(feature_name) ⇒ Object
92
93
94
95
96
|
# File 'lib/factory_toys/f_factory.rb', line 92
def get_scenario_name(feature_name)
feature_name =~ /^@(.+)_#{FactoryToys.scenarios}$/
identifier = $1
return "#{identifier}_#{FactoryToys.scenario}".to_sym
end
|
#option_string(scenario, option) ⇒ Object
104
105
106
107
108
109
|
# File 'lib/factory_toys/f_factory.rb', line 104
def option_string(scenario, option)
return '' unless scenario[:foreach]
foreach = get_option_types(scenario[:foreach], false)
raise InternalForEachError unless foreach.size == option.size
foreach.map(&:to_s).join(', ') + ' = ' + option.map{|o| self.write_value(o)}.join(', ')
end
|
#output_filename ⇒ Object
37
38
39
40
41
|
# File 'lib/factory_toys/f_factory.rb', line 37
def output_filename
filename = File.basename(@filename, ".rb")
dir = FactoryToys.features_location + filename
File.join(dir, "#{filename}_0.feature")
end
|
#process_file(conf_names) ⇒ Object
67
68
69
70
71
72
|
# File 'lib/factory_toys/f_factory.rb', line 67
def process_file(conf_names)
scenarios = conf_names.find_all{|var| var =~ /_#{FactoryToys.scenarios}$/}
scenarios.each do |feature_name|
self.process_scenarios(feature_name)
end
end
|
#process_files ⇒ Object
30
31
32
33
34
35
|
# File 'lib/factory_toys/f_factory.rb', line 30
def process_files
eval(self.data[:base])
self.add_feature(self.data[:feature])
conf_names = self.instance_variables
self.process_file(conf_names)
end
|
#process_options(all_options, elements, element) ⇒ Object
136
137
138
139
140
|
# File 'lib/factory_toys/f_factory.rb', line 136
def process_options(all_options, elements, element)
raise MissingForeachListError, element.to_s unless elements.is_a?(Array)
return self.add_option(all_options, elements)
end
|
#process_scenarios(feature_name) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/factory_toys/f_factory.rb', line 74
def process_scenarios(feature_name)
feature = eval(feature_name)
options = self.get_options(feature)
options.each do |option|
self.add_scenario(feature, option, feature_name)
end
end
|
#write ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/factory_toys/f_factory.rb', line 43
def write
process_files
filename = File.basename(@filename, ".rb")
dir = File.join(FactoryToys.features_location, filename)
FileUtils.mkdir_p(dir)
FileUtils.rm_r(Dir.glob(File.join(dir, "*.feature")))
@outputs.each_with_index do |output, i|
output_file_name = File.join(dir, "#{filename}_#{i}.feature")
File.open(output_file_name, 'w') do |f|
output_str = (@header + output).join("\n")
f.puts output_str
end
end
end
|
#write_value(o) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/factory_toys/f_factory.rb', line 111
def write_value(o)
case o
when Symbol
return ":#{o}"
when String
return "'#{o}'"
when Numeric
return o.to_s
when Array
"[#{o.map{|v| self.write_value(v).join(',')}}]"
when Hash
"{#{o.map{|k,v| self.write_value(k) + ' => ' + self.write_value(v)}.join(', ')}}"
end
end
|