multidispatch_dsl 0.1.0 → 0.1.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 +4 -4
- data/Gemfile +4 -0
- data/README.rdoc +15 -0
- data/lib/multidispatch_dsl.rb +1 -1
- data/lib/multidispatch_dsl/dsl.rb +15 -16
- data/lib/multidispatch_dsl/version.rb +1 -1
- data/spec/lib/multidispatch_dsl_generator_spec.rb +7 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/widget_serialier.rb +19 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d05a546b16eb193d3474dc67e8fb081bd136df94
|
4
|
+
data.tar.gz: 551e941caf878ff1e484858a4e2759c1f95530fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b04b1ca0466ec4e3e5d65bb9eae0a46d33c2edfc5ca86743b6dc32cab47a552580b7838a176289c5631c7773e713b45f488a74886b9ead7a1528129b056cbbf8
|
7
|
+
data.tar.gz: f78bfb5f30de768ff7c17deee9235c50b3159b2fcb99f452dba964011eb5d983c9d90c6f4b1e945713fd6a6cc3e0d0e46289f75fa9f88eadcafb56324cbf74c3
|
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -6,6 +6,7 @@ gem will allow you to do it.
|
|
6
6
|
|
7
7
|
{<img src="https://codeclimate.com/github/jalkoby/multidispatch_dsl.png" />}[https://codeclimate.com/github/jalkoby/multidispatch_dsl]
|
8
8
|
{<img src="https://travis-ci.org/jalkoby/uni_sender_gem.png?branch=master" alt="Build Status" />}[https://travis-ci.org/jalkoby/uni_sender_gem]
|
9
|
+
{<img src="https://badge.fury.io/rb/multidispatch_dsl.png" alt="Gem Version" />}[http://badge.fury.io/rb/multidispatch_dsl]
|
9
10
|
|
10
11
|
== Installation
|
11
12
|
|
@@ -93,6 +94,20 @@ Just include MultidispatchDSL into your class and your will get `mdef` class met
|
|
93
94
|
|
94
95
|
As you can see defining version of method with *yield* is little bit tricky. That because defining method from block add extra scope. More details about it here http://www.andylindeman.com/2011/01/08/defining-methods-using-blocks-in-ruby.html
|
95
96
|
|
97
|
+
If you need define version of method for any number arguments of any types use `:anything` symbol
|
98
|
+
|
99
|
+
mdef(:process, String) do |value|
|
100
|
+
{ :string => value }
|
101
|
+
end
|
102
|
+
|
103
|
+
mdef(:process, Fixnum) do |value|
|
104
|
+
{ :int => value }
|
105
|
+
end
|
106
|
+
|
107
|
+
mdef(:process, :anything) do |value|
|
108
|
+
{ :anything => value }
|
109
|
+
end
|
110
|
+
|
96
111
|
If you desire add this functionality to all classes just include MultidispatchDSL to Object class:
|
97
112
|
|
98
113
|
Object.send(:include, MultidispatchDSL)
|
data/lib/multidispatch_dsl.rb
CHANGED
@@ -2,27 +2,26 @@ require 'multidispatch_dsl/generator'
|
|
2
2
|
require 'multidispatch_dsl/missing_declaration_error'
|
3
3
|
|
4
4
|
module MultidispatchDSL::DSL
|
5
|
-
def
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
def mdef(method_name, *declaration, &block)
|
11
|
-
real_method_name = ::MultidispatchDSL::Generator.method_name_from_declaration(method_name, declaration)
|
12
|
-
define_method(real_method_name, &block)
|
5
|
+
def mdef(method_name, *declaration, &block)
|
6
|
+
real_method_name = ::MultidispatchDSL::Generator.method_name_from_declaration(method_name, declaration)
|
7
|
+
define_method(real_method_name, &block)
|
13
8
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
unless method_defined?(method_name)
|
10
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
11
|
+
def #{ method_name }(*args, &block)
|
12
|
+
real_method_name = ::MultidispatchDSL::Generator.method_name_from_args(__callee__, args)
|
13
|
+
if respond_to?(real_method_name)
|
14
|
+
send(real_method_name, *args, &block)
|
15
|
+
else
|
16
|
+
fallback_method_name = "#{ method_name }_anything"
|
17
|
+
if respond_to?(fallback_method_name)
|
18
|
+
send(fallback_method_name, *args, &block)
|
20
19
|
else
|
21
20
|
raise ::MultidispatchDSL::MissingDeclarationError.new(__callee__, args)
|
22
21
|
end
|
23
22
|
end
|
24
|
-
|
25
|
-
|
23
|
+
end
|
24
|
+
RUBY
|
26
25
|
end
|
27
26
|
end
|
28
27
|
end
|
@@ -20,4 +20,11 @@ describe MultidispatchDSL::Generator do
|
|
20
20
|
described_class.declaration_from_args(args)
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
it 'allow set default case' do
|
25
|
+
WidgetSerializer.process("string").should == { :string => "string" }
|
26
|
+
WidgetSerializer.process(1).should == { :int => 1 }
|
27
|
+
WidgetSerializer.process(:symbol).should == { :anything => :symbol }
|
28
|
+
WidgetSerializer.process([:array]).should == { :anything => [:array] }
|
29
|
+
end
|
23
30
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,3 +1,9 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
Bundler.require(:default)
|
3
3
|
Dir["#{ File.dirname(__FILE__) }/support/**/*.rb"].each { |f| require f }
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
7
|
+
config.filter_run :focus => true
|
8
|
+
config.run_all_when_everything_filtered = true
|
9
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class WidgetSerializer
|
2
|
+
include MultidispatchDSL
|
3
|
+
|
4
|
+
def self.process(record)
|
5
|
+
new.process(record)
|
6
|
+
end
|
7
|
+
|
8
|
+
mdef(:process, String) do |value|
|
9
|
+
{ :string => value }
|
10
|
+
end
|
11
|
+
|
12
|
+
mdef(:process, Fixnum) do |value|
|
13
|
+
{ :int => value }
|
14
|
+
end
|
15
|
+
|
16
|
+
mdef(:process, :anything) do |value|
|
17
|
+
{ :anything => value }
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multidispatch_dsl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Pchelincev
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -75,6 +75,7 @@ files:
|
|
75
75
|
- spec/lib/multidispatch_dsl_spec.rb
|
76
76
|
- spec/spec_helper.rb
|
77
77
|
- spec/support/test_class.rb
|
78
|
+
- spec/support/widget_serialier.rb
|
78
79
|
homepage: ''
|
79
80
|
licenses:
|
80
81
|
- MIT
|
@@ -104,3 +105,4 @@ test_files:
|
|
104
105
|
- spec/lib/multidispatch_dsl_spec.rb
|
105
106
|
- spec/spec_helper.rb
|
106
107
|
- spec/support/test_class.rb
|
108
|
+
- spec/support/widget_serialier.rb
|