lib/locabulary/item.rb in locabulary-0.5.0 vs lib/locabulary/item.rb in locabulary-0.5.1

- old
+ new

@@ -13,28 +13,40 @@ # @option predicate_name [String] # @return [Locabulary::Item] # @see Locabulary::Items def self.build(attributes = {}) predicate_name = attributes.fetch(:predicate_name) { attributes.fetch('predicate_name') } - builder_for(predicate_name: predicate_name).call(attributes) + class_to_instantiate(predicate_name: predicate_name).new(attributes) end # @api public # @since 0.2.1 + # @deprecated 0.6.0 Prefer instead class_to_instantiate # # Responsible for finding the appropriate Factory method for building a Locabulary::Item # # @param options [Hash] # @option predicate_name [String] Used for lookup of the correct Locabulary::Item type # @return [#call] A builder method (`.new` for the given constant) def self.builder_for(options = {}) + class_to_instantiate(options).method(:new) + end + + # @api public + # @since 0.5.1 + # + # Responsible for finding the appropriate class that will be instantiated + # + # @param options [Hash] + # @option options [String] :predicate_name Used for lookup of the correct Locabulary::Item type + # @return [Locabulary::Items::Base] + def self.class_to_instantiate(options = {}) predicate_name = options.fetch(:predicate_name) possible_class_name_for_predicate_name = predicate_name.singularize.classify - klass = begin + begin Items.const_get(possible_class_name_for_predicate_name) rescue NameError Items::Base end - klass.method(:new) end end end