Class: Hashmake::ArgSpec
- Inherits:
-
Object
- Object
- Hashmake::ArgSpec
- 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.
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
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#reqd ⇒ Object
readonly
Returns the value of attribute reqd.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
Instance Method Summary collapse
-
#hash_make_if_needed(val) ⇒ Object
If the val is not of the right type, but is a Hash, attempt to make an object of the right type if it is hash-makeable.
-
#initialize(hashed_args) ⇒ ArgSpec
constructor
A new instance of ArgSpec.
- #make_hash_if_possible(val) ⇒ Object
-
#validate_value(val) ⇒ Object
Check the given value, and raise ArgumentError it is not valid.
Constructor Details
#initialize(hashed_args) ⇒ ArgSpec
A new instance of ArgSpec.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/hashmake/arg_spec.rb', line 41 def initialize hashed_args new_args = DEFAULT_ARGS.merge(hashed_args) @type = new_args[:type] raise ArgumentError, "#{@type} is not a Class" unless @type.is_a?(Class) @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
#default ⇒ Object (readonly)
Returns the value of attribute default.
23 24 25 |
# File 'lib/hashmake/arg_spec.rb', line 23 def default @default end |
#reqd ⇒ Object (readonly)
Returns the value of attribute reqd.
23 24 25 |
# File 'lib/hashmake/arg_spec.rb', line 23 def reqd @reqd end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/hashmake/arg_spec.rb', line 23 def type @type end |
#validator ⇒ Object (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
#hash_make_if_needed(val) ⇒ Object
If the val is not of the right type, but is a Hash, attempt to make an object of the right type if it is hash-makeable
59 60 61 62 63 64 |
# File 'lib/hashmake/arg_spec.rb', line 59 def hash_make_if_needed val if Hashmake.hash_makeable?(@type) and val.is_a?(Hash) val = @type.new val end return val end |
#make_hash_if_possible(val) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/hashmake/arg_spec.rb', line 72 def make_hash_if_possible val if Hashmake::hash_makeable?(val.class) and val.class.is_a?(@type) val = val.make_hash end return val end |
#validate_value(val) ⇒ Object
Check the given value, and raise ArgumentError it is not valid.
67 68 69 70 |
# File 'lib/hashmake/arg_spec.rb', line 67 def validate_value val raise ArgumentError, "val #{val} is not a #{@type}" unless val.is_a?(@type) raise ArgumentError, "val #{val} is not valid" unless @validator.call(val) end |