class Rev::ApiSerializable
Utility class with instance methods for hash/JSON conversion
Public Class Methods
new(fields = {})
click to toggle source
Map given hash to instance properties
@param fields [Hash] of fields to initialize instance. See instance attributes for available fields.
# File lib/rev-api/api_serializable.rb, line 8 def initialize(fields = {}) fields.each { |k,v| self.instance_variable_set("@#{k.to_sym}", v) if self.methods.include? k.to_sym } end
Public Instance Methods
to_hash()
click to toggle source
Recursively convert object to hash @note stackoverflow.com/questions/1684588/how-to-do-ruby-object-serialization-using-json
@return [Hash] hash map of the object including all nested children
# File lib/rev-api/api_serializable.rb, line 16 def to_hash h = {} instance_variables.each do |e| o = instance_variable_get e.to_sym h[e[1..-1]] = (o.respond_to? :to_hash) ? o.to_hash : o; end h end
to_json(*args)
click to toggle source
Recursively convert object to JSON (internally utilizing hash)
# File lib/rev-api/api_serializable.rb, line 26 def to_json *args to_hash.to_json *args end