method_object 0.0.3 → 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7cda58f183562da4e7e27ba4b270919de0f55c53
4
+ data.tar.gz: 1580f33fec747b9be76c0c0e82b351e5aa55714e
5
+ SHA512:
6
+ metadata.gz: f2599f1d96b78a0bca5e4da74411d979f589bdfcaea5c9779a24d2a8b3876247db31ba53fe87b8dca9ebdc4f47cb881799f33430cc0152f4304a32b8f27df9b3
7
+ data.tar.gz: 285b9e3159846a104cc599e62cff4e5c364f27f7ad27651205750e15aa410a752838cb626e0eb4ee6f2e5675feaacdbee8d3d9d199dcc9b22e9597ae2fcc4a4b
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - 1.9.2
6
+ - jruby-18mode
7
+ - jruby-19mode
8
+ - rbx-2.1.1
9
+ - ruby-head
10
+ - jruby-head
11
+ - 1.8.7
12
+ - ree
data/Gemfile CHANGED
@@ -1,8 +1,3 @@
1
- source 'https://rubygems.org'
1
+ source 'http://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in method_object.gemspec
4
3
  gemspec
5
-
6
- group :development do
7
- gem 'rake'
8
- end
data/README.md CHANGED
@@ -20,62 +20,25 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
-
24
- class Car
25
-
26
- # take the following long method
27
- def drive_to location, speed=:slow
28
- car = Car.new
29
- car.location = GoogleMaps.find(location)
30
- car.start!
31
- car.speed = speed
32
- return car
33
- end
34
-
23
+ ```ruby
24
+ class OpenChest < MethodObject
25
+ option :size, default: 12
26
+ option :color, required: true
27
+ def call
28
+ [@size, color]
35
29
  end
30
+ end
36
31
 
37
- Rather then complecting your Car object with more the one method that all
38
- have to do with driving to a location you can use a method object to break
39
- up your code without cluttering Car
40
-
41
- class Car
42
-
43
- class DriveTo < MethodObject
44
-
45
- # Note: call takes no arguments. The hash given to Car.call is
46
- # turn into instance variables
47
- def call
48
- find_location!
49
- start_car!
50
- set_speed!
51
- return car
52
- end
53
-
54
- def car
55
- @car ||= Car.new
56
- end
57
-
58
- def find_location!
59
- car.location = GoogleMaps.find(@location)
60
- end
61
-
62
- def start_car!
63
- car.start!
64
- end
65
-
66
- def set_speed!
67
- car.speed = @speed
68
- end
69
-
70
- end
71
-
72
- def drive_to location, speed=:slow
73
- DriveTo.call(:location => location, :speed => speed)
74
- end
75
-
32
+ class OpenMagicChest < OpenChest
33
+ option :size, required: true
34
+ option :key_type, default: ->{ :upside }
35
+ def call
36
+ [size, color, @key_type]
76
37
  end
38
+ end
39
+ ```
77
40
 
78
- ## Contributing
41
+ # Contributing
79
42
 
80
43
  1. Fork it
81
44
  2. Create your feature branch (`git checkout -b my-new-feature`)
data/lib/method_object.rb CHANGED
@@ -1,61 +1,21 @@
1
- require 'simple_struct'
1
+ class MethodObject
2
2
 
3
- class MethodObject < SimpleStruct
4
-
5
- VERSION = "0.0.3"
3
+ VERSION = '0.1.0'
6
4
 
7
5
  class << self
6
+ private :new
8
7
 
9
- def new *members, &block
10
- subclass = super(*members, &block)
11
- subclass.send(:private_class_method, :new)
12
- subclass
13
- end
14
-
15
- def call *args
16
- new(*args).call
8
+ def call *args, &block
9
+ new.call(*args, &block)
17
10
  end
18
11
 
19
12
  def to_proc
20
13
  method(:call).to_proc
21
14
  end
22
-
23
15
  end
24
16
 
25
- end
26
-
27
- eval <<-RUBY if $0 == __FILE__
28
-
29
- require 'test/unit'
30
-
31
- # example method objects
32
- FindTreasureChest = MethodObject.new(:color, :size) do
33
17
  def call
34
- [@color, @size, :treasure_chest]
35
- end
36
- end
37
-
38
- #tests
39
- class MethodObjectTestUnitTestCase < Test::Unit::TestCase
40
-
41
- def test_method_objects_have_private_new_method
42
- assert_raises(NoMethodError){ FindTreasureChest.new }
43
- end
44
-
45
- def test_that_expected_options_are_ok
46
- run_proc_tests FindTreasureChest
47
- end
48
-
49
- def test_to_proc
50
- run_proc_tests FindTreasureChest.to_proc
51
- end
52
-
53
- def run_proc_tests proc
54
- assert_equal proc.call, [nil, nil, :treasure_chest]
55
- assert_equal proc.call(:red), [:red, nil, :treasure_chest]
56
- assert_equal proc.call(:red, 42), [:red, 42, :treasure_chest]
18
+ raise "#{self.class}#call is not defined"
57
19
  end
