Class: FriendlyRoutes::Params::HashParams

Inherits:
Base
  • Object
show all
Defined in:
lib/friendly_routes/params/hash_params.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name, #type

Instance Method Summary collapse

Methods inherited from Base

#optional?, #required?

Constructor Details

#initialize(name, hash, optional: true) ⇒ 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

#hashHash

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

#constraintsObject



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