Module: Locabulary::Item

Defined in:
lib/locabulary/item.rb

Overview

A container of builder methods for items

Since:

  • 0.1.0

Class Method Summary collapse

Class Method Details

.build(attributes = {}) ⇒ Locabulary::Item

A Factory method that is responsible for building the appropriate object given a :predicate_name and additional attributes.

Parameters:

  • attributes (Hash) (defaults to: {})
  • predicate_name (Hash)

    a customizable set of options

Returns:

See Also:

Since:

  • 0.2.1



16
17
18
19
# File 'lib/locabulary/item.rb', line 16

def self.build(attributes = {})
  predicate_name = attributes.fetch(:predicate_name) { attributes.fetch('predicate_name') }
  class_to_instantiate(predicate_name: predicate_name).new(attributes)
end

.builder_for(options = {}) ⇒ #call

Deprecated.

0.6.0 Prefer instead class_to_instantiate; This will be removed in 0.7.0.

Responsible for finding the appropriate Factory method for building a Locabulary::Item

Parameters:

  • options (Hash) (defaults to: {})
  • predicate_name (Hash)

    a customizable set of options

Returns:

  • (#call)

    A builder method (‘.new` for the given constant)

Since:

  • 0.2.1



30
31
32
# File 'lib/locabulary/item.rb', line 30

def self.builder_for(options = {})
  class_to_instantiate(options).method(:new)
end

.class_to_instantiate(options = {}) ⇒ Locabulary::Items::Base

Responsible for finding the appropriate class that will be instantiated

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :predicate_name (String)

    Used for lookup of the correct Locabulary::Item type

Returns:

Since:

  • 0.5.1



42
43
44
45
46
47
48
49
50
# File 'lib/locabulary/item.rb', line 42

def self.class_to_instantiate(options = {})
  predicate_name = options.fetch(:predicate_name)
  possible_class_name_for_predicate_name = predicate_name.singularize.classify
  begin
    "Locabulary::Items::#{possible_class_name_for_predicate_name}".constantize
  rescue NameError
    Items::Base
  end
end