58
20
 
59
21
  end
60
-
61
- RUBY
@@ -15,5 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = MethodObject::VERSION
17
17
 
18
- gem.add_runtime_dependency "simple_struct"
18
+ gem.add_development_dependency 'rake'
19
+ gem.add_development_dependency 'rspec'
20
+ gem.add_development_dependency 'pry-debugger'
19
21
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe MethodObject do
4
+
5
+ let(:block) { ->{} }
6
+
7
+ class SimpleMethodObject < MethodObject
8
+ def call *args, &block
9
+ [args, block]
10
+ end
11
+ end
12
+
13
+ class SimpleMethodObjectWithNoCallMethod < MethodObject
14
+ end
15
+
16
+ shared_examples 'a method object' do
17
+ describe '.new' do
18
+ it 'should be private' do
19
+ expect(subject.private_methods).to include :new
20
+ end
21
+ end
22
+ describe '.to_proc' do
23
+ it 'should return a proc that calls call' do
24
+ expect(subject.to_proc).to be_a Proc
25
+ expect(subject).to receive(:call).with(:a,:b)
26
+ subject.to_proc.(:a, :b)
27
+ end
28
+ end
29
+ end
30
+
31
+
32
+ describe SimpleMethodObjectWithNoCallMethod do
33
+ subject{ SimpleMethodObjectWithNoCallMethod }
34
+ it_behaves_like 'a method object'
35
+ describe '.call' do
36
+ it 'should raise a SimpleMethodObjectWithNoCallMethod#call is not defined error' do
37
+ expect{ subject.call }.to raise_error StandardError, "SimpleMethodObjectWithNoCallMethod#call is not defined"
38
+ end
39
+ end
40
+ end
41
+
42
+ describe SimpleMethodObject do
43
+ subject{ SimpleMethodObject }
44
+ it_behaves_like 'a method object'
45
+ it "should curry all args and block to #call" do
46
+ expect( subject.call(1,2,3, &block) ).to eq [[1,2,3], block]
47
+ expect( subject.to_proc.call(1,2,3, &block) ).to eq [[1,2,3], block]
48
+ end
49
+ end
50
+
51
+ describe 'used as a block' do
52
+ it 'should work' do
53
+ expect( [1,2,3].map(&SimpleMethodObject) ).to eq [
54
+ [[1], nil],
55
+ [[2], nil],
56
+ [[3], nil],
57
+ ]
58
+ end
59
+ end
60
+
61
+ describe 'used to define a method' do
62
+ it 'should work' do
63
+ _class = Class.new do
64
+ define_method :foo, &SimpleMethodObject
65
+ end
66
+
67
+ expect( _class.new.foo('hello', :world, &block) ).to eq [['hello', :world], block]
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,19 @@
1
+ require '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 CHANGED
@@ -1,30 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
5
- prerelease:
4
+ version: 0.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jared Grippe
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-28 00:00:00.000000000 Z
11
+ date: 2013-11-06 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: simple_struct
14
+ name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
- type: :runtime
20
+ type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-debugger
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
+ - - '>='
28
53
  - !ruby/object:Gem::Version
29
54
  version: '0'
30
55
  description: wraps up the method object pattern into a class
@@ -35,40 +60,39 @@ extensions: []
35
60
  extra_rdoc_files: []
36
61
  files:
37
62
  - .gitignore
63
+ - .rspec
64
+ - .travis.yml
38
65
  - Gemfile
39
66
  - LICENSE
40
67
  - README.md
41
68
  - Rakefile
42
69
  - lib/method_object.rb
43
70
  - method_object.gemspec
71
+ - spec/method_object_spec.rb
72
+ - spec/spec_helper.rb
44
73
  homepage: https://github.com/deadlyicon/method_object
45
74
  licenses: []
75
+ metadata: {}
46
76
  post_install_message:
47
77
  rdoc_options: []
48
78
  require_paths:
49
79
  - lib
50
80
  required_ruby_version: !ruby/object:Gem::Requirement
51
- none: false
52
81
  requirements:
53
- - - ! '>='
82
+ - - '>='
54
83
  - !ruby/object:Gem::Version
55
84
  version: '0'
56
- segments:
57
- - 0
58
- hash: 296936207624278339
59
85
  required_rubygems_version: !ruby/object:Gem::Requirement
60
- none: false
61
86
  requirements:
62
- - - ! '>='
87
+ - - '>='
63
88
  - !ruby/object:Gem::Version
64
89
  version: '0'
65
- segments:
66
- - 0
67
- hash: 296936207624278339
68
90
  requirements: []
69
91
  rubyforge_project:
70
- rubygems_version: 1.8.24
92
+ rubygems_version: 2.1.10
71
93
  signing_key:
72
- specification_version: 3
94
+ specification_version: 4
73
95
  summary: wraps up the method object pattern into a class
74
- test_files: []
96
+ test_files:
97
+ - spec/method_object_spec.rb
98
+ - spec/spec_helper.rb