Sha256: 25749d9358d507cf41987762b54d0b0c4a6e8c5cc6561b2bcacd5c80d9bdf5ce
Contents?: true
Size: 997 Bytes
Versions: 3
Compression:
Stored size: 997 Bytes
Contents
module Rev # Utility class with instance methods for hash/JSON conversion class ApiSerializable # Map given hash to instance properties # # @param fields [Hash] of fields to initialize instance. See instance attributes for available fields. def initialize(fields = {}) fields.each { |k,v| self.instance_variable_set("@#{k.to_sym}", v) if self.methods.include? k.to_sym } end # Recursively convert object to hash # @note http://stackoverflow.com/questions/1684588/how-to-do-ruby-object-serialization-using-json # # @return [Hash] hash map of the object including all nested children 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 # Recursively convert object to JSON (internally utilizing hash) def to_json *args to_hash.to_json *args end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rev-api-2.5.0 | lib/rev-api/api_serializable.rb |
rev-api-2.3.1 | lib/rev-api/api_serializable.rb |
rev-api-2.2.0 | lib/rev-api/api_serializable.rb |