options_hash-method_object 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +12 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +58 -0
- data/Rakefile +1 -0
- data/lib/options_hash/method_object.rb +54 -0
- data/options_hash-method_object.gemspec +26 -0
- data/spec/options_hash/method_object_spec.rb +63 -0
- data/spec/spec_helper.rb +19 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c905985693808cabd9a0925d45c76358664f666e
|
4
|
+
data.tar.gz: fb96dce849a0ff02ffd34d577b53f391e06b3bfc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 73d3e22e24591b6b2c12640a7c0867ddfb615caf8dcf3d40b61b95cdab2c5c50064f0aa1d68be0c06d7d1ed54048183ba0fa3e04580ee8ebb230c34bb4692bc6
|
7
|
+
data.tar.gz: f38eff8163adc3294aca7ace796b97301ed62cdf123ce0dbbc6631c22b9492255fdbe260be22eb655dc1d8e2d56331888fb54152ef4378921fb9665175f638dd
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jared Grippe
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# OptionsHash::MethodObject
|
2
|
+
|
3
|
+
A MethodObject with an OptionsHash built in
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'options_hash-method_object'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install options_hash-method_object
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class CreatePerson < OptionsHash::MethodObject
|
23
|
+
|
24
|
+
required :name
|
25
|
+
optional :favorite_color, default: ->{ 'blue' }
|
26
|
+
|
27
|
+
def call
|
28
|
+
build_person!
|
29
|
+
validate_person!
|
30
|
+
create_person!
|
31
|
+
@person
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_person!
|
35
|
+
@person = Person.new(options.to_hash)
|
36
|
+
end
|
37
|
+
|
38
|
+
def validate_person!
|
39
|
+
@person.valid? or raise 'invalid person'
|
40
|
+
end
|
41
|
+
|
42
|
+
def create_person!
|
43
|
+
@person.save!
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
**For details about the `require` and `optional` methods see [OptionsHash](https://github.com/deadlyicon/options_hash)**
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
1. Fork it
|
55
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
56
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
57
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
58
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'options_hash'
|
2
|
+
|
3
|
+
class OptionsHash::MethodObject
|
4
|
+
|
5
|
+
VERSION = '0.0.1'
|
6
|
+
|
7
|
+
def self.inherited(subclass)
|
8
|
+
subclass.send :extend, ClassMethods
|
9
|
+
subclass.send :include, InstanceMethods
|
10
|
+
subclass
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def call options={}
|
15
|
+
new(options).call
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_proc
|
19
|
+
method(:call).to_proc
|
20
|
+
end
|
21
|
+
|
22
|
+
def options
|
23
|
+
@options ||= Class.new(superclass.respond_to?(:options) ?
|
24
|
+
superclass.options : OptionsHash)
|
25
|
+
end
|
26
|
+
|
27
|
+
def required *keys, &block
|
28
|
+
options.required *keys, &block
|
29
|
+
end
|
30
|
+
|
31
|
+
def optional *keys, &block
|
32
|
+
options.optional *keys, &block
|
33
|
+
end
|
34
|
+
|
35
|
+
def options_reader *keys
|
36
|
+
keys.each do |key|
|
37
|
+
options.option?(key) or raise KeyError, "#{key} is not an option", caller(2)
|
38
|
+
define_method(key){ self.options[key] }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def option_readers!
|
43
|
+
options_reader *options.keys
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
def initialize options={}
|
49
|
+
@options = self.class.options.parse(options)
|
50
|
+
end
|
51
|
+
attr_reader :options
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'options_hash/method_object'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "options_hash-method_object"
|
8
|
+
spec.version = OptionsHash::MethodObject::VERSION
|
9
|
+
spec.authors = ["Jared Grippe"]
|
10
|
+
spec.email = ["jared@deadlyicon.com"]
|
11
|
+
spec.description = %q{A MethodObject with an OptionsHash built in}
|
12
|
+
spec.summary = %q{A MethodObject with an OptionsHash built in}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency 'options_hash'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'pry-debugger'
|
26
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class CreatePerson < OptionsHash::MethodObject
|
4
|
+
|
5
|
+
required :name
|
6
|
+
optional :favorite_color, default: ->{ 'blue' }
|
7
|
+
|
8
|
+
options_reader :name, :favorite_color
|
9
|
+
|
10
|
+
def call
|
11
|
+
options
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
class CreateRobot < CreatePerson
|
17
|
+
|
18
|
+
required :model_number
|
19
|
+
|
20
|
+
option_readers!
|
21
|
+
|
22
|
+
def call
|
23
|
+
[name, favorite_color, model_number]
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe OptionsHash::MethodObject do
|
29
|
+
subject{ OptionsHash::MethodObject }
|
30
|
+
|
31
|
+
it { should_not respond_to :call }
|
32
|
+
it { should_not respond_to :options_hash }
|
33
|
+
it { should_not respond_to :required }
|
34
|
+
it { should_not respond_to :optional }
|
35
|
+
it { should_not respond_to :options }
|
36
|
+
|
37
|
+
describe CreatePerson do
|
38
|
+
describe '.call' do
|
39
|
+
it 'should take one optional argument' do
|
40
|
+
expect( CreatePerson.method(:call).arity ).to eq -1
|
41
|
+
expect{ CreatePerson.call }.to raise_error ArgumentError, 'required options: :name'
|
42
|
+
expect{ CreatePerson.call name: 'Jared' }.to_not raise_error
|
43
|
+
options = CreatePerson.call name: 'Jared'
|
44
|
+
expect(options.name).to eq 'Jared'
|
45
|
+
expect(options.favorite_color).to eq 'blue'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
describe '.options' do
|
49
|
+
it 'should return a hash of the possible arguments' do
|
50
|
+
expect( CreatePerson.options.keys.to_set ).to eq Set[:favorite_color, :name]
|
51
|
+
expect( CreatePerson.options[:name] ).to be_required
|
52
|
+
expect( CreatePerson.options[:favorite_color] ).to_not be_required
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should be subclassable' do
|
58
|
+
expect( CreateRobot.options.keys.to_set ).to eq Set[:favorite_color, :name, :model_number]
|
59
|
+
expect{ CreateRobot.call }.to raise_error ArgumentError, 'required options: :model_number, :name'
|
60
|
+
expect( CreateRobot.call(name: 'Hal', model_number: 14) ).to eq ['Hal', 'blue', 14]
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'options_hash/method_object'
|
2
|
+
require 'pry'
|
3
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
4
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
5
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
6
|
+
# loaded once.
|
7
|
+
#
|
8
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
|
14
|
+
# Run specs in random order to surface order dependencies. If you find an
|
15
|
+
# order dependency and want to debug it, you can fix the order by providing
|
16
|
+
# the seed, which is printed after each run.
|
17
|
+
# --seed 1234
|
18
|
+
config.order = 'random'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: options_hash-method_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jared Grippe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: options_hash
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-debugger
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A MethodObject with an OptionsHash built in
|
84
|
+
email:
|
85
|
+
- jared@deadlyicon.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .travis.yml
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/options_hash/method_object.rb
|
98
|
+
- options_hash-method_object.gemspec
|
99
|
+
- spec/options_hash/method_object_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
homepage: ''
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - '>='
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubyforge_project:
|
121
|
+
rubygems_version: 2.1.10
|
122
|
+
signing_key:
|
123
|
+
specification_version: 4
|
124
|
+
summary: A MethodObject with an OptionsHash built in
|
125
|
+
test_files:
|
126
|
+
- spec/options_hash/method_object_spec.rb
|
127
|
+
- spec/spec_helper.rb
|