Module: FactoryCreateMethod::ClassMethods

Defined in:
lib/mysql_backup/utilities/factory_create_method.rb

Instance Method Summary collapse

Instance Method Details

#append_factory_method(&block) ⇒ Object



40
41
42
43
# File 'lib/mysql_backup/utilities/factory_create_method.rb', line 40

def append_factory_method &block
  orig = factory_create_base_class
  orig.define_method_with_value :factory_methods, [block] + factory_methods
end

#build_object(args = {}) ⇒ Object



34
35
36
37
38
# File 'lib/mysql_backup/utilities/factory_create_method.rb', line 34

def build_object args = {}
  create_object args
rescue
  nil
end

#create_object(args = {}) ⇒ Object

Creates the appropriate object.

Throws NoMethodError if no factory method responded to the arguments.

Use build_object to return nil instead of throwing an exception.



20
21
22
23
24
25
26
# File 'lib/mysql_backup/utilities/factory_create_method.rb', line 20

def create_object args = {}
  factory_create_base_class.factory_methods.each do |f|
    result = f.call args
    return result if result
  end
  raise FactoryCreateMethod::NoMatchingFactory, "No factory method matched #{args.inspect}"
end

#factory_methodsObject

Note that this method will be replaced on the first call to append_factory_method.



30
31
32
# File 'lib/mysql_backup/utilities/factory_create_method.rb', line 30

def factory_methods # nodoc
  return []
end

#new_if_class(klass, field) ⇒ Object



45
46
47
48
49
# File 'lib/mysql_backup/utilities/factory_create_method.rb', line 45

def new_if_class klass, field
  append_factory_method do |args|
    klass === args[field] && new(args)
  end
end