Class: ObjectForge::Crucible

Inherits:
UnBasicObject show all
Defined in:
lib/object_forge/crucible.rb

Overview

Note:

This class is not intended to be used directly, but it’s not a private API.

Melting pot for the forged object’s attributes.

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UnBasicObject

#class, #eql?, #freeze, #frozen?, #hash, #inspect, #is_a?, #pretty_print, #pretty_print_cycle, #respond_to?, #to_s

Constructor Details

#initialize(attributes) ⇒ Crucible

Returns a new instance of Crucible.

Parameters:

  • attributes (Hash{Symbol => Proc, Any})

    initial attributes

Since:

  • 0.1.0



37
38
39
40
41
42
# File 'lib/object_forge/crucible.rb', line 37

def initialize(attributes)
  super()
  @attributes = attributes
  @resolved_attributes = ::Set.new
  @resolving_attributes = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Any (private) Also known as: []

Get the value of the attribute name.

To prevent problems with calling methods which may be defined, #[] can be used instead.

Examples:

attrs = {
  name: -> { "Name" },
  description: -> { name.downcase },
  duration: -> { rand(1000) }
}
Crucible.call(attrs)
  # => { name: "Name", description: "name", duration: 123 }

using conflicting and reserved names

attrs = {
  "[]": -> { "Brackets" },
  "[]=": -> { "#{self[:[]]} are brackets" },
  "!": -> { "#{self[:[]=]}!" }
}
Crucible.resolve(attrs)
  # => { "[]": "Brackets", "[]=": "Brackets are brackets", "!": "Brackets are brackets!" }

Parameters:

  • name (Symbol)

Returns:

  • (Any)

Raises:

Since:

  • 0.1.0



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/object_forge/crucible.rb', line 91

def method_missing(name) # rubocop:disable Metrics/MethodLength
  if @attributes.key?(name)
    if @resolving_attributes.include?(name)
      raise_circular_dependency_error!(name)
    elsif !@resolved_attributes.include?(name) && (::Proc === @attributes[name])
      begin
        @resolving_attributes << name
        @attributes[name] = instance_exec(&@attributes[name])
        @resolved_attributes << name
      ensure
        @resolving_attributes.pop
      end
    end
    @attributes[name]
  else
    super
  end
end

Class Method Details

.call(attributes) ⇒ Hash{Symbol => Any} Also known as: resolve

Note:

This method destructively modifies initial attributes.

Resolve all attributes by calling their Procs, using a new instance as evaluation context.

Parameters:

  • attributes (Hash{Symbol => Proc, Any})

    initial attributes

Returns:

  • (Hash{Symbol => Any})

    resolved attributes

See Also:

Since:

  • 0.1.0



27
28
29
# File 'lib/object_forge/crucible.rb', line 27

def call(attributes)
  new(attributes).resolve!
end

Instance Method Details

#resolve!Hash{Symbol => Any}

Note:

This method destructively modifies initial attributes.

Resolve all attributes by calling their Procs, using self as the evaluation context.

Attributes can freely refer to each other inside Procs through bareword names or #[]. However, make sure to avoid cyclic dependencies: they can’t be resolved and will raise ObjectForge::CircularAttributeDependencyError.

Returns:

  • (Hash{Symbol => Any})

    resolved attributes

Raises:

Since:

  • 0.1.0



57
58
59
60
# File 'lib/object_forge/crucible.rb', line 57

def resolve!
  @attributes.each_key { |name| method_missing(name) }
  @attributes
end