module_with_params 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/README.md +53 -0
- data/Rakefile +14 -0
- data/lib/module_with_params.rb +19 -0
- data/lib/module_with_params/version.rb +3 -0
- data/module_with_params.gemspec +15 -0
- data/specs/common.rb +10 -0
- data/specs/module_with_params_spec.rb +48 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
This gem is a simple way to solve a problem I often have: How
|
2
|
+
to write a module designed to be included and allow the class including
|
3
|
+
it to configure its behavior.
|
4
|
+
|
5
|
+
I have tried many solutions but this one is the most elegant one I
|
6
|
+
have for now and the code is at the same time really short and easy to
|
7
|
+
understand.
|
8
|
+
|
9
|
+
# Supported versions
|
10
|
+
|
11
|
+
- MRI 1.8.7+
|
12
|
+
|
13
|
+
# Example
|
14
|
+
|
15
|
+
You can pass a hash as argument:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
module MySuperModule
|
19
|
+
include ModuleWithParams
|
20
|
+
|
21
|
+
def number
|
22
|
+
_MySuperModule_module_options[:number]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
class A
|
28
|
+
include MySuperModule[:number => 234]
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
A.new.number # => 234
|
33
|
+
```
|
34
|
+
|
35
|
+
Or an array:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
module MySuperModule
|
39
|
+
include ModuleWithParams
|
40
|
+
|
41
|
+
def number
|
42
|
+
_MySuperModule_module_options[0]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
class A
|
48
|
+
include MySuperModule[234]
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
A.new.number # => 234
|
53
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "module_with_params/version"
|
2
|
+
|
3
|
+
module ModuleWithParams
|
4
|
+
def self.included(klass)
|
5
|
+
klass.send(:extend, ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def [](*args)
|
10
|
+
clone.tap do |mod|
|
11
|
+
module_name = self.name
|
12
|
+
mod.instance_eval do
|
13
|
+
define_method(:"_#{module_name}_module_options"){ (args.size == 1) ? args[0] : args }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/module_with_params/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Julien Ammous"]
|
6
|
+
gem.email = []
|
7
|
+
gem.description = %q{include module with parameters}
|
8
|
+
gem.summary = %q{module with parameters}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split("\n")
|
12
|
+
gem.name = "module_with_params"
|
13
|
+
gem.require_paths = ["lib"]
|
14
|
+
gem.version = ModuleWithParams::VERSION
|
15
|
+
end
|
data/specs/common.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path('../common', __FILE__)
|
2
|
+
|
3
|
+
module TestModule
|
4
|
+
include ModuleWithParams
|
5
|
+
|
6
|
+
def number
|
7
|
+
_TestModule_module_options[:number]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module TestModuleArray
|
12
|
+
include ModuleWithParams
|
13
|
+
|
14
|
+
def first
|
15
|
+
_TestModuleArray_module_options[0]
|
16
|
+
end
|
17
|
+
|
18
|
+
def second
|
19
|
+
_TestModuleArray_module_options[1]
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
describe 'Module with params' do
|
25
|
+
|
26
|
+
it 'can be included included in multiple separate classes' do
|
27
|
+
cl = Class.new do
|
28
|
+
include TestModule[:number => 5]
|
29
|
+
end
|
30
|
+
|
31
|
+
cl2 = Class.new do
|
32
|
+
include TestModule[:number => 43]
|
33
|
+
end
|
34
|
+
|
35
|
+
cl.new.number.should == 5
|
36
|
+
cl2.new.number.should == 43
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'can be used with non hash parameters' do
|
40
|
+
cl = Class.new do
|
41
|
+
include TestModuleArray["hello", 43]
|
42
|
+
end
|
43
|
+
obj = cl.new
|
44
|
+
obj.first.should == "hello"
|
45
|
+
obj.second.should == 43
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: module_with_params
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Julien Ammous
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: include module with parameters
|
15
|
+
email: []
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .gitignore
|
21
|
+
- Gemfile
|
22
|
+
- README.md
|
23
|
+
- Rakefile
|
24
|
+
- lib/module_with_params.rb
|
25
|
+
- lib/module_with_params/version.rb
|
26
|
+
- module_with_params.gemspec
|
27
|
+
- specs/common.rb
|
28
|
+
- specs/module_with_params_spec.rb
|
29
|
+
homepage: ''
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
hash: -1270772489623524378
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
hash: -1270772489623524378
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.11
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: module with parameters
|
59
|
+
test_files: []
|