module HornOfPlenty::Parsers::Hash

Private Instance Methods

parse_boolean(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 42
def parse_boolean(hash, key, options = {}, &user_block)
  converter = lambda do |value|
    if value
      %w{true TRUE True t T yes YES Yes y Y 1}.include?(value) ? true : false
    end
  end

  parse_key(hash, key, converter, options, &user_block)
end
parse_existance(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 38
def parse_existance(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { value }, options, &user_block)
end
parse_float(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 22
def parse_float(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { value.to_f }, options, &user_block)
end
parse_html(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 12
def parse_html(hash, key, options = {}, &user_block)
  converter = ->(value) { CGI.unescape_html(value.to_s) }

  parse_key(hash, key, converter, options, &user_block)
end
parse_integer(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 18
def parse_integer(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { value.to_i }, options, &user_block)
end
parse_key(hash, key, converter = nil, options = {}) { |value| ... } click to toggle source

rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity

# File lib/horn_of_plenty/parsers/hash.rb, line 57
def parse_key(hash, key, converter = nil, options = {})
  default_value = if options[:default_value]
                    options[:default_value]
                  elsif options[:array]
                    []
                  end

  value = hash.dig(*key.split('/:/'))
  value = if value.to_s.empty?
            default_value
          else
            value
          end
  value = if options[:array] && value.respond_to?(:split)
            value.split(',')
          else
            value
          end
  value = if value.respond_to?(:map) && converter
            value.map do |item|
              converter.call(item)
            end
          elsif value && converter
            converter.call(value)
          else
            value
          end

  if block_given? && (value || options[:yield_if_nil])
    yield value
  else
    value
  end
end
parse_percentage(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 52
def parse_percentage(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { (value.to_f / 100).round(4) }, options, &user_block)
end
parse_phone(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 34
def parse_phone(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { value.gsub(/[^x\d]/, '') }, options, &user_block)
end
parse_text(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 8
def parse_text(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { value.to_s }, options, &user_block)
end
parse_time(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 26
def parse_time(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { Time.iso8601(value).round }, options, &user_block)
end
parse_url(hash, key, options = {}, &user_block) click to toggle source
# File lib/horn_of_plenty/parsers/hash.rb, line 30
def parse_url(hash, key, options = {}, &user_block)
  parse_key(hash, key, ->(value) { URI.parse(value) }, options, &user_block)
end