hashmake 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ Gemfile.lock
2
+
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ coverage
8
+ InstalledFiles
9
+ lib/bundler/man
10
+ pkg
11
+ rdoc
12
+ spec/reports
13
+ test/tmp
14
+ test/version_tmp
15
+ tmp
16
+
17
+ # YARD artifacts
18
+ .yardoc
19
+ _yardoc
20
+ doc/
data/ChangeLog.rdoc ADDED
@@ -0,0 +1,4 @@
1
+ === 0.1.0 / 2013-02-01
2
+
3
+ * Initial release:
4
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 James Tunnell
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ hashmake
2
+ ========
3
+
4
+ Like making your ruby objects with hashed args? This lib aims to make the process easier and more powerful
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = hashmake
2
+
3
+ * {Homepage}[https://rubygems.org/gems/hashmake]
4
+ * {Documentation}[http://rubydoc.info/gems/hashmake/frames]
5
+ * {Email}[mailto:jamestunnell at lavabit.com]
6
+
7
+ == Description
8
+
9
+ Make hash-based object initialization easy! Provides hash_make method that can consider parameter name, type, default value, validation, and requiredd/not, according to the specification provided. Also, by default assigns by default to instance variable of same name as parameter.
10
+
11
+ == Features
12
+
13
+ == Examples
14
+
15
+ require 'hashmake'
16
+
17
+ == Requirements
18
+
19
+ == Install
20
+
21
+ $ gem install hashmake
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2013 James Tunnell
26
+
27
+ See LICENSE.txt for details.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+
5
+ begin
6
+ require 'bundler'
7
+ rescue LoadError => e
8
+ warn e.message
9
+ warn "Run `gem install bundler` to install Bundler."
10
+ exit -1
11
+ end
12
+
13
+ begin
14
+ Bundler.setup(:development)
15
+ rescue Bundler::BundlerError => e
16
+ warn e.message
17
+ warn "Run `bundle install` to install missing gems."
18
+ exit e.status_code
19
+ end
20
+
21
+ require 'rake'
22
+
23
+ require 'rspec/core/rake_task'
24
+ RSpec::Core::RakeTask.new
25
+
26
+ task :test => :spec
27
+ task :default => :spec
28
+
29
+ require "bundler/gem_tasks"
30
+
31
+ require 'yard'
32
+ YARD::Rake::YardocTask.new
33
+ task :doc => :yard
data/hashmake.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/hashmake/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "hashmake"
7
+ gem.version = Hashmake::VERSION
8
+ gem.summary = %q{Make hashed-based object initialization easy!}
9
+ gem.description = %q{Make hash-based object initialization easy! Provides hash_make method that can consider parameter name, type, default value, validation, and requiredd/not, according to the specification provided. Also, by default assigns by default to instance variable of same name as parameter.}
10
+ gem.license = "MIT"
11
+ gem.authors = ["James Tunnell"]
12
+ gem.email = "jamestunnell@lavabit.com"
13
+ gem.homepage = "https://rubygems.org/gems/hashmake"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ gem.add_development_dependency 'bundler', '~> 1.0'
21
+ gem.add_development_dependency 'rake', '~> 0.8'
22
+ gem.add_development_dependency 'rspec', '~> 2.4'
23
+ gem.add_development_dependency 'yard', '~> 0.8'
24
+ end
data/lib/hashmake.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'hashmake/version'
2
+ require 'hashmake/hashed_arg'
3
+ require 'hashmake/hash_makeable'
@@ -0,0 +1,53 @@
1
+ module Hashmake
2
+ module HashMakeable
3
+
4
+ # Use the included hook to also extend the including class with HashMake
5
+ # class methods
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ def hash_make arg_specs, hashed_args, assign_args = true
11
+ arg_specs.each do |arg_spec|
12
+ raise ArgumentError, "arg_specs item #{arg_spec} is not a HashedArg" unless arg_spec.is_a?(HashedArg)
13
+ end
14
+ raise ArgumentError, "hashed_args is not a Hash" unless hashed_args.is_a?(Hash)
15
+
16
+ arg_specs.each do |arg_spec|
17
+ key = arg_spec.key
18
+ if hashed_args.has_key?(key)
19
+ val = hashed_args[key]
20
+ else
21
+ if arg_spec.reqd
22
+ raise ArgumentError, "hashed_args does not have required key #{key}"
23
+ else
24
+ if arg_spec.default.is_a?(Proc) && arg_spec.type != Proc
25
+ val = arg_spec.default.call
26
+ else
27
+ val = arg_spec.default
28
+ end
29
+ end
30
+ end
31
+
32
+ if arg_spec.array
33
+ raise ArgumentError, "val #{val} is not an array" unless val.is_a?(Array)
34
+ val.each do |item|
35
+ raise ArgumentError, "array item #{item} is not a #{arg_spec.type}" unless item.is_a?(arg_spec.type)
36
+ end
37
+ else
38
+ raise ArgumentError, "val #{val} is not a #{arg_spec.type}" unless val.is_a?(arg_spec.type)
39
+ end
40
+
41
+ if assign_args
42
+ raise ArgumentError, "value #{val} is not valid" unless arg_spec.validator.call(val)
43
+ self.instance_variable_set("@#{key.to_s}".to_sym, val)
44
+ end
45
+ end
46
+ end
47
+
48
+ module ClassMethods
49
+ # none right now, maybe later
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,31 @@
1
+ module Hashmake
2
+ class HashedArg
3
+
4
+ DEFAULT_ARGS = {
5
+ :reqd => true,
6
+ :validator => ->(a){true},
7
+ :array => false,
8
+ }
9
+
10
+ attr_reader :key, :type, :validator, :reqd, :default, :array
11
+
12
+ def initialize args
13
+ new_args = DEFAULT_ARGS.merge(args)
14
+
15
+ @key = new_args[:key]
16
+ @type = new_args[:type]
17
+ raise ArgumentError, "args[:type] #{@type} is not a Class" unless @type.is_a?(Class)
18
+
19
+ @validator = new_args[:validator]
20
+ @reqd = new_args[:reqd]
21
+ @array = new_args[:array]
22
+
23
+ unless @reqd
24
+ msg = "if hashed arg is not required, a default value or value generator (proc) must be defined via :default key"
25
+ raise ArgumentError, msg unless args.has_key?(:default)
26
+ @default = new_args[:default]
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ module Hashmake
2
+ # hashmake version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Hashmake::HashMakeable do
4
+ class MyTestClass
5
+ include HashMakeable
6
+
7
+ HASHED_ARG_SPECS = [
8
+ HashedArg.new(:reqd => true, :key => :reqd_string, :type => String, :validator => ->(a){ a.length < 10 }),
9
+ HashedArg.new(:reqd => false, :key => :not_reqd_float, :type => Float, :default => 0.0, :validator => ->(a){ a.between?(0.0,1.0) }),
10
+ ]
11
+
12
+ attr_reader :reqd_string, :not_reqd_float
13
+
14
+ def initialize hashed_args = {}
15
+ hash_make MyTestClass::HASHED_ARG_SPECS, hashed_args
16
+ end
17
+ end
18
+
19
+ describe '#hash_make' do
20
+ context 'for a reqd arg' do
21
+ it 'should raise an ArgumentError if not given in the hash' do
22
+ lambda { MyTestClass.new }.should raise_error(ArgumentError)
23
+ end
24
+ end
25
+
26
+ context 'for a not reqd arg' do
27
+ it 'should not raise an ArgumentError if not given in the hash' do
28
+ lambda { MyTestClass.new( :reqd_string => "okedoke" ) }.should_not raise_error(ArgumentError)
29
+ end
30
+ end
31
+
32
+ context 'any arg' do
33
+ it 'should not raise an ArgumentError if validator returns true' do
34
+ lambda { MyTestClass.new( :reqd_string => "okedoke" ) }.should_not raise_error(ArgumentError)
35
+ lambda { MyTestClass.new( :reqd_string => "okedoke", :not_reqd_float => 0.5 ) }.should_not raise_error(ArgumentError)
36
+ end
37
+
38
+ it 'should raise an ArgumentError if validator returns false' do
39
+ lambda { MyTestClass.new( :reqd_string => "okedokedokedoke" ) }.should raise_error(ArgumentError)
40
+ lambda { MyTestClass.new( :reqd_string => "okedoke", :not_reqd_float => 1.1 ) }.should raise_error(ArgumentError)
41
+ end
42
+
43
+ it 'should raise an ArgumentError if arg of incorrect type is given' do
44
+ lambda { MyTestClass.new( :reqd_string => 1.1 ) }.should raise_error(ArgumentError)
45
+ lambda { MyTestClass.new( :reqd_string => "okedoke", :not_reqd_float => "" ) }.should raise_error(ArgumentError)
46
+ end
47
+
48
+ it 'should set instance variables to correspond with each hashed arg' do
49
+ a = MyTestClass.new :reqd_string => "goodstuff", :not_reqd_float => 0.321
50
+ a.instance_variables.include?("@#{:reqd_string.to_s}".to_sym).should be_true
51
+ a.instance_variables.include?("@#{:not_reqd_float.to_s}".to_sym).should be_true
52
+ a.reqd_string.should eq("goodstuff")
53
+ a.not_reqd_float.should eq(0.321)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Hashmake::HashedArg do
4
+ it 'should raise ArgumentError if :reqd is false and no :default value is given' do
5
+ hash = {
6
+ :reqd => false, :key => :some_variable, :type => String
7
+ }
8
+
9
+ lambda { Hashmake::HashedArg.new hash }.should raise_error(ArgumentError)
10
+ end
11
+
12
+ it 'should not raise ArgumentError if :reqd is false and a :default value is given' do
13
+ hash = {
14
+ :reqd => false, :key => :some_variable, :type => String, :default => ""
15
+ }
16
+
17
+ lambda { Hashmake::HashedArg.new hash }.should_not raise_error(ArgumentError)
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ require 'hashmake'
3
+
4
+ describe Hashmake do
5
+ it "should have a VERSION constant" do
6
+ subject.const_get('VERSION').should_not be_empty
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require 'hashmake'
3
+
4
+ include Hashmake
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hashmake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - James Tunnell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.8'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.8'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.4'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.8'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.8'
78
+ description: Make hash-based object initialization easy! Provides hash_make method
79
+ that can consider parameter name, type, default value, validation, and requiredd/not,
80
+ according to the specification provided. Also, by default assigns by default to
81
+ instance variable of same name as parameter.
82
+ email: jamestunnell@lavabit.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - ChangeLog.rdoc
89
+ - Gemfile
90
+ - LICENSE.txt
91
+ - README.md
92
+ - README.rdoc
93
+ - Rakefile
94
+ - hashmake.gemspec
95
+ - lib/hashmake.rb
96
+ - lib/hashmake/hash_makeable.rb
97
+ - lib/hashmake/hashed_arg.rb
98
+ - lib/hashmake/version.rb
99
+ - spec/hash_makeable_spec.rb
100
+ - spec/hashed_arg_spec.rb
101
+ - spec/hashmake_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://rubygems.org/gems/hashmake
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ segments:
117
+ - 0
118
+ hash: -762830051
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ segments:
126
+ - 0
127
+ hash: -762830051
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.23
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Make hashed-based object initialization easy!
134
+ test_files:
135
+ - spec/hash_makeable_spec.rb
136
+ - spec/hashed_arg_spec.rb
137
+ - spec/hashmake_spec.rb
138
+ - spec/spec_helper.rb
139
+ has_rdoc: