Sha256: ed77de2cace0226284558ce665a56b32fb92389293761ab0b83cc1a06f8b389d
Contents?: true
Size: 980 Bytes
Versions: 1
Compression:
Stored size: 980 Bytes
Contents
# frozen_string_literal: true module EasyParams module Types # base interface for simple types class Generic def array? @title == :array end def initialize(title, default = nil, normalize_proc = nil, &coerce_proc) @title = title @default = default @coerce_proc = coerce_proc @normalize_proc = normalize_proc end def default(value) self.class.new(@title, value, @normalize_proc, &@coerce_proc) end def optional self.class.new(@title, @default, @normalize_proc, &@coerce_proc) end def optional? @optional end def normalize(&block) self.class.new(@title, @default, block, &@coerce_proc) end def coerce(value) value = @normalize_proc.call(value) if @normalize_proc return @default if value.nil? @coerce_proc.call(value) rescue StandardError @default end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
easy_params-0.5.0 | lib/easy_params/types/generic.rb |