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

CONTAINERS =

The valid container types. If nil, indicates no container is expected, just a plain object of type given by :type.

[ nil, Hash, Array ]
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. The :container key is set to CONTAINER_NONE.

{
  :reqd => true,
  :validator => ->(a){true},
  :container => nil,
  :type => Object,
}

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, :container, 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.
    

    :container => indicates whether the arg key will be paired

    with a container (array or hash) which contains
    objects of the type specified by :type. Valid values
    for this are given by ArgSpec::CONTAINERS.
    

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hashmake/arg_spec.rb', line 47

def initialize hashed_args
  new_args = DEFAULT_ARGS.merge(hashed_args)
  
  @type = new_args[:type]
  raise ArgumentError, "args[:type] #{@type} is not a Class" unless @type.is_a?(Class)
  
  @validator = new_args[:validator]
  @reqd = new_args[:reqd]
  
  @container = new_args[:container]
  raise ArgumentError, "CONTAINERS does not include container #{@container}" unless CONTAINERS.include?(@container)
  
  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

#containerObject (readonly)

Returns the value of attribute container.



25
26
27
# File 'lib/hashmake/arg_spec.rb', line 25

def container
  @container
end

#defaultObject (readonly)

Returns the value of attribute default.



25
26
27
# File 'lib/hashmake/arg_spec.rb', line 25

def default
  @default
end

#reqdObject (readonly)

Returns the value of attribute reqd.



25
26
27
# File 'lib/hashmake/arg_spec.rb', line 25

def reqd
  @reqd
end

#typeObject (readonly)

Returns the value of attribute type.



25
26
27
# File 'lib/hashmake/arg_spec.rb', line 25

def type
  @type
end

#validatorObject (readonly)

Returns the value of attribute validator.



25
26
27
# File 'lib/hashmake/arg_spec.rb', line 25

def validator
  @validator
end