class Hanami::Helpers::FormHelper::Values

Values from params and form helpers.

It’s responsible to populate input values with data coming from params and inline values specified via form helpers like ‘text_field`.

@since 2.1.0 @api private

Constants

GET_SEPARATOR

@since 2.1.0 @api private

Attributes

csrf_token[R]

@api private @since 2.1.0

Public Class Methods

new(values: {}, params: {}, csrf_token: nil) click to toggle source

@since 2.1.0 @api private

# File lib/hanami/helpers/form_helper/values.rb, line 24
def initialize(values: {}, params: {}, csrf_token: nil)
  @values = values.to_h
  @params = params.to_h
  @csrf_token = csrf_token
end

Public Instance Methods

get(*keys) click to toggle source

Returns the value (if present) for the given key. Nested values are expressed with an array if symbols.

@since 2.1.0 @api private

# File lib/hanami/helpers/form_helper/values.rb, line 35
def get(*keys)
  get_from_params(*keys) || get_from_values(*keys)
end

Private Instance Methods

dig(base, key) click to toggle source

@since 2.1.0 @api private

# File lib/hanami/helpers/form_helper/values.rb, line 65
def dig(base, key)
  case base
  when ::Hash then base[key]
  when Array then base[key.to_s.to_i]
  when ->(r) { r.respond_to?(key) } then base.public_send(key)
  end
end
get_from_params(*keys) click to toggle source

@since 2.1.0 @api private

# File lib/hanami/helpers/form_helper/values.rb, line 43
def get_from_params(*keys)
  keys.map! { |key| /\A\d+\z/.match?(key.to_s) ? key.to_s.to_i : key }
  @params.dig(*keys)
end
get_from_values(*keys) click to toggle source

@since 2.1.0 @api private

# File lib/hanami/helpers/form_helper/values.rb, line 50
def get_from_values(*keys)
  head, *tail = *keys
  result = @values[head]

  tail.each do |k|
    break if result.nil?

    result = dig(result, k)
  end

  result
end