Class: Factrey::Ref

Inherits:
Object
  • Object
show all
Defined in:
lib/factrey/ref.rb,
lib/factrey/ref/defer.rb,
lib/factrey/ref/builder.rb,
lib/factrey/ref/resolver.rb,
lib/factrey/ref/shorthand_methods.rb

Overview

Represents a reference that can be embedded in the data and resolved later.

Refs and Defers are usually created through ShorthandMethods#ref.

Examples:

# Some data containing several references:
include Factrey::Ref::ShorthandMethods
some_data1 = [12, ref.foo, 34, ref.bar, 56]
some_data2 = { foo: ref.foo, foobar: ref { |foo, bar| foo + bar } }

# Resolve references by a `mapping` hash table:
mapping = { foo: 'hello', bar: 'world' }
resolver = Factrey::Ref::Resolver.new { mapping.fetch(_1) }
resolver.resolve(some_data1) #=> [12, 'hello', 34, 'world', 56]
resolver.resolve(some_data2) #=> { foo: 'hello', foobar: 'helloworld' }

Defined Under Namespace

Modules: ShorthandMethods Classes: Builder, Defer, Resolver

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Ref

Returns a new instance of Ref.

Parameters:

  • name (Symbol)

Raises:

  • (TypeError)


28
29
30
31
32
# File 'lib/factrey/ref.rb', line 28

def initialize(name)
  raise TypeError, "name must be a Symbol" unless name.is_a?(Symbol)

  @name = name
end

Instance Attribute Details

#nameSymbol (readonly)

Returns:

  • (Symbol)


25
26
27
# File 'lib/factrey/ref.rb', line 25

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



34
# File 'lib/factrey/ref.rb', line 34

def ==(other) = other.is_a?(Ref) && other.name == name

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
# File 'lib/factrey/ref.rb', line 36

def eql?(other) = self == other

#hashObject



38
# File 'lib/factrey/ref.rb', line 38

def hash = name.hash