Class: Brujula::TypeExtender::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/brujula/type_extender/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:) ⇒ Resource

Returns a new instance of Resource.



6
7
8
# File 'lib/brujula/type_extender/resource.rb', line 6

def initialize(definition:)
  @definition = definition
end

Instance Attribute Details

#definitionObject (readonly)

Returns the value of attribute definition.



4
5
6
# File 'lib/brujula/type_extender/resource.rb', line 4

def definition
  @definition
end

Instance Method Details

#apply_inherit_chainObject



20
21
22
23
24
25
# File 'lib/brujula/type_extender/resource.rb', line 20

def apply_inherit_chain
  object = definition.dup
  object = apply_type(object) unless definition.type.nil?
  object = apply_traits(object) unless definition.is.nil?
  object
end

#apply_traits(object) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/brujula/type_extender/resource.rb', line 33

def apply_traits(object)
  return object if definition.methods.nil?

  object.methods.each do |method|
    new_method = apply_traits_to_method(method)
    object.methods.merge(new_method.name, new_method)
  end
  object
end

#apply_traits_to_method(method) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/brujula/type_extender/resource.rb', line 43

def apply_traits_to_method(method)
  definition.is.inject(method.dup) do |object, trait|
    Brujula::Mergers::ObjectMerger.new(
      superinstance: trait, instance: object
    ).call
  end
end

#apply_type(object) ⇒ Object



27
28
29
30
31
# File 'lib/brujula/type_extender/resource.rb', line 27

def apply_type(object)
  Brujula::Mergers::ObjectMerger.new(
    superinstance: definition.type, instance: object
  ).call
end

#callObject



10
11
12
13
14
# File 'lib/brujula/type_extender/resource.rb', line 10

def call
  return definition if definition.type.nil? && definition.is.nil?

  extended_object
end

#extended_objectObject



16
17
18
# File 'lib/brujula/type_extender/resource.rb', line 16

def extended_object
  @extended_object ||= apply_inherit_chain
end