Sha256: 958e149a1c4dd039cbdddf5aa82001af816da425304a3d146984c23703e59da9

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module Science
  
  # General helpers for the internals of this gem.
  # In time, most of these should be abstracted to gems.
  module Helpers
    
    # TODO: Abstract to `convertable` and `error` gems.
    def validate_and_convert_hash(name, object)
      raise TypeError, "`#{name}` should be a Hash or respond to :to_hash or :to_h" unless object.is_a?(Hash) || object.respond_to?(:to_hash) || object.respond_to?(:to_h)
      object = object.to_hash rescue object.to_h unless object.is_a?(Hash)
      
      object
    end
    
    # TODO: Abstract to `convertable` and `error` gems.
    def validate_and_convert_float(name, object)
      raise TypeError, "`#{name}` should be a Float or respond to :to_f" unless object.is_a?(Float) || object.respond_to?(:to_f)
      object = object.to_f unless object.is_a?(Float)
      
      object
    end
    
    # TODO: Abstract to `convertable` gem
    def validate_hash_keys(name, hash, *keys)
      keys.each do |key|
        raise "`#{name}` requires key #{key.inspect}" unless hash.has_key?(key)
      end
    end
    
    extend self
    
  end
  
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
science-0.0.1b lib/science/helpers.rb