Class: ResoTransport::Property

Inherits:
Struct
  • Object
show all
Defined in:
lib/reso_transport/property.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



90
91
92
# File 'lib/reso_transport/property.rb', line 90

def method_missing(name, *args, &block)
  self.attrs[name] || self.attrs[camelize(name)] || super
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs

Returns:

  • (Object)

    the current value of attrs



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def attrs
  @attrs
end

#complex_typeObject

Returns the value of attribute complex_type

Returns:

  • (Object)

    the current value of complex_type



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def complex_type
  @complex_type
end

#data_typeObject

Returns the value of attribute data_type

Returns:

  • (Object)

    the current value of data_type



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def data_type
  @data_type
end

#entity_typeObject

Returns the value of attribute entity_type

Returns:

  • (Object)

    the current value of entity_type



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def entity_type
  @entity_type
end

#enumObject

Returns the value of attribute enum

Returns:

  • (Object)

    the current value of enum



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def enum
  @enum
end

#multiObject

Returns the value of attribute multi

Returns:

  • (Object)

    the current value of multi



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def multi
  @multi
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



2
3
4
# File 'lib/reso_transport/property.rb', line 2

def name
  @name
end

Class Method Details

.from_stream(args) ⇒ Object



4
5
6
# File 'lib/reso_transport/property.rb', line 4

def self.from_stream(args)
  new(args["Name"], args["Type"], args)
end

Instance Method Details

#encode(value) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/reso_transport/property.rb', line 32

def encode(value)
  case value
  when Array
    value.map {|v| parser_object.encode_value(v) }
  else
    parser_object.encode_value(value)
  end
end

#encode_value(value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reso_transport/property.rb', line 41

def encode_value(value)
  case data_type
  when "Edm.String"
    "'#{value}'"
  when "Edm.DateTimeOffset"
    if value.respond_to?(:to_datetime)
      value.to_datetime.strftime(ODATA_TIME_FORMAT)
    else
      DateTime.parse(value).strftime(ODATA_TIME_FORMAT)
    end
  else
    value
  end
end

#finalize_type(parser) ⇒ Object



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
86
87
88
# File 'lib/reso_transport/property.rb', line 60

def finalize_type(parser)
  type_name, is_collection = case self.data_type
  when /^Collection\((.*)\)$/ 
    [$1, true]
  when /^Edm\.(.*)$/
    [$1, false]
  else
    [self.data_type, false]
  end

  if enum = parser.enumerations.detect {|e| e.name == type_name }
    self.multi = is_collection || enum.is_flags
    self.enum = enum
  end

  schema_name, collection_name = ResoTransport.split_schema_and_class_name(type_name)
  if schema = parser.schemas.detect {|e| e.namespace == schema_name }
    if complex_type = schema.complex_types.detect {|c| c.name == collection_name }
      self.multi = is_collection
      self.complex_type = complex_type
    end

    if entity_type = schema.entity_types.detect {|et| et.name == collection_name }
      self.multi = is_collection
      self.entity_type = entity_type
    end
  end

end

#parse(value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/reso_transport/property.rb', line 8

def parse(value)
  case value
  when Array
    value.map {|v| parser_object.parse_value(v) }
  else
    if multi
      value.split(',').map(&:strip).map {|v| parser_object.parse_value(v) }
    else
      parser_object.parse_value(value)
    end
  end
end

#parse_value(value) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/reso_transport/property.rb', line 21

def parse_value(value)
  case data_type
  when "Edm.DateTimeOffset"
    DateTime.parse(value)
  when "Edm.Date"
    Date.parse(value)
  else
    value
  end
end

#parser_objectObject



56
57
58
# File 'lib/reso_transport/property.rb', line 56

def parser_object
  enum || complex_type || entity_type || self
end