lib/hexx/dependencies.rb in hexx-5.3.0 vs lib/hexx/dependencies.rb in hexx-5.4.0
- old
+ new
@@ -85,15 +85,15 @@
# extend Hexx::Dependencies
# self.depends_on %w(some_class another_class)
# end
#
# @param [Array<String, Symbol>] names The list of names for dependencies.
- def depends_on(*names)
+ def depends_on(*names, default: nil)
names.flatten.each do |name|
DependencyName.new(name).validate
add_dependency_setter name
- add_dependency_getter name
+ add_dependency_getter name, default
end
end
private
@@ -161,15 +161,22 @@
Dependency.new(:#{ name }, value).validate;
@#{ name }= value.to_s;
end"
end
- def add_dependency_getter(name)
+ def add_dependency_getter(name, default)
instance_eval "
def #{ name };
- value = @#{ name };
- fail NotImplementedError unless value;
- value.constantize;
+ value = @#{ default_name name, default };
+ constantize(value);
end"
+ end
+
+ def constantize(value)
+ value.is_a?(Module) ? value : Kernel.const_get(value.to_s)
+ end
+
+ def default_name(name, default)
+ [name, default].compact.map(&:to_s).join " || "
end
end
end