Class: MR::ReadModel::JsonStructField

Inherits:
Struct
  • Object
show all
Defined in:
lib/mr/read_model/fields.rb

Direct Known Subclasses

JsonStructListField, JsonStructObjField

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



162
163
164
# File 'lib/mr/read_model/fields.rb', line 162

def name
  @name
end

#read_model_classObject

Returns the value of attribute read_model_class

Returns:

  • (Object)

    the current value of read_model_class



162
163
164
# File 'lib/mr/read_model/fields.rb', line 162

def read_model_class
  @read_model_class
end

#struct_class_nameObject

Returns the value of attribute struct_class_name

Returns:

  • (Object)

    the current value of struct_class_name



162
163
164
# File 'lib/mr/read_model/fields.rb', line 162

def struct_class_name
  @struct_class_name
end

Instance Method Details

#reader(read_model_data) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/mr/read_model/fields.rb', line 174

def reader(read_model_data)
  json_value = read_model_data[self.name]
  return json_value if json_value.nil?

  if json_value.kind_of?(String)
    begin
      return MR::JsonField.decode(json_value)
    rescue MR::JsonField::InvalidJSONError => exception
      message = "can't decode `#{self.name}` JSON: #{exception.message}"
      raise exception.class, message, exception.backtrace
    end
  else
    return json_value
  end
end

#struct_classObject



164
165
166
167
168
169
170
171
172
# File 'lib/mr/read_model/fields.rb', line 164

def struct_class
  @struct_class ||= if self.struct_class_name.kind_of?(String)
    names     = self.struct_class_name.to_s.split('::').reject{ |name| name.empty? }
    namespace = self.struct_class_name =~ /^::/ ? Object : self.read_model_class
    names.inject(namespace){ |o, name| o.const_get(name) }
  else
    self.struct_class_name
  end
end