Sha256: 4f96c2742e4da35e00226c53aae415b93982c9fd1bf09e879f4d078040ad2de5

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Hashmake
module HashMakeable
  
  # Use the included hook to also extend the including class with HashMake
  # class methods
  def self.included(base)
    base.extend(ClassMethods)
  end

  def hash_make arg_specs, hashed_args, assign_args = true
    arg_specs.each do |arg_spec|
      raise ArgumentError, "arg_specs item #{arg_spec} is not a HashedArg" unless arg_spec.is_a?(HashedArg)
    end
    raise ArgumentError, "hashed_args is not a Hash" unless hashed_args.is_a?(Hash)

    arg_specs.each do |arg_spec|
      key = arg_spec.key
      if hashed_args.has_key?(key)
        val = hashed_args[key]
      else
        if arg_spec.reqd
          raise ArgumentError, "hashed_args does not have required key #{key}"
        else
          if arg_spec.default.is_a?(Proc) && arg_spec.type != Proc
            val = arg_spec.default.call
          else
            val = arg_spec.default
          end
        end
      end
      
      if arg_spec.array
        raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
        val.each do |item|
          raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
        end
      else
        raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
      end
      
      if assign_args
        raise ArgumentError, "value #{val} is not valid" unless arg_spec.validator.call(val)
        self.instance_variable_set("@#{key.to_s}".to_sym, val)
      end
    end
  end

  module ClassMethods
    # none right now, maybe later
  end

end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hashmake-0.1.0 lib/hashmake/hash_makeable.rb