| Module | Sequel::Plugins::JsonSerializer::ClassMethods |
| In: |
lib/sequel/plugins/json_serializer.rb
|
| json_serializer_opts | [R] | The default opts to use when serializing model objects to JSON. |
Copy the current model object‘s default json options into the subclass.
# File lib/sequel/plugins/json_serializer.rb, line 126
126: def inherited(subclass)
127: super
128: opts = {}
129: json_serializer_opts.each{|k, v| opts[k] = (v.is_a?(Array) || v.is_a?(Hash)) ? v.dup : v}
130: subclass.instance_variable_set(:@json_serializer_opts, opts)
131: end
Create a new model object from the hash provided by parsing JSON. Handles column values (stored in values), associations (stored in associations), and other values (by calling a setter method). If an entry in the hash is not a column or an association, and no setter method exists, raises an Error.
# File lib/sequel/plugins/json_serializer.rb, line 100
100: def json_create(hash)
101: obj = new
102: cols = columns.map{|x| x.to_s}
103: assocs = associations.map{|x| x.to_s}
104: meths = obj.send(:setter_methods, nil, nil)
105: hash.delete(JSON.create_id)
106: hash.each do |k, v|
107: if assocs.include?(k)
108: obj.associations[k.to_sym] = v
109: elsif meths.include?("#{k}=")
110: obj.send("#{k}=", v)
111: elsif cols.include?(k)
112: obj.values[k.to_sym] = v
113: else
114: raise Error, "Entry in JSON hash not an association or column and no setter method exists: #{k}"
115: end
116: end
117: obj
118: end