Class: Simrpc::Schema::DataFieldDef

Inherits:
Object
  • Object
show all
Defined in:
lib/simrpc/schema.rb

Overview

Data field defintion containing a type, name, and value. Optinally an associated class or type may be given. Associated only valid for :obj and :array types, and

must be set to associated ClassDef or Type (:array only)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ DataFieldDef

Returns a new instance of DataFieldDef.



75
76
77
78
79
80
# File 'lib/simrpc/schema.rb', line 75

def initialize(args = {})
   @type = args[:type] unless args[:type].nil?
   @name = args[:name] unless args[:name].nil?
   @associated = args[:associated] unless args[:associated].nil?
   @ignore_null = !args[:ignore_null].nil? && args[:ignore_null]
end

Instance Attribute Details

#associatedObject

Returns the value of attribute associated.



67
68
69
# File 'lib/simrpc/schema.rb', line 67

def associated
  @associated
end

#defaultObject

Indicates the default value of the field



73
74
75
# File 'lib/simrpc/schema.rb', line 73

def default
  @default
end

#ignore_nullObject

Indicates this data field should be ignored if it has a null value



70
71
72
# File 'lib/simrpc/schema.rb', line 70

def ignore_null
  @ignore_null
end

#nameObject

Returns the value of attribute name.



67
68
69
# File 'lib/simrpc/schema.rb', line 67

def name
  @name
end

#typeObject

Returns the value of attribute type.



67
68
69
# File 'lib/simrpc/schema.rb', line 67

def type
  @type
end

Instance Method Details

#associated_class_def(schema_def) ⇒ Object

helper method to lookup and return the class definition corresponding to the associated attribte in the specified schema def



91
92
93
# File 'lib/simrpc/schema.rb', line 91

def associated_class_def(schema_def)
  find_class_def(@associated, schema_def) unless @associated.nil? 
end

#find_class_def(cl_name, schema_def) ⇒ Object

helper method to lookup and return the specified class name in the specified schema



84
85
86
87
# File 'lib/simrpc/schema.rb', line 84

def find_class_def(cl_name, schema_def)
  return schema_def.classes.find { |cl| cl.name == cl_name.to_s } unless cl_name.nil? || schema_def.nil?
  nil
end

#from_s(str, schema_def = nil, converted_classes = []) ⇒ Object

convert given string representation of this data field into its original value. Provide schema_def for :obj or :array data fields associated w/ non-primitive types # coverted_classes is a recursive helper array used/maintained internally



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/simrpc/schema.rb', line 143

def from_s(str, schema_def = nil, converted_classes = [])
  if str == "" # || str == "nil" # FIXME uncomment & test
    return nil

  elsif Schema::is_primitive?(@type)
    return Schema::primitive_from_str(@type, str)

  elsif @type == :array
    res = []
    cl_def = associated_class_def(schema_def) unless Schema::is_primitive?(@associated)
    alen = str[0...4].to_i
    apos = 4
    (0...alen).each { |i|
      elen = str[apos...apos+4].to_i
      parsed = str[apos+4...apos+4+elen]
      if Schema::is_primitive?(@associated)
        p =  Schema::primitive_from_str(@associated, parsed)
        res.push p
      else
        res.push cl_def.from_s(parsed, schema_def, converted_classes)
      end
      apos = apos+4+elen
    }
    return res

  # elsif @type == :map # TODO

  elsif @type == :obj
    cl_def = associated_class_def(schema_def)

    # if associated class isn't specified,  parse the class name, 
    # providing for generic object support
    if cl_def.nil?
      cnlen = str[0...4].to_i
      cname = str[4...cnlen+4]
      str = str[cnlen+4...str.size]
      cl_def = find_class_def(cname, schema_def)
      raise InvalidSchemaClass.new("cannot find #{cname} in schema") if cl_def.nil?
    end

    return cl_def.from_s(str, schema_def, converted_classes)

  end
end

#to_s(value, schema_def = nil, converted_classes = []) ⇒ Object

convert given value of this data field into a string. Provide schema_def for :obj or :array data fields associated w/ a non-primitive type. converted_classes is a recursive helper array used/maintained internally



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/simrpc/schema.rb', line 98

def to_s(value, schema_def = nil, converted_classes = [])
   if value.nil?
      return ""
   elsif Schema::is_primitive?(@type)
     return value.to_s

   elsif type == :array
     str = "%04d" % value.size
     value.each { |val|
       if Schema::is_primitive?(@associated)
          str += "%04d" % val.to_s.size
          str += val.to_s
       else
          cl_def = associated_class_def(schema_def)
          unless cl_def.nil?
            cl_s = cl_def.to_s(val, schema_def, converted_classes)
            str += "%04d" % cl_s.size
            str += cl_s
          end
       end
     }
     return str

  # elsif @type == :map # TODO

   elsif type == :obj
     cl_name = ""
     cl_def = associated_class_def(schema_def)

     # if associated class isn't specified,  store the class name,
     # providing for generic object support
     if cl_def.nil?
       cl_name = value.class.to_s.demodulize
       cl_def = find_class_def(cl_name, schema_def)
       raise InvalidSchemaClass.new("cannot find #{cl_name} in schema") if cl_def.nil?
       cl_name = "%04d" % cl_name.size + cl_name
     end

     return cl_name + cl_def.to_s(value, schema_def, converted_classes)
   end
end