Class: Marshalsea::Marshal::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/marshalsea/marshal/node.rb

Constant Summary collapse

STRING_BACKED_TYPES =
%i[string regexp].freeze
WRAPPER_TYPES =
%i[user_class extended].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, tag: nil, value: nil, class_name: nil, undecoded_tail: nil) ⇒ Node

Returns a new instance of Node.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/marshalsea/marshal/node.rb', line 15

def initialize(type:, tag: nil, value: nil, class_name: nil, undecoded_tail: nil)
  @type = type
  @tag = tag
  @value = value
  @class_name = class_name
  @undecoded_tail = undecoded_tail
  @children = []
  @instance_variables_map = {}
  @instance_variable_pairs = []
  @auxiliary = []
end

Instance Attribute Details

#auxiliaryObject (readonly)

Returns the value of attribute auxiliary.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def auxiliary
  @auxiliary
end

#childrenObject (readonly)

Returns the value of attribute children.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def children
  @children
end

#class_nameObject

Returns the value of attribute class_name.



13
14
15
# File 'lib/marshalsea/marshal/node.rb', line 13

def class_name
  @class_name
end

#instance_variable_pairsObject (readonly)

Returns the value of attribute instance_variable_pairs.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def instance_variable_pairs
  @instance_variable_pairs
end

#instance_variables_mapObject (readonly)

Returns the value of attribute instance_variables_map.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def instance_variables_map
  @instance_variables_map
end

Returns the value of attribute link_target.



13
14
15
# File 'lib/marshalsea/marshal/node.rb', line 13

def link_target
  @link_target
end

#regexp_optionsObject

Returns the value of attribute regexp_options.



13
14
15
# File 'lib/marshalsea/marshal/node.rb', line 13

def regexp_options
  @regexp_options
end

#tagObject (readonly)

Returns the value of attribute tag.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def tag
  @tag
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def type
  @type
end

#undecoded_tailObject (readonly)

Returns the value of attribute undecoded_tail.



11
12
13
# File 'lib/marshalsea/marshal/node.rb', line 11

def undecoded_tail
  @undecoded_tail
end

#valueObject

Returns the value of attribute value.



13
14
15
# File 'lib/marshalsea/marshal/node.rb', line 13

def value
  @value
end

Instance Method Details

#dispatches_key_methods?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/marshalsea/marshal/node.rb', line 43

def dispatches_key_methods?
  !hash_dispatcher.nil? || !eql_dispatcher.nil?
end

#each {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



95
96
97
98
99
100
101
# File 'lib/marshalsea/marshal/node.rb', line 95

def each(&block)
  return enum_for(:each) unless block

  yield self
  children.each { |child| child.each(&block) }
  auxiliary.each { |child| child.each(&block) }
end

#effective_class_nameObject



84
85
86
# File 'lib/marshalsea/marshal/node.rb', line 84

def effective_class_name
  link_target ? link_target.class_name : class_name
end

#eql_dispatcher(seen = {}.compare_by_identity) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/marshalsea/marshal/node.rb', line 58

def eql_dispatcher(seen = {}.compare_by_identity)
  return nil if seen.key?(self)

  seen[self] = true
  return link_target&.eql_dispatcher(seen) if type == :object_link
  return member_dispatcher(:eql_dispatcher, seen) unless class_name

  self
end

#fully_decoded?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/marshalsea/marshal/node.rb', line 27

def fully_decoded?
  undecoded_tail.nil?
end

#gated?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/marshalsea/marshal/node.rb', line 39

def gated?
  Constants::GATED_SINK_TAGS.include?(tag)
end

#hash_dispatcher(seen = {}.compare_by_identity) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/marshalsea/marshal/node.rb', line 47

def hash_dispatcher(seen = {}.compare_by_identity)
  return nil if seen.key?(self)

  seen[self] = true
  return link_target&.hash_dispatcher(seen) if type == :object_link
  return member_dispatcher(:hash_dispatcher, seen) unless class_name
  return nil if WRAPPER_TYPES.include?(type) && string_backed?

  self
end

#member_dispatcher(probe, seen) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/marshalsea/marshal/node.rb', line 68

def member_dispatcher(probe, seen)
  children.each do |child|
    found = child.public_send(probe, seen)
    return found if found
  end
  nil
end

#range_endpointsObject



76
77
78
79
80
81
82
# File 'lib/marshalsea/marshal/node.rb', line 76

def range_endpoints
  instance_variable_pairs.filter_map do |name, value|
    next unless Constants::RANGE_ENDPOINT_IVARS.include?(name.value)

    value if value.effective_class_name
  end
end

#sealObject



103
104
105
106
107
108
109
110
111
112
# File 'lib/marshalsea/marshal/node.rb', line 103

def seal
  value.freeze
  class_name.freeze
  undecoded_tail.freeze
  children.freeze
  auxiliary.freeze
  instance_variables_map.freeze
  instance_variable_pairs.freeze
  freeze
end

#sink?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/marshalsea/marshal/node.rb', line 31

def sink?
  Constants::SINK_TAGS.include?(tag)
end

#sink_methodObject



35
36
37
# File 'lib/marshalsea/marshal/node.rb', line 35

def sink_method
  Constants::SINK_METHODS[tag]
end

#string_backed?Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
# File 'lib/marshalsea/marshal/node.rb', line 88

def string_backed?
  wrapped = children.first
  return false unless wrapped

  STRING_BACKED_TYPES.include?(wrapped.type)
end