Class: RTransmission::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/rtransmission/type.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Type

Returns a new instance of Type.



51
52
53
# File 'lib/rtransmission/type.rb', line 51

def initialize(hash)
  @hash = hash
end

Class Method Details

.attribute(rpc_name, args = {}) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rtransmission/type.rb', line 43

def self.attribute(rpc_name, args = {})
  name = args[:name] || RTransmission::Client.rpc_name_to_ruby_name(rpc_name)
    
  self.send :define_method, name.to_s do
    RTransmission::Type.unmap(@hash[rpc_name], args[:type])
  end
end

.map(value, type) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rtransmission/type.rb', line 29

def self.map(value, type)
  if type
    if type.class == Array
      type = RTransmission::Type.type_to_class(type[0])
      value.map! { |v| type.map(v) }
    else
      type = RTransmission::Type.type_to_class(type)
      value = type.map(value)
    end
  end

  value 
end

.type_to_class(type) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/rtransmission/type.rb', line 7

def self.type_to_class(type)
  type = type.to_s
  type = '_' + type
  type.gsub!(/_./) { |x| x[1].upcase }

  RTransmission::Types.const_get(type)
end

.unmap(value, type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rtransmission/type.rb', line 15

def self.unmap(value, type)
  if type
    if type.class == Array
      type = RTransmission::Type.type_to_class(type[0])
      value.map! { |v| type.unmap(v) }
    else
      type = RTransmission::Type.type_to_class(type)
      value = type.unmap(value)
    end
  end

  value 
end