smart_methods 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/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +96 -0
- data/Rakefile +12 -0
- data/lib/smart_methods.rb +37 -0
- data/lib/smart_methods/version.rb +3 -0
- data/smart_methods.gemspec +23 -0
- data/test/smart_methods_test.rb +67 -0
- data/test/test_helper.rb +7 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrey Deryabin
|
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,96 @@
|
|
1
|
+
# smart_methods
|
2
|
+
|
3
|
+
smart_methods helps to define class and instance methods using pretty DSL.
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/aderyabin/smart_methods.png)](http://travis-ci.org/aderyabin/smart_methods)
|
6
|
+
[![Gem Version](https://badge.fury.io/aderyabin/smart_methods.png)](http://badge.fury.io/aderyabin/smart_methods)
|
7
|
+
[![Dependency Status](https://gemnasium.com/aderyabin/smart_methods.png)](https://gemnasium.com/aderyabin/smart_methods)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/aderyabin/smart_methods.png)](https://codeclimate.com/github/aderyabin/smart_methods)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'smart_methods'
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
Working with class methods:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class Source
|
22
|
+
extend SmartMethods
|
23
|
+
smart_methods :status, :type
|
24
|
+
|
25
|
+
self.status 'undefined'
|
26
|
+
self.type 'undefined'
|
27
|
+
end
|
28
|
+
|
29
|
+
class Sms < Source
|
30
|
+
self.type 'sms'
|
31
|
+
end
|
32
|
+
|
33
|
+
class Phone < Source
|
34
|
+
end
|
35
|
+
|
36
|
+
Phone.type = 'phone'
|
37
|
+
|
38
|
+
Phone.type
|
39
|
+
# => 'phone'
|
40
|
+
|
41
|
+
Sms.type
|
42
|
+
# => 'sms'
|
43
|
+
|
44
|
+
Phone.status
|
45
|
+
# => "undefined"
|
46
|
+
```
|
47
|
+
|
48
|
+
Working with instance methods:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Source
|
52
|
+
extend SmartMethods
|
53
|
+
smart_methods :status, :type
|
54
|
+
|
55
|
+
status { "new_" + type.to_s }
|
56
|
+
end
|
57
|
+
|
58
|
+
Source.new.type
|
59
|
+
# => nil
|
60
|
+
|
61
|
+
Source.new.status
|
62
|
+
# => 'new_'
|
63
|
+
|
64
|
+
class Sms < Source
|
65
|
+
type { 'sms' }
|
66
|
+
end
|
67
|
+
|
68
|
+
Sms.new.type
|
69
|
+
# => 'sms'
|
70
|
+
|
71
|
+
Sms.new.status
|
72
|
+
# => 'new_sms'
|
73
|
+
|
74
|
+
class Phone < Sms
|
75
|
+
type { 'phone' }
|
76
|
+
end
|
77
|
+
|
78
|
+
Phone.new.status
|
79
|
+
# => 'new_phone'
|
80
|
+
|
81
|
+
class Email < Sms
|
82
|
+
end
|
83
|
+
|
84
|
+
email = Email.new
|
85
|
+
email.type = 'email'
|
86
|
+
|
87
|
+
email.type
|
88
|
+
# => 'email'
|
89
|
+
|
90
|
+
email.status
|
91
|
+
# => 'new_email'
|
92
|
+
```
|
93
|
+
|
94
|
+
## Thanks
|
95
|
+
Sponsored by [Evil Martians].
|
96
|
+
[Evil Martians]: http://evilmartians.com/
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module SmartMethods
|
2
|
+
def smart_methods(*names)
|
3
|
+
names.each do |name|
|
4
|
+
instance_eval %Q{
|
5
|
+
def #{name}(v = nil, &block)
|
6
|
+
if block_given?
|
7
|
+
@#{name} = block
|
8
|
+
elsif v
|
9
|
+
@#{name} = v
|
10
|
+
else
|
11
|
+
return @#{name} unless superclass.respond_to?(:#{name}) and value = superclass.#{name}
|
12
|
+
@#{name} ||= value.clone
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def #{name}=(v)
|
17
|
+
@#{name} = v
|
18
|
+
end
|
19
|
+
}
|
20
|
+
|
21
|
+
class_eval %Q{
|
22
|
+
def #{name}
|
23
|
+
return @#{name} if @#{name}
|
24
|
+
if self.class.#{name}.is_a?(Proc)
|
25
|
+
@#{name} = instance_eval(&self.class.#{name})
|
26
|
+
else
|
27
|
+
@#{name} = self.class.#{name}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def #{name}=(v)
|
32
|
+
@#{name} = v
|
33
|
+
end
|
34
|
+
}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'smart_methods/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "smart_methods"
|
8
|
+
spec.version = SmartMethods::VERSION
|
9
|
+
spec.authors = ["Andrey Deryabin"]
|
10
|
+
spec.email = ["deriabin@gmail.com"]
|
11
|
+
spec.description = %q{smart_methods helps to define class and instance methods using pretty DSL}
|
12
|
+
spec.summary = %q{smart_methods helps to define class and instance methods using pretty DSL}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "turn"
|
23
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe SmartMethods do
|
5
|
+
before(:each) do
|
6
|
+
class Foo
|
7
|
+
extend SmartMethods
|
8
|
+
smart_methods :a, :b, :c
|
9
|
+
|
10
|
+
b { 'lorem' }
|
11
|
+
c { 'lorem' }
|
12
|
+
end
|
13
|
+
|
14
|
+
class SubFoo < Foo
|
15
|
+
end
|
16
|
+
|
17
|
+
class SubSubFoo < SubFoo
|
18
|
+
a { c + " ipsum" }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "class must respond to a" do
|
23
|
+
Foo.must_respond_to :a
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'subclass must respond to a' do
|
27
|
+
SubFoo.must_respond_to :a
|
28
|
+
end
|
29
|
+
|
30
|
+
it "instance must respond to a" do
|
31
|
+
Foo.new.must_respond_to :a
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'subclass instance method must respond to a' do
|
35
|
+
SubFoo.new.must_respond_to :a
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'may change subclass method without class' do
|
39
|
+
SubSubFoo.b = 'ipsum'
|
40
|
+
SubSubFoo.b.must_equal 'ipsum'
|
41
|
+
Foo.b.call.must_equal 'lorem'
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'class instance method should be "lorem"' do
|
45
|
+
Foo.new.b.must_equal 'lorem'
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'subclass instance method should be "lorem"' do
|
49
|
+
SubFoo.new.b.must_equal 'lorem'
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'instance method could be overriden' do
|
53
|
+
instance = SubFoo.new
|
54
|
+
instance.b = 'ipsum'
|
55
|
+
instance.b.must_equal 'ipsum'
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'instance not change class method' do
|
59
|
+
SubFoo.new.b = 'ipsum'
|
60
|
+
SubFoo.b.call.must_equal 'lorem'
|
61
|
+
Foo.b.call.must_equal 'lorem'
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'may use proc' do
|
65
|
+
SubSubFoo.new.a.must_equal 'lorem ipsum'
|
66
|
+
end
|
67
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_methods
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrey Deryabin
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
type: :development
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.3'
|
29
|
+
name: bundler
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
type: :development
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
name: rake
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
type: :development
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
prerelease: false
|
55
|
+
version_requirements: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
name: turn
|
62
|
+
description: smart_methods helps to define class and instance methods using pretty
|
63
|
+
DSL
|
64
|
+
email:
|
65
|
+
- deriabin@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .travis.yml
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/smart_methods.rb
|
77
|
+
- lib/smart_methods/version.rb
|
78
|
+
- smart_methods.gemspec
|
79
|
+
- test/smart_methods_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
homepage: ''
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.23
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: smart_methods helps to define class and instance methods using pretty DSL
|
106
|
+
test_files:
|
107
|
+
- test/smart_methods_test.rb
|
108
|
+
- test/test_helper.rb
|