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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c188815e5ebea95a603b6756349b233ae3deba0
4
- data.tar.gz: 4002fc2dfb32cc96e86cad69dafb88a727a09f88
3
+ metadata.gz: d05a546b16eb193d3474dc67e8fb081bd136df94
4
+ data.tar.gz: 551e941caf878ff1e484858a4e2759c1f95530fa
5
5
  SHA512:
6
- metadata.gz: de7e6fabc8c9cfd0de466917c036e1990a44684371b0710a7b37b603b75d869797cc0c3b92da9f1b05b9759c2bdb1c097d85158f540f6c19dcdb0d21d3a5124a
7
- data.tar.gz: 0f51bc346dcc8120c7a7cf3db4812abf204f6cc9ec876b09a5592bbd6c3d108becbd453a3cd4adb4f27bde5b6aa8bd342a5ff9da5baa299614624bd85b6d23c9
6
+ metadata.gz: b04b1ca0466ec4e3e5d65bb9eae0a46d33c2edfc5ca86743b6dc32cab47a552580b7838a176289c5631c7773e713b45f488a74886b9ead7a1528129b056cbbf8
7
+ data.tar.gz: f78bfb5f30de768ff7c17deee9235c50b3159b2fcb99f452dba964011eb5d983c9d90c6f4b1e945713fd6a6cc3e0d0e46289f75fa9f88eadcafb56324cbf74c3
data/Gemfile CHANGED
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in multidispatch_dsl.gemspec
4
4
  gemspec
5
+
6
+ platform :mri_19, :mri_20 do
7
+ gem 'debugger'
8
+ end
@@ -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)
@@ -2,6 +2,6 @@ module MultidispatchDSL
2
2
  autoload :DSL, 'multidispatch_dsl/dsl'
3
3
 
4
4
  def self.included(klass)
5
- klass.send(:include, ::MultidispatchDSL::DSL)
5
+ klass.send(:extend, ::MultidispatchDSL::DSL)
6
6
  end
7
7
  end
@@ -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 self.included(klass)
6
- klass.extend(::MultidispatchDSL::DSL::ClassMethods)
7
- end
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
- unless method_defined?(method_name)
15
- class_eval <<-RUBY, __FILE__, __LINE__ + 1
16
- def #{ method_name }(*args, &block)
17
- real_method_name = ::MultidispatchDSL::Generator.method_name_from_args(__callee__, args)
18
- if respond_to?(real_method_name)
19
- send(real_method_name, *args, &block)
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
- RUBY
25
- end
23
+ end
24
+ RUBY
26
25
  end
27
26
  end
28
27
  end
@@ -1,3 +1,3 @@
1
1
  module MultidispatchDSL
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  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
@@ -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.0
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-28 00:00:00.000000000 Z
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