Sha256: e98d32d8af7a9a18fbfb441f002dc705f6eea19cbb4101b55c6e5895f128e814

Contents?: true

Size: 967 Bytes

Versions: 14

Compression:

Stored size: 967 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

14 entries across 14 versions & 1 rubygems

Version Path
rev-api-2.4.0 lib/rev-api/api_serializable.rb
rev-api-2.3.2 lib/rev-api/api_serializable.rb
rev-api-2.3.0 lib/rev-api/api_serializable.rb
rev-api-2.2.1 lib/rev-api/api_serializable.rb
rev-api-2.1.0 lib/rev-api/api_serializable.rb
rev-api-2.0.3 lib/rev-api/api_serializable.rb
rev-api-2.0.2 lib/rev-api/api_serializable.rb
rev-api-2.0.1 lib/rev-api/api_serializable.rb
rev-api-2.0.0 lib/rev-api/api_serializable.rb
rev-api-1.0.4 lib/rev-api/api_serializable.rb
rev-api-1.0.3 lib/rev-api/api_serializable.rb
rev-api-1.0.2 lib/rev-api/api_serializable.rb
rev-api-1.0.1 lib/rev-api/api_serializable.rb
rev-api-1.0.0 lib/rev-api/api_serializable.rb