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

Returns a new instance of HashParams.



9
10
11
12
13
# File 'lib/friendly_routes/params/hash_params.rb', line 9

def initialize(name, hash, optional: true)
  super(:collection, name, optional)
  @hash = hash
  check_params
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 (Integer)

    hash value

Returns:

  • (Boolean)


36
37
38
# File 'lib/friendly_routes/params/hash_params.rb', line 36

def allowed?(value)
  @hash.values.include?(value)
end

#compose(value) ⇒ String, Symbol

Generate request value from params

Inverse of #parse

Parameters:

  • value (Object)

    hash value

Returns:

  • (String, Symbol)

    hash key



29
30
31
# File 'lib/friendly_routes/params/hash_params.rb', line 29

def compose(value)
  @hash.key(value)
end

#constraintsObject



15
16
17
# File 'lib/friendly_routes/params/hash_params.rb', line 15

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



22
23
24
# File 'lib/friendly_routes/params/hash_params.rb', line 22

def parse(value)
  @hash[value]
end