Module: ServiceStack::DTO::ClassMethods

Defined in:
lib/servicestack/dto.rb

Instance Method Summary collapse

Instance Method Details

#all_propertiesObject

This DTO's properties, including the properties it inherits.



78
79
80
81
82
83
84
85
86
# File 'lib/servicestack/dto.rb', line 78

def all_properties
  to = {}
  ancestors.reverse.each do |klass|
    next unless klass.respond_to?(:properties)

    to.merge!(klass.properties)
  end
  to
end

#from_hash(hash) ⇒ Object

Creates a populated DTO from the Hash of a JSON API Response.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/servicestack/dto.rb', line 89

def from_hash(hash)
  return nil if hash.nil?

  instance = new
  props = all_properties
  hash.each do |key, value|
    attr, meta = find_property(props, key.to_s)
    next if attr.nil?

    instance.instance_variable_set("@#{attr}", Serializer.from_json_value(meta[:type], value))
  end
  instance
end

#from_json(json) ⇒ Object

Creates a populated DTO from a JSON string.



104
105
106
107
# File 'lib/servicestack/dto.rb', line 104

def from_json(json)
  require 'json'
  from_hash(::JSON.parse(json))
end

#propertiesObject

The wire name and Type of each property, overridden by generated DTOs.



73
74
75
# File 'lib/servicestack/dto.rb', line 73

def properties
  {}
end