Class: Pancake::Mixins::Publish::ActionOptions
- Defined in:
- lib/pancake/mixins/publish/action_options.rb
Constant Summary collapse
- CONFIG_OPTIONS =
[:provides, :only_provides]
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#formats ⇒ Object
readonly
Returns the value of attribute formats.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
-
#initialize(default_formats, opts) ⇒ ActionOptions
constructor
private
Generates a new instance of the class.
-
#validate_and_coerce(incoming_params) ⇒ Object
private
Accepts a hash of parameters and replaces each entry with it’s coerced or default value.
Constructor Details
#initialize(default_formats, opts) ⇒ ActionOptions
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Generates a new instance of the class. This instance encapsulates the options and logic needed to validate the parameters being input into a controller action. First argument is a list of symbols for the formats it supports. The second is hash of options. Typically this hash is generated by the publish declaration in a controller.
TODO: Allow params to be grouped together
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pancake/mixins/publish/action_options.rb', line 17 def initialize(default_formats, opts) # Extract the params — excluding configuration — and turn the keys into # strings. Additionally, check to see if someone has passed in a a raw # value rather than using one of our as_* methods. @params = opts.inject({}) do |memo, opt| unless CONFIG_OPTIONS.include? opt[0] unless opt[1].is_a?(Array) raise "Parameter values must be specified with an as_* method. You passed in a #{opt[1].class.to_s}" end memo[opt[0].to_s] = opt[1] end memo end @formats = (default_formats, opts) end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
5 6 7 |
# File 'lib/pancake/mixins/publish/action_options.rb', line 5 def default @default end |
#formats ⇒ Object (readonly)
Returns the value of attribute formats.
5 6 7 |
# File 'lib/pancake/mixins/publish/action_options.rb', line 5 def formats @formats end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
5 6 7 |
# File 'lib/pancake/mixins/publish/action_options.rb', line 5 def params @params end |
Instance Method Details
#validate_and_coerce(incoming_params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Accepts a hash of parameters and replaces each entry with it’s coerced or default value. If an expected param is missing, it will raise an error.
TODO: Flag missing params rather than just raising an error TODO: Allow defaults to be dynamically generated, i.e. method call
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/pancake/mixins/publish/action_options.rb', line 41 def validate_and_coerce(incoming_params) missing = [] params.each do |name, config| type, default, opts = config value = incoming_params[name] if !value.nil? && value != "" incoming_params[name] = send("validate_and_coerce_#{type}", value, opts) else if default == :req missing << [name, type] elsif default != :opt incoming_params[name] = default end end end [incoming_params, missing] end |