Class: FriendlyRoutes::Params::HashParams
- Defined in:
- lib/friendly_routes/params/hash_params.rb
Instance Attribute Summary collapse
-
#hash ⇒ Hash
keys are values for matching and values is needed values.
Attributes inherited from Base
Instance Method Summary collapse
-
#allowed?(value) ⇒ Boolean
Check if value can be composed.
-
#compose(value) ⇒ String, Symbol
Generate request value from params.
- #constraints ⇒ Object
-
#initialize(name, hash, optional: true) ⇒ HashParams
constructor
A new instance of HashParams.
-
#parse(value) ⇒ Object
Parse values from request.
Methods inherited from Base
#optional?, #refused?, #required?
Constructor Details
#initialize(name, hash, optional: true) ⇒ HashParams
Returns a new instance of HashParams.
9 10 11 12 13 14 15 16 |
# File 'lib/friendly_routes/params/hash_params.rb', line 9 def initialize(name, hash, optional: true) super(:collection, name, optional) @hash = hash check_params @hash = @hash.map do |k, v| [k, v.try(:to_s) || v] end.to_h end |
Instance Attribute Details
#hash ⇒ Hash
keys are values for matching and values is needed values
6 7 8 |
# File 'lib/friendly_routes/params/hash_params.rb', line 6 def hash @hash end |
Instance Method Details
#allowed?(value) ⇒ Boolean
Check if value can be composed
39 40 41 |
# File 'lib/friendly_routes/params/hash_params.rb', line 39 def allowed?(value) @hash.values.include?(value) || @hash.values.include?(value.try(:to_s)) end |
#compose(value) ⇒ String, Symbol
Generate request value from params
Inverse of #parse
32 33 34 |
# File 'lib/friendly_routes/params/hash_params.rb', line 32 def compose(value) @hash.key(value) || @hash.key(value.try(:to_s)) end |
#constraints ⇒ Object
18 19 20 |
# File 'lib/friendly_routes/params/hash_params.rb', line 18 def constraints Regexp.new @hash.keys.join('|') end |
#parse(value) ⇒ Object
Parse values from request
Inverse of #compose
25 26 27 |
# File 'lib/friendly_routes/params/hash_params.rb', line 25 def parse(value) @hash[value] end |