~ubuntu-branches/ubuntu/lucid/jruby/lucid

« back to all changes in this revision

Viewing changes to lib/ruby/1.9/yaml/yamlnode.rb

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Delafond
  • Date: 2009-12-09 17:30:55 UTC
  • Revision ID: james.westby@ubuntu.com-20091209173055-8ffzikq1768gywux
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# YAML::YamlNode class
 
3
#
 
4
require 'yaml/basenode'
 
5
 
 
6
module YAML
 
7
 
 
8
    #
 
9
    # YAML Generic Model container
 
10
    #
 
11
    class YamlNode
 
12
        include BaseNode
 
13
        attr_accessor :kind, :type_id, :value, :anchor
 
14
        def initialize(t, v)
 
15
            @type_id = t
 
16
            if Hash === v
 
17
                @kind = 'map'
 
18
                @value = {}
 
19
                v.each {|key,val|
 
20
                    @value[key.transform] = [key, val]
 
21
                }
 
22
            elsif Array === v
 
23
                @kind = 'seq'
 
24
                @value = v
 
25
            elsif String === v
 
26
                @kind = 'scalar'
 
27
                @value = v
 
28
            end
 
29
        end
 
30
 
 
31
        #
 
32
        # Transform this node fully into a native type
 
33
        #
 
34
        def transform
 
35
            t = nil
 
36
            if @value.is_a? Hash
 
37
                t = {}
 
38
                @value.each { |k,v|
 
39
                    t[ k ] = v[1].transform
 
40
                }
 
41
            elsif @value.is_a? Array
 
42
                t = []
 
43
                @value.each { |v|
 
44
                    t.push v.transform
 
45
                }
 
46
            else
 
47
                t = @value
 
48
            end
 
49
            YAML.transfer_method( @type_id, t )
 
50
        end
 
51
 
 
52
    end
 
53
 
 
54
end