我的check_processors方法应该通过一个处理器数组(由processor_list参数提供)并输出一个包含相同处理器的新数组。
processor_list参数是nil或处理器数组,例如:
[
<Processor _id: 5158713883c336812100007f, _type: nil, created_at: 2013-03-31 17:24:08 UTC, updated_at: 2013-03-31 17:24:08 UTC, name: "my_mapping_processor", value: {"something"=>{"something_else"}}, type: "mapping", order: 0>,
<Processor _id: 5158713883c3368121000080, _type: nil, created_at: 2013-03-31 17:24:08 UTC, updated_at: 2013-03-31 17:24:08 UTC, name: "my_presentation_processor", value: {"something"=>{"something_else"}}, type: "presentation", order: 1>,
<Processor _id: 5348143r83c93641210d0080, _ty...
]
check_processors方法应验证是否存在 2 个特定处理器(由处理器的类型属性识别)。如果没有找到type == "mapping" 的处理器,create_mapping_processor应该它。或者,如果没有找到type == "presentation" 的处理器,create_presentation_processor应该创建它。
下面的代码可以工作,但它所做的工作非常简单,必须用更少的代码就可以实现。
def check_processors(processor_list = nil)
processors = []
if processor_list == nil
mapping_processor = create_mapping_processor
presentation_processor = create_presentation_processor
processors.push(mapping_processor)
processors.push(presentation_processor)
else
mapping_processor_exists = false
presentation_processor_exists = false
processor_list.each do |processor|
processors.push(processor)
mapping_processor_exists = true if processor.type == "mapping"
presentation_processor_exists = true if processor.type == "presentation"
end
if mapping_processor_exists == false
mapping_processor = create_mapping_processor
processors.push(mapping_processor)
end
if presentation_processor_exists == false
presentation_processor = create_presentation_processor
processors.push(presentation_processor)
end
end
processors #=> [<Processor _id: 5158713883c336812100007f, _type: nil, created_at: 2013-03-31 17:24:08 UTC, updated_at: 2013-03-31 17:24:08 UTC, name: "my_mapping_processor", value: {"something"=>{"something_else"}}, type: "mapping", order: 0>, <Processor _id: 5158713883c3368121000080, _type: nil, created_at: 2013-03-31 17:24:08 UTC, updated_at: 2013-03-31 17:24:08 UTC, name: "my_presentation_processor", value: {"something"=>{"something_else"}}, type: "presentation", order: 1>, <Processor _id: 5348143r83c93641210d0080, _ty...]
end
def create_mapping_processor
mapping_processor = Processor.new(:name => "my_mapping_processor", :value => {"something"=>{"something_else"}}, :type => "mapping", :order => 0)
mapping_processor
end
def create_presentation_processor
presentation_processor = Processor.new(:name => "my_presentation_processor", :value => {"something"=>{"something_else"}}, :type => "presentation", :order => 1)
presentation_processor
end