Class: CBOR::Transform

Inherits:
Object show all
Defined in:
lib/cbor-transform.rb

Direct Known Subclasses

Transform_j, Transform_jr

Instance Method Summary collapse

Instance Method Details

#transform(obj) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cbor-transform.rb', line 3

def transform(obj)
  case obj
  when NilClass
    transform_nil(obj)
  when FalseClass, TrueClass
    transform_bool(obj)
  when CBOR::Simple
    transform_simple(obj)
  when Float
    transform_float(obj)
  when Integer
    transform_integer(obj)
  # XXX should probably handle Symbol
  when String
    case obj.encoding
    when Encoding::BINARY
      transform_bytes(obj)
    else
      transform_text(obj)
    end
  when Array
    transform_array(obj)
  when Hash
    transform_hash(obj)
  when CBOR::Tagged
    transform_tag(obj)
  end
end

#transform_array(obj) ⇒ Object



60
61
62
# File 'lib/cbor-transform.rb', line 60

def transform_array(obj)
  obj.map {|x| transform(x)}
end

#transform_bool(obj) ⇒ Object



36
37
38
# File 'lib/cbor-transform.rb', line 36

def transform_bool(obj)
  obj
end

#transform_bytes(obj) ⇒ Object



52
53
54
# File 'lib/cbor-transform.rb', line 52

def transform_bytes(obj)
  obj
end

#transform_float(obj) ⇒ Object



44
45
46
# File 'lib/cbor-transform.rb', line 44

def transform_float(obj)
  obj
end

#transform_hash(obj) ⇒ Object



64
65
66
# File 'lib/cbor-transform.rb', line 64

def transform_hash(obj)
  Hash[obj.map {|k, v| [transform(k), transform(v)]}]
end

#transform_integer(obj) ⇒ Object



48
49
50
# File 'lib/cbor-transform.rb', line 48

def transform_integer(obj)
  obj
end

#transform_nil(obj) ⇒ Object



32
33
34
# File 'lib/cbor-transform.rb', line 32

def transform_nil(obj)
  obj
end

#transform_simple(obj) ⇒ Object



40
41
42
# File 'lib/cbor-transform.rb', line 40

def transform_simple(obj)
  obj
end

#transform_tag(obj) ⇒ Object



68
69
70
# File 'lib/cbor-transform.rb', line 68

def transform_tag(obj)
  CBOR::Tagged.new(transform(obj.tag), transform(obj.value))
end

#transform_text(obj) ⇒ Object



56
57
58
# File 'lib/cbor-transform.rb', line 56

def transform_text(obj)
  obj
end