GithubHelp home page GithubHelp logo

jamestunnell / hashmake Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 184 KB

Like making your ruby objects with hashed args? This lib aims to make the process easier and more powerful

License: MIT License

Ruby 100.00%

hashmake's Introduction

hashmake

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 © 2013 James Tunnell

See LICENSE.txt for details.

hashmake's People

Contributors

jamestunnell avatar

Watchers

James Cloos avatar  avatar  avatar

hashmake's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.