Sha256: 0b44ad74eddb89209663e3a578bb99e3a943c45282c4703708566ecd8e7e6cea
Contents?: true
Size: 1.58 KB
Versions: 7
Compression:
Stored size: 1.58 KB
Contents
module Decanter module ValueParser module Core def self.included(base) base.extend(ClassMethods) end module ClassMethods def parse(name, values, options={}) value_ary = values.is_a?(Array) ? values : [values] # want to treat 'false' as an actual value if value_ary.all? { |value| value.nil? || value == "" } if options[:required] raise ArgumentError.new("No value for required argument: #{name}") else return { name => nil } end end if @allowed && value_ary.all? { |value| @allowed.include?(value.class) } return { name => values } end unless @parser raise ArgumentError.new("No parser for argument: #{name} with types: #{value_ary.map(&:class).join(', ')}") end case @result when :raw # Parser result must be a hash parsed = @parser.call(name, values, options) parsed.is_a?(Hash) ? parsed : raise(ArgumentError.new("Result of parser #{self.name} with values #{values} was #{parsed} when it must be a hash.")) else # Parser result will be treated as a single value belonging to the name { name => @parser.call(name, values, options) } end end def parser(&block) @parser = block end def allow(*args) @allowed = args end def result(opt) @result = opt end end end end end
Version data entries
7 entries across 7 versions & 1 rubygems