Class: Hashmake::ArgSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/hashmake/arg_spec.rb

Overview

Provides a specification of how a hashed arg is to be processed by the hash_make method.

Author:

  • James Tunnell

Constant Summary collapse

DEFAULT_ARGS =

Defines default key/value pairs to use in initializing an instance. The :reqd key is set to true by default. The :validator key is set to a Proc that always returns true.

{
  :reqd => true,
  :validator => ->(a){true},
  :type => Object,
  :allow_nil => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hashed_args) ⇒ ArgSpec

A new instance of ArgSpec.

Parameters:

  • hashed_args (Hash)

    Hash to use in initializing an instance. Optional keys are :type, :reqd, :validator, and :default. :type => the type of object expected to be paired

    with the key.
    

    :reqd => If true, the arg key must be in the hash

    passed to #initialize. If false, then a
    default must be specified with :default
    key. Set to true by default.
    

    :default => If reqd is false, this must be specified.

    This can be any object reference. If it
    is a Proc, then the Proc will be called,
    expecting it to produce the default.
    

    :validator => a Proc used to check the validity of

    whatever value is paired with an arg key.
    


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hashmake/arg_spec.rb', line 41

def initialize hashed_args
  new_args = DEFAULT_ARGS.merge(hashed_args)
  
  @type = new_args[:type]
  if @type.is_a?(Enumerable)
    @type.each do |type|
      raise ArgumentError, "#{type} is not a Class" unless type.is_a?(Class)
    end
  else
    raise ArgumentError, "#{@type} is not a Class" unless @type.is_a?(Class)
  end
  
  @validator = new_args[:validator]
  @reqd = new_args[:reqd]
  
  unless @reqd
    msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
    raise ArgumentError, msg unless new_args.has_key?(:default)
    @default = new_args[:default]
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



23
24
25
# File 'lib/hashmake/arg_spec.rb', line 23

def default
  @default
end

#reqdObject (readonly)

Returns the value of attribute reqd.



23
24
25
# File 'lib/hashmake/arg_spec.rb', line 23

def reqd
  @reqd
end

#typeObject (readonly)

Returns the value of attribute type.



23
24
25
# File 'lib/hashmake/arg_spec.rb', line 23

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator.



23
24
25
# File 'lib/hashmake/arg_spec.rb', line 23

def validator
  @validator
end

Instance Method Details

#validate_value(val) ⇒ Object

Check the given value, and raise ArgumentError it is not valid.

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/hashmake/arg_spec.rb', line 64

def validate_value val
  if @type.is_a?(Array)
    type_matched = false
    @type.each do |type|
      if val.is_a? type
        type_matched = true
      end
    end
    unless type_matched
      raise ArgumentError, "val #{val} is not one of #{@type}"
    end
  else
    raise ArgumentError, "val #{val} is not a #{@type}" unless val.is_a?(@type)
  end

  raise ArgumentError, "val #{val} is not valid" unless @validator.call(val)
end