Class: Logue::ElementFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/logue/elements/element_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(writer) ⇒ ElementFactory



12
13
14
# File 'lib/logue/elements/element_factory.rb', line 12

def initialize writer
  @writer = writer
end

Instance Method Details

#to_complex_element(cls, msg, obj, context) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/logue/elements/element_factory.rb', line 31

def to_complex_element cls, msg, obj, context
  if obj && context.include?(obj.object_id)
    MsgObjElement.new msg, obj.object_id.to_s + " (recursed)", context, @writer
  else
    newlist = context.dup
    newlist << obj.object_id
    cls.new msg, obj, newlist, @writer
  end
end

#to_element(msg, obj, context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/logue/elements/element_factory.rb', line 16

def to_element msg, obj, context
  case obj
  when Element
    obj
  when Struct
    to_complex_element StructElement, msg, obj, context
  when Hash
    to_complex_element HashElement, msg, obj, context
  when Enumerable
    to_complex_element IndexedElement, msg, obj, context
  else
    to_msg_element msg, obj, context
  end
end

#to_msg_element(msg, obj, context) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/logue/elements/element_factory.rb', line 41

def to_msg_element msg, obj, context
  # avoid using == on obj:
  if obj.nil? || (obj.instance_of?(Object) && obj == ObjectUtil::NONE) || (obj.instance_of?(Symbol) && obj == :none)
    MsgElement.new msg, context, @writer
  else
    MsgObjElement.new msg, obj, context, @writer
  end
end