Class: SchemaTools::Cleaner
- Inherits:
-
Object
- Object
- SchemaTools::Cleaner
- Defined in:
- lib/schema_tools/cleaner.rb
Class Method Summary collapse
-
.clean_params!(obj_name, params, opts = {}) ⇒ Object
Clean a hash before a new object is created from it.
Class Method Details
.clean_params!(obj_name, params, opts = {}) ⇒ Object
Clean a hash before a new object is created from it. Can be used in your ruby controllers where new objects are created from a params-hash Directly CHANGES incoming params-hash!
even if defined as readonly
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/schema_tools/cleaner.rb', line 17 def clean_params!(obj_name, params, opts={}) schema = SchemaTools::Reader.read(obj_name) setters = [] # gather allowed properties schema[:properties].each{ |k,v| setters << k if !v['readonly'] } setters += opts[:keep] if opts[:keep] && opts[:keep].is_a?(Array) # kick readonly params.delete_if { |k,v| !setters.include?("#{k}") } #convert to type in schema, # atm. only strings since number can vary float, int, BigDec params.each do |k,v| if schema[:properties]["#{k}"]['type'] == 'string' && !v.is_a?(String) params[k] = "#{v}" end end end |