Class: Attribeauty::Params
- Inherits:
-
Object
- Object
- Attribeauty::Params
- Extended by:
- Forwardable
- Defined in:
- lib/attribeauty/params.rb
Instance Attribute Summary collapse
-
#acceptables ⇒ Object
readonly
Returns the value of attribute acceptables.
-
#default_args ⇒ Object
readonly
Returns the value of attribute default_args.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#request_params ⇒ Object
readonly
Returns the value of attribute request_params.
-
#strict ⇒ Object
readonly
Returns the value of attribute strict.
-
#to_h ⇒ Object
readonly
Returns the value of attribute to_h.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #accept(**args) ⇒ Object
- #accept!(**args) ⇒ Object
- #attribute(name, type = nil, **args, &block) ⇒ Object
-
#generate_for(model, *columns) ⇒ Object
in Rails if you have a user model you can call Attribeauty::Params.with(params.to_unsafe_h).generate_for(User, :username, :name, :age, :email) It will grab the type, and add an exclude_if: for all with Null false Note, there are very few circumstances where you wouldn’t want to just assign_attributes to the model, and use the types from there, but here you go.
- #generate_for!(model, *columns) ⇒ Object
-
#initialize(request_params) ⇒ Params
constructor
A new instance of Params.
- #inspect ⇒ Object
- #root(name) ⇒ Object
- #strict? ⇒ Boolean
- #to_hash ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(request_params) ⇒ Params
Returns a new instance of Params.
15 16 17 18 19 |
# File 'lib/attribeauty/params.rb', line 15 def initialize(request_params) @request_params = (request_params || {}).transform_keys(&:to_sym) @to_h = {} @errors = [] end |
Instance Attribute Details
#acceptables ⇒ Object (readonly)
Returns the value of attribute acceptables.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def acceptables @acceptables end |
#default_args ⇒ Object (readonly)
Returns the value of attribute default_args.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def default_args @default_args end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def errors @errors end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def prefix @prefix end |
#request_params ⇒ Object (readonly)
Returns the value of attribute request_params.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def request_params @request_params end |
#strict ⇒ Object (readonly)
Returns the value of attribute strict.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def strict @strict end |
#to_h ⇒ Object (readonly)
Returns the value of attribute to_h.
13 14 15 |
# File 'lib/attribeauty/params.rb', line 13 def to_h @to_h end |
Class Method Details
.with(request_params) ⇒ Object
9 10 11 |
# File 'lib/attribeauty/params.rb', line 9 def self.with(request_params) new(request_params) end |
Instance Method Details
#[](key) ⇒ Object
38 39 40 |
# File 'lib/attribeauty/params.rb', line 38 def [](key) to_h[key] end |
#[]=(key, value) ⇒ Object
42 43 44 |
# File 'lib/attribeauty/params.rb', line 42 def []=(key, value) to_h[key] = value end |
#accept(**args) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/attribeauty/params.rb', line 21 def accept(**args, &) @default_args = args instance_eval(&) raise MissingAttributeError, errors.join(", ") if errors.any? && strict? self end |
#accept!(**args) ⇒ Object
30 31 32 33 34 |
# File 'lib/attribeauty/params.rb', line 30 def accept!(**args, &) @strict = true accept(**args, &) end |
#attribute(name, type = nil, **args, &block) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/attribeauty/params.rb', line 52 def attribute(name, type = nil, **args, &block) value = request_params[name] return hash_from_nested(name, value, &block) if block_given? value_from_validator(name, value, type, **args) end |
#generate_for(model, *columns) ⇒ Object
in Rails if you have a user model you can call Attribeauty::Params.with(params.to_unsafe_h).generate_for(User, :username, :name, :age, :email) It will grab the type, and add an exclude_if: for all with Null false Note, there are very few circumstances where you wouldn’t want to just assign_attributes to the model, and use the types from there, but here you go. This api will never be tested or documented.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/attribeauty/params.rb', line 77 def generate_for(model, *columns) raise "Method requires Rails" unless defined?(Rails) root_node = model.to_s.downcase.to_sym cols = columns.map(&:to_s) root root_node do model.columns_hash.slice(*cols).each_value do |table| attrs = table.name.to_sym, table.type attrs << { exclude_if: :nil? } if table.null == false attribute(*attrs) end end self end |
#generate_for!(model, *columns) ⇒ Object
93 94 95 96 97 |
# File 'lib/attribeauty/params.rb', line 93 def generate_for!(model, *columns) @strict = true generate_for(model, *columns) end |
#inspect ⇒ Object
59 60 61 |
# File 'lib/attribeauty/params.rb', line 59 def inspect to_h.inspect end |
#root(name) ⇒ Object
46 47 48 49 50 |
# File 'lib/attribeauty/params.rb', line 46 def root(name) @request_params = request_params[name].transform_keys(&:to_sym) yield end |
#strict? ⇒ Boolean
67 68 69 |
# File 'lib/attribeauty/params.rb', line 67 def strict? strict end |
#to_hash ⇒ Object
36 |
# File 'lib/attribeauty/params.rb', line 36 def to_hash = to_h |
#valid? ⇒ Boolean
63 64 65 |
# File 'lib/attribeauty/params.rb', line 63 def valid? errors.empty? end |