Sha256: 00ed76e9f154b7877dab626cb417619b467b85ac92e9df495d1127b679cc63f5

Contents?: true

Size: 794 Bytes

Versions: 1

Compression:

Stored size: 794 Bytes

Contents

require 'hanami/utils/hash'

module Hanami
  module Helpers
    module FormHelper
      class Values
        GET_SEPARATOR = '.'.freeze

        def initialize(values, params)
          @values = Utils::Hash.new(values).stringify!
          @params = params
        end

        def get(key)
          @params.get(key) || _get_from_values(key)
        end

        private
        def _get_from_values(key)
          initial_key, *keys = key.to_s.split(GET_SEPARATOR)
          result             = @values[initial_key]

          Array(keys).each do |k|
            break if result.nil?

            result = if result.respond_to?(k)
              result.public_send(k)
            else
              nil
            end
          end

          result
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-helpers-0.3.0 lib/hanami/helpers/form_helper/values.rb