Class: RbYAML::BaseRepresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/rbyaml/representer.rb

Direct Known Subclasses

SafeRepresenter

Constant Summary collapse

CLASSOBJ_TYPE =
Class
INSTANCE_TYPE =
Object
FUNCTION_TYPE =
Method
BUILTIN_FUNCTION_TYPE =
Method
MODULE_TYPE =
Module
@@yaml_representers =
{}
@@yaml_multi_representers =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(serializer, default_style = nil, default_flow_style = nil) ⇒ BaseRepresenter

Returns a new instance of BaseRepresenter.



15
16
17
18
19
20
# File 'lib/rbyaml/representer.rb', line 15

def initialize(serializer, default_style=nil, default_flow_style=nil)
  @serializer = serializer
  @default_style = default_style
  @default_flow_style = default_flow_style
  @represented_objects = {}
end

Class Method Details

.add_multi_representer(data_type, representer) ⇒ Object



91
92
93
# File 'lib/rbyaml/representer.rb', line 91

def self.add_multi_representer(data_type, representer)
  @@yaml_multi_representers[data_type] = representer
end

.add_representer(data_type, representer) ⇒ Object



87
88
89
# File 'lib/rbyaml/representer.rb', line 87

def self.add_representer(data_type, representer)
  @@yaml_representers[data_type] = representer
end

Instance Method Details

#get_classobj_bases(cls) ⇒ Object



34
35
36
# File 'lib/rbyaml/representer.rb', line 34

def get_classobj_bases(cls)
  [cls] + cls.ancestors
end

#ignore_aliases(data) ⇒ Object



136
137
138
# File 'lib/rbyaml/representer.rb', line 136

def ignore_aliases(data)
  false
end

#represent(data) ⇒ Object



22
23
24
25
26
# File 'lib/rbyaml/representer.rb', line 22

def represent(data)
  node = represent_data(data)
  @serializer.serialize(node)
  represented_objects = {}
end

#represent_data(data) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rbyaml/representer.rb', line 38

def represent_data(data)
  if ignore_aliases(data)
    alias_key = nil
  else
    alias_key = data.object_id
  end
  
  if !alias_key.nil?
    if @represented_objects.include?(alias_key)
      node = @represented_objects[alias_key]
      raise RepresenterError.new("recursive objects are not allowed: #{data}") if node.nil?
      return node
    end
    @represented_objects[alias_key] = nil
  end
  
  data_types = data.class.ancestors
  data_types = get_classobj_bases(data.class) + data_types if INSTANCE_TYPE === data

  if @@yaml_representers.include?(data_types[0])
    node = send(@@yaml_representers[data_types[0]],data)
  else
    rerun = true
    for data_type in data_types
      if @@yaml_multi_representers.include?(data_type)
        node = send(@@yaml_multi_representers[data_type],data)
        rerun = false
        break
      elsif @@yaml_representers.include?(data_type)
        node = send(@@yaml_representers[data_type],data)
        rerun = false
        break
      end
    end
    if rerun
      if @@yaml_multi_representers.include?(nil)
        node = send(@@yaml_multi_representers[nil],data)
      elsif @@yaml_representers.include?(nil)
        node = send(@@yaml_representers[nil],data)
      else
        node = ScalarNode.new(nil, data)
      end
    end
  end
    
  @represented_objects[alias_key] = node if !alias_key.nil?
  node
end

#represent_mapping(tag, mapping, flow_style = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rbyaml/representer.rb', line 111

def represent_mapping(tag, mapping, flow_style=nil)
  best_style = true
  if mapping.respond_to?(:keys)
    value = {}
    for item_key,item_value in mapping
      node_key = represent_data(item_key)
      node_value = represent_data(item_value)
      best_style = false if !node_key.__is_scalar && !node_key.flow_style
      best_style = false if !node_value.__is_scalar && !node_value.flow_style
      value[node_key] = node_value
    end
  else
    value = []
    for item_key, item_value in mapping
      node_key = represent_data(item_key)
      node_value = represent_data(item_value)
      best_style = false if !node_key.__is_scalar && !node_key.flow_style
      best_style = false if !node_value.__is_scalar && !node_value.flow_style
      value << [node_key, node_value]
    end
  end
  flow_style ||= (@flow_default_style || best_style)
  MappingNode.new(tag, value, flow_style)
end

#represent_scalar(tag, value, style = nil) ⇒ Object



95
96
97
98
# File 'lib/rbyaml/representer.rb', line 95

def represent_scalar(tag, value, style=nil)
  style ||= @default_style
  ScalarNode.new(tag, value, style)
end

#represent_sequence(tag, sequence, flow_style = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/rbyaml/representer.rb', line 100

def represent_sequence(tag, sequence, flow_style=nil)
  best_style = true
  value = sequence.map {|item| 
    node_item = represent_data(item)
    best_style = false if !node_item.__is_scalar && !node_item.flow_style
    node_item
  }
  flow_style ||= (@flow_default_style || best_style)
  SequenceNode.new(tag, value, flow_style)
end