Module: SubstAttr::Substitute

Extended by:
Substitute
Included in:
Substitute
Defined in:
lib/subst_attr/substitute.rb,
lib/subst_attr/substitute/null_object.rb

Defined Under Namespace

Modules: NullObject

Instance Method Summary collapse

Instance Method Details

#build(interface = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/subst_attr/substitute.rb', line 5

def build(interface=nil)
  if interface
    specialization = specialization(interface)
    return specialization if specialization
  end

  return NullObject.build(interface)
end

#call(attr_name, receiver) ⇒ Object



14
15
16
17
18
19
# File 'lib/subst_attr/substitute.rb', line 14

def call(attr_name, receiver)
  interface = receiver.send(attr_name).class
  substitute = build(interface)
  receiver.send :"#{attr_name}=", substitute
  substitute
end

#specialization(interface) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/subst_attr/substitute.rb', line 21

def specialization(interface)
  constant_name = :Substitute

  reflection = Reflect.(interface, constant_name, strict: false, ancestors: true)

  if reflection.nil?
    return nil
  end

  specialization_module = reflection.constant

  if specialization_module.equal?(self)
    return nil
  end

  unless specialization_module.respond_to?(:build)
    return nil
  end

  specialization_module.send(:build)
end