Socket
Book a DemoInstallSign in
Socket

hashmake

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hashmake

0.2.1
bundlerRubygems
Version published
Maintainers
1
Created
Source

= hashmake

  • {Homepage}[https://rubygems.org/gems/hashmake]
  • {Documentation}[http://rubydoc.info/gems/hashmake/frames]
  • {Email}[mailto:jamestunnell@lavabit.com]

== Description

Make hash-based object initialization easy!

The hash_make method checks hashed arguments against a set of specifications, then values are assigned to instance variables matching the arg keys.

== Features Type checking via Object#is_a?. The default type is Object, allowing anything. Can allow multiple types. Mark as required/not. Set to true, by default. Default value, for non-required args that aren't given. For default values that are mutable, make the default value a Proc that can be called to generate the mutable value. Validation of values via a Proc.

== Examples

require 'hashmake'

class MyClass include Hashmake::HashMakeable

ARG_SPECS = {
  :x => arg_spec(:reqd => true, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }),
  :y => arg_spec(:reqd => false, :type => Float, :validator => ->(a){ a.between?(0.0,1.0) }, :default => 0.0),
  :z => arg_spec_array(:reqd => false, :type => String, :validator => ->(a){ a.size > 0 })
}

attr_reader :x, :y, :z

def initialize hashed_args
  hash_make(ARG_SPECS, hashed_args)
end

end

a = MyClass.new :x => 0.5 # a.x => 0.5, a.y => 0.0 a = MyClass.new :x => 0.5, :y => 0.2 # a.x => 0.5, a.y => 0.2 a = MyClass.new # raise ArgumentError because :x is reqd and not given a = MyClass.new :y => 0.5 # raise ArgumentError because :x is reqd and not given

a = MyClass.new :x => 0.5, :z => ["abc", "efg"] # OK a = MyClass.new :x => 0.5, :z => ["abc", ""] # raise ArgumentError, because validator requires str.size > 0

class MultitypeDemo include Hashmake::HashMakeable

ARG_SPECS = {
  :str_or_class => arg_spec(:reqd => true, :type => [String, Class])
}

end

MultitypeDemo.new(:str_or_class => "") # OK MultitypeDemo.new(:str_or_class => String) # OK MultitypeDemo.new(:str_or_class => 1) # raise ArgumentError

== Requirements

== Install

$ gem install hashmake

== Copyright

Copyright (c) 2013 James Tunnell

See LICENSE.txt for details.

FAQs

Package last updated on 08 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.