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?, #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

#hashHash

keys are values for matching and values is needed values

Returns:

  • (Hash)

    the current value of hash



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

Parameters:

  • value (Object)

    hash value

Returns:

  • (Boolean)


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

Parameters:

  • value (Object)

    hash value

Returns:

  • (String, Symbol)

    hash key



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

Parameters:

  • value (String, Symbol)

    hash key

Returns:

  • (Object)

    hash value



25
26
27
# File 'lib/friendly_routes/params/hash_params.rb', line 25

def parse(value)
  @hash[value]
end