Class: Foxy::Factory
- Inherits:
-
Object
show all
- Defined in:
- lib/foxy_factory.rb
Defined Under Namespace
Classes: ConstantIsNotClassError, ConstantIsNotModuleError, ConstantNotFoundError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*namespaces) ⇒ Factory
Returns a new instance of Factory.
15
16
17
18
19
|
# File 'lib/foxy_factory.rb', line 15
def initialize(*namespaces)
@ns_registry = namespaces.last.kind_of?(Hash) ? remove_last(namespaces) : {}
@base_namespace = namespace(namespaces)
end
|
Instance Attribute Details
#base_namespace ⇒ Object
Returns the value of attribute base_namespace.
3
4
5
|
# File 'lib/foxy_factory.rb', line 3
def base_namespace
@base_namespace
end
|
#ns_registry ⇒ Object
Returns the value of attribute ns_registry.
3
4
5
|
# File 'lib/foxy_factory.rb', line 3
def ns_registry
@ns_registry
end
|
Instance Method Details
#create(*args, &block) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/foxy_factory.rb', line 21
def create(*args, &block)
if !args.last.kind_of?(Hash) || !args.last[:args]
raise ArgumentError, "Last argument before block must be a hash of type :args => [...]" if block
raise ArgumentError, "Last argument must be a hash of type :args => [...]"
end
instance_args = args.delete(args.last)[:args]
ns_list, name = parse_args(args)
namespaces = get_namespaces(ns_list)
obj = find_class(namespaces, name).new *instance_args
if block
block.arity < 1 ? obj.instance_eval(&block) : block.call(obj)
end
end
|
#find_class(*args) ⇒ Object
35
36
37
38
39
40
41
42
|
# File 'lib/foxy_factory.rb', line 35
def find_class(*args)
ns_list, name = parse_args(args)
namespaces = get_namespaces(ns_list)
clazz = find_constant(*args)
return clazz if clazz.is_a? Class
ns = full_ns_name(namespaces, name)
raise Factory::ConstantIsNotClassError, "#{ns} is not a class"
end
|
#find_constant(*args) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/foxy_factory.rb', line 63
def find_constant(*args)
ns_list, name = parse_args(args)
namespaces = get_namespaces(ns_list)
ns = namespace(namespaces)
register_namespace(ns) if !registered?(ns)
cache = ns_cache(ns)
begin
if !cache[name]
full_name = ns_name(ns, name)
cache[name] = constantize(full_name)
end
cache[name]
rescue
raise Factory::ConstantNotFoundError, "No constant found for #{camelize(name)} in namespace #{real_ns_name(ns)}"
end
end
|
#find_module(*args) ⇒ Object
44
45
46
47
48
49
50
51
|
# File 'lib/foxy_factory.rb', line 44
def find_module(*args)
ns_list, name = parse_args(args)
namespaces = get_namespaces(ns_list)
m_dule = find_constant(*args)
return m_dule if m_dule.is_a?(Module)
ns = full_ns_name(namespaces, name)
raise Factory::ConstantIsNotModuleError, "#{ns} is not a module"
end
|
#find_module!(*args) ⇒ Object
53
54
55
56
57
58
59
60
|
# File 'lib/foxy_factory.rb', line 53
def find_module!(*args)
ns_list, name = parse_args(args)
namespaces = get_namespaces(ns_list)
m_dule = find_constant(*args)
return m_dule if m_dule.is_a?(Module) && !m_dule.is_a?(Class)
ns = full_ns_name(namespaces, name)
raise Factory::ConstantIsNotModuleError, "#{ns} is not a module"
end
|