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 131 131: def inherited(subclass) 132: super 133: opts = {} 134: json_serializer_opts.each{|k, v| opts[k] = (v.is_a?(Array) || v.is_a?(Hash)) ? v.dup : v} 135: subclass.instance_variable_set(:@json_serializer_opts, opts) 136: 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 105 105: def json_create(hash) 106: obj = new 107: cols = columns.map{|x| x.to_s} 108: assocs = associations.map{|x| x.to_s} 109: meths = obj.send(:setter_methods, nil, nil) 110: hash.delete(JSON.create_id) 111: hash.each do |k, v| 112: if assocs.include?(k) 113: obj.associations[k.to_sym] = v 114: elsif meths.include?("#{k}=") 115: obj.send("#{k}=", v) 116: elsif cols.include?(k) 117: obj.values[k.to_sym] = v 118: else 119: raise Error, "Entry in JSON hash not an association or column and no setter method exists: #{k}" 120: end 121: end 122: obj 123: end