Sha256: e06e6e3503fb71e89a91b5fc7e04c2f63f5dedcf311496cfc7525f3f8602bacb

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

module SoapyCake
  module Helper
    def walk_tree(obj, key = nil, &block)
      return nil if obj == {}

      case obj
      when Hash
        obj.map { |hk, hv| [hk, walk_tree(hv, hk, &block)] }.to_h
      when Array
        obj.map { |av| walk_tree(av, &block) }
      else
        yield(obj, key)
      end
    end

    def validate_id(opts, key)
      fail Error, "Parameter '#{key}' must be > 0!" if opts[key].to_i < 1
    end

    def require_params(opts, params)
      params.each do |param|
        fail Error, "Parameter '#{param}' missing!" unless opts.key?(param)
      end
    end

    def translate_booleans!(opts)
      opts.each do |k, v|
        opts[k] = 'on' if v == true
        opts[k] = 'off' if v == false
      end
    end

    def translate_values!(opts, params)
      params.each do |type|
        opts[type] = const_lookup(type, opts[type]) if opts.key?(type)
      end
    end

    def const_lookup(type, key)
      Const::CONSTS[type].fetch(key) do
        fail ArgumentError, "#{key} is not a valid value for #{type}"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
soapy_cake-1.8.1 lib/soapy_cake/helper.rb
soapy_cake-1.8.0 lib/soapy_cake/helper.rb
soapy_cake-1.7.2 lib/soapy_cake/helper.rb
soapy_cake-1.7.1 lib/soapy_cake/helper.rb
soapy_cake-1.7.0 lib/soapy_cake/helper.rb
soapy_cake-1.6.9 lib/soapy_cake/helper.rb