class SimpleParams::Params
Attributes
options[RW]
original_hash[RW]
original_params[RW]
raw_params[RW]
Public Class Methods
add_validations(name, opts = {})
click to toggle source
# File lib/simple_params/params.rb, line 32 def add_validations(name, opts = {}) conditional_validation = opts.fetch(:validations, {}).fetch(:if, nil) if conditional_validation opts[:optional] = false end validations = ValidationBuilder.new(opts).build validates name, validations unless validations.empty? end
api_pie_documentation()
click to toggle source
# File lib/simple_params/params.rb, line 23 def api_pie_documentation SimpleParams::ApiPieDoc.new(self).build end
model_name()
click to toggle source
# File lib/simple_params/params.rb, line 19 def model_name ActiveModel::Name.new(self) end
nested_array(name, opts={}, &block)
click to toggle source
# File lib/simple_params/params.rb, line 63 def nested_array(name, opts={}, &block) klass = NestedParams.define_new_array_class(self, name, opts, &block) add_nested_class(name, klass, opts) end
nested_arrays()
click to toggle source
# File lib/simple_params/params.rb, line 52 def nested_arrays nested_classes.select { |key, klass| klass.array? } end
nested_classes()
click to toggle source
# File lib/simple_params/params.rb, line 44 def nested_classes @nested_classes ||= {} end
nested_hash(name, opts={}, &block)
click to toggle source
# File lib/simple_params/params.rb, line 56 def nested_hash(name, opts={}, &block) klass = NestedParams.define_new_hash_class(self, name, opts, &block) add_nested_class(name, klass, opts) end
Also aliased as: nested_param, nested
nested_hashes()
click to toggle source
# File lib/simple_params/params.rb, line 48 def nested_hashes nested_classes.select { |key, klass| klass.hash? } end
new(params={})
click to toggle source
# File lib/simple_params/params.rb, line 111 def initialize(params={}) set_strictness params = InitializationHash.new(params) @original_params = params.original_params define_attributes(@original_params) set_accessors(params) end
param(name, opts={})
click to toggle source
# File lib/simple_params/params.rb, line 27 def param(name, opts={}) define_attribute(name, opts) add_validations(name, opts) end
Private Class Methods
add_nested_class(name, klass, opts)
click to toggle source
# File lib/simple_params/params.rb, line 69 def add_nested_class(name, klass, opts) @nested_classes ||= {} @nested_classes[name.to_sym] = klass define_nested_accessor(name, klass, opts) if using_rails_helpers? define_rails_helpers(name, klass) end end
define_nested_accessor(name, klass, opts)
click to toggle source
# File lib/simple_params/params.rb, line 78 def define_nested_accessor(name, klass, opts) define_method("#{name}") do if instance_variable_defined?("@#{name}") instance_variable_get("@#{name}") else # This logic basically sets the nested class to an instance of itself, unless # it is optional. init_value = if opts[:optional] klass.hash? ? nil : [] else klass_instance = klass.new({}, self) klass.hash? ? klass_instance : [klass_instance] end instance_variable_set("@#{name}", init_value) end end define_method("#{name}_params") do original_params = instance_variable_get("@original_params") original_params[:"#{name}"] || original_params[:"#{name}_attributes"] end define_method("#{name}=") do |initializer| init_value = klass.build(initializer, self, name) instance_variable_set("@#{name}", init_value) end end
Public Instance Methods
all_nested_classes()
click to toggle source
# File lib/simple_params/params.rb, line 137 def all_nested_classes nested_class_list.class_instances end
define_attributes(params)
click to toggle source
# File lib/simple_params/params.rb, line 119 def define_attributes(params) self.class.defined_attributes.each_pair do |key, opts| self.send("#{key}_attribute=", Attribute.new(self, key, opts)) end end
errors()
click to toggle source
# File lib/simple_params/params.rb, line 141 def errors @errors ||= SimpleParams::Errors.new(self, nested_class_hash) end
nested_class_attributes()
click to toggle source
# File lib/simple_params/params.rb, line 129 def nested_class_attributes nested_classes.keys end
nested_class_hash()
click to toggle source
# File lib/simple_params/params.rb, line 133 def nested_class_hash nested_class_list.to_hash end
to_hash()
click to toggle source
# File lib/simple_params/params.rb, line 125 def to_hash HashBuilder.new(self).build end
Private Instance Methods
defined_attributes()
click to toggle source
# File lib/simple_params/params.rb, line 156 def defined_attributes self.class.defined_attributes end
nested_class_list()
click to toggle source
# File lib/simple_params/params.rb, line 152 def nested_class_list @nested_class_list ||= NestedClassList.new(self) end
nested_classes()
click to toggle source
# File lib/simple_params/params.rb, line 160 def nested_classes self.class.nested_classes end
set_accessors(params={})
click to toggle source
# File lib/simple_params/params.rb, line 146 def set_accessors(params={}) params.each do |attribute_name, value| send("#{attribute_name}=", value) end end