Class Index [+]

Quicksearch

Sequel::Postgres::JSONDatabaseMethods

Methods enabling Database object integration with the json type.

Public Class Methods

extended(db) click to toggle source

Reset the conversion procs when extending the Database object, so it will pick up the json convertor. This is only done for the native postgres adapter.

     # File lib/sequel/extensions/pg_json.rb, line 109
109:       def self.extended(db)
110:         db.reset_conversion_procs if db.respond_to?(:reset_conversion_procs)
111:       end
parse_json(s) click to toggle source

Parse the given string as json, returning either a JSONArray or JSONHash instance, and raising an error if the JSON parsing does not yield an array or hash.

     # File lib/sequel/extensions/pg_json.rb, line 89
 89:       def self.parse_json(s)
 90:         begin
 91:           value = JSON.parse(s)
 92:         rescue JSON::ParserError=>e
 93:           raise Sequel.convert_exception_class(e, Sequel::InvalidValue)
 94:         end
 95: 
 96:         case value
 97:         when Array
 98:           JSONArray.new(value)
 99:         when Hash 
100:           JSONHash.new(value)
101:         else
102:           raise Sequel::InvalidValue, "unhandled json value: #{value.inspect} (from #{s.inspect})"
103:         end
104:       end

Public Instance Methods

bound_variable_arg(arg, conn) click to toggle source

Handle JSONArray and JSONHash in bound variables

     # File lib/sequel/extensions/pg_json.rb, line 114
114:       def bound_variable_arg(arg, conn)
115:         case arg
116:         when JSONArray, JSONHash
117:           arg.to_json
118:         else
119:           super
120:         end
121:       end
schema_column_type(db_type) click to toggle source

Make the column type detection recognize the json type.

     # File lib/sequel/extensions/pg_json.rb, line 124
124:       def schema_column_type(db_type)
125:         case db_type
126:         when 'json'
127:           :json
128:         else
129:           super
130:         end
131:       end

Private Instance Methods

typecast_value_json(value) click to toggle source

Given a value to typecast to the json column

     # File lib/sequel/extensions/pg_json.rb, line 141
141:       def typecast_value_json(value)
142:         case value
143:         when JSONArray, JSONHash
144:           value
145:         when Array
146:           JSONArray.new(value)
147:         when Hash 
148:           JSONHash.new(value)
149:         when String
150:           JSONDatabaseMethods.parse_json(value)
151:         else
152:           raise Sequel::InvalidValue, "invalid value for json: #{value.inspect}"
153:         end
154:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.