variants 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -0
- data/Gemfile +2 -0
- data/README.md +48 -0
- data/lib/variants/helpers.rb +11 -16
- data/lib/variants/version.rb +1 -1
- data/spec/actionview_extensions_spec.rb +85 -50
- data/variants.gemspec +0 -3
- metadata +7 -36
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
script: 'bundle exec rspec spec'
|
data/Gemfile
CHANGED
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Variants [ ![Build status](http://travis-ci.org/kossnocorp/variants.png) ](http://travis-ci.org/kossnocorp/variants)
|
2
|
+
|
3
|
+
Be boss of your views.
|
4
|
+
|
5
|
+
TODO: More info, stay in touch.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Puts this line into Gemfile:
|
10
|
+
|
11
|
+
``` ruby
|
12
|
+
gem 'variants', '0.1.0'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Examples
|
16
|
+
|
17
|
+
``` haml
|
18
|
+
- if variant :logged_in?
|
19
|
+
= link_to 'Sign out', sign_out_path
|
20
|
+
```
|
21
|
+
|
22
|
+
TODO: Examples, examples, examples
|
23
|
+
|
24
|
+
## API
|
25
|
+
|
26
|
+
TODO: Write full API
|
27
|
+
|
28
|
+
## Roadmap
|
29
|
+
|
30
|
+
TODO: Write roadmap
|
31
|
+
|
32
|
+
## Contributors
|
33
|
+
|
34
|
+
* @kossnocorp
|
35
|
+
|
36
|
+
Special thanks to @brainopia.
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The MIT License
|
41
|
+
|
42
|
+
Copyright (c) 2011 Sasha Koss
|
43
|
+
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
45
|
+
|
46
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
47
|
+
|
48
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/variants/helpers.rb
CHANGED
@@ -1,32 +1,27 @@
|
|
1
1
|
module Variants
|
2
2
|
module Helpers
|
3
|
-
|
3
|
+
def self.included base
|
4
|
+
base.send(:include, InstanceMethods)
|
5
|
+
end
|
4
6
|
|
5
7
|
module InstanceMethods
|
6
|
-
def variant name,
|
8
|
+
def variant name, *args
|
7
9
|
|
8
10
|
# If Rails env is development
|
9
11
|
if Rails.env.development?
|
10
12
|
# Override conditions if params[:variant_name] is presented
|
11
|
-
|
12
|
-
return self.params[name
|
13
|
+
unless self.params[name].nil?
|
14
|
+
return self.params[name].to_s == 'true'
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
# If
|
17
|
-
|
18
|
-
return
|
19
|
-
end
|
20
|
-
|
21
|
-
# Add ? to end of name
|
22
|
-
method_name = "#{name.to_s}?"
|
23
|
-
# And try to call it
|
24
|
-
if self.respond_to?(method_name)
|
25
|
-
return self.send(method_name)
|
18
|
+
# If block is given
|
19
|
+
if block_given?
|
20
|
+
return yield(*args)
|
26
21
|
end
|
27
22
|
|
28
|
-
#
|
29
|
-
return
|
23
|
+
# Try to call condition method
|
24
|
+
return self.send(name, *args)
|
30
25
|
end
|
31
26
|
|
32
27
|
alias_method :v, :variant
|
data/lib/variants/version.rb
CHANGED
@@ -8,73 +8,108 @@ describe Variants do
|
|
8
8
|
|
9
9
|
before :each do
|
10
10
|
Rails.stub_chain(:env, :development?).and_return(true)
|
11
|
+
self.stub!(:params).and_return({})
|
11
12
|
end
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
# Basic behaviour examples
|
15
|
+
context 'basic behaviour' do
|
16
|
+
|
17
|
+
before :each do
|
18
|
+
self.stub!(:variant_name).and_return(true)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should respond to variant' do
|
22
|
+
self.should respond_to(:variant)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should respond to v' do
|
26
|
+
self.should respond_to(:v)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'v should be alias to variant' do
|
30
|
+
self.method(:v).should eq self.method(:variant)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should accept symbol' do
|
34
|
+
lambda {
|
35
|
+
self.variant :variant_name
|
36
|
+
}.should_not raise_error
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should accept string' do
|
40
|
+
lambda {
|
41
|
+
self.variant 'variant_name'
|
42
|
+
}.should_not raise_error
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should accept boolean in block' do
|
46
|
+
lambda {
|
47
|
+
self.variant(:variant_name) { true }
|
48
|
+
}.should_not raise_error
|
49
|
+
end
|
15
50
|
end
|
16
51
|
|
17
|
-
|
18
|
-
self.should respond_to(:v)
|
19
|
-
end
|
52
|
+
context 'calling condition method' do
|
20
53
|
|
21
|
-
|
22
|
-
|
23
|
-
|
54
|
+
it 'should raise error if variant name is not defined' do
|
55
|
+
lambda {
|
56
|
+
self.variant :variant_name
|
57
|
+
}.should raise_error
|
58
|
+
end
|
24
59
|
|
25
|
-
|
26
|
-
|
27
|
-
self.variant
|
28
|
-
|
29
|
-
end
|
60
|
+
it 'should call method_name for :method_name' do
|
61
|
+
self.stub!(:variant_name).and_return(true)
|
62
|
+
self.variant(:variant_name).should eql true
|
63
|
+
end
|
30
64
|
|
31
|
-
it 'should accept string' do
|
32
|
-
lambda {
|
33
|
-
self.variant 'variant_name'
|
34
|
-
}.should_not raise_error
|
35
65
|
end
|
36
66
|
|
37
|
-
|
38
|
-
lambda {
|
39
|
-
self.variant :variant_name, true
|
40
|
-
}.should_not raise_error
|
41
|
-
end
|
67
|
+
context 'passing condition in block' do
|
42
68
|
|
43
|
-
|
44
|
-
|
45
|
-
|
69
|
+
it 'should return condition value if variant is not overrided' do
|
70
|
+
self.variant(:variant_name) { true }.should eql true
|
71
|
+
self.variant(:variant_name) { false }.should eql false
|
72
|
+
end
|
46
73
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
74
|
+
it 'should return condition before calling method' do
|
75
|
+
self.stub!(:variant_name).and_return(true)
|
76
|
+
self.variant(:variant_name) { false }.should eql false
|
77
|
+
end
|
51
78
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
79
|
+
it 'should pass arguments to condition method' do
|
80
|
+
self.should_receive(:variant_name).with(42).and_return(true)
|
81
|
+
self.should_receive(:another_variant_name).with(42, 11).and_return(true)
|
82
|
+
self.variant(:variant_name, 42)
|
83
|
+
self.variant(:another_variant_name, 42, 11)
|
84
|
+
end
|
56
85
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
86
|
+
it 'should pass arguments to block' do
|
87
|
+
self.variant(:variant_name, 42, 11) { |a, b| a + b == 53 }.should be_true
|
88
|
+
end
|
61
89
|
|
62
|
-
it 'should look for params[:variant_name]' do
|
63
|
-
self.stub!(:params).and_return( { :variant_name => true } )
|
64
|
-
self.variant(:variant_name).should eql true
|
65
|
-
self.variant(:variant_name, false).should eql true
|
66
90
|
end
|
67
91
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
92
|
+
context 'looking for params' do
|
93
|
+
|
94
|
+
it 'should look for params[:variant_name]' do
|
95
|
+
self.stub!(:params).and_return( { 'variant_name' => true }.with_indifferent_access )
|
96
|
+
self.variant(:variant_name).should eql true
|
97
|
+
self.variant(:variant_name) { false }.should eql true
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'should convert params[:variant_name] to boolean' do
|
101
|
+
self.stub!(:params).and_return( { 'variant_name' => 'true' }.with_indifferent_access )
|
102
|
+
self.variant(:variant_name).should eql true
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'should look for params only if Rails.env.delevopment?' do
|
106
|
+
Rails.stub_chain(:env, :development?).and_return(false)
|
107
|
+
self.stub!(:params).and_return( { 'variant_name' => true } )
|
108
|
+
self.stub!(:variant_name).and_return(false)
|
109
|
+
self.variant(:variant_name).should eql false
|
110
|
+
self.variant(:variant_name) { false }.should eql false
|
111
|
+
end
|
72
112
|
|
73
|
-
it 'should look for params only if Rails.env.delevopment?' do
|
74
|
-
Rails.stub_chain(:env, :development?).and_return(false)
|
75
|
-
self.stub!(:params).and_return( { :variant_name => true } )
|
76
|
-
self.variant(:variant_name).should eql false
|
77
|
-
self.variant(:variant_name, false).should eql false
|
78
113
|
end
|
79
114
|
|
80
115
|
end
|
data/variants.gemspec
CHANGED
@@ -18,7 +18,4 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
-
|
22
|
-
s.add_dependency 'activesupport', '>= 3.0.7'
|
23
|
-
s.add_dependency 'actionpack', '>= 3.0.7'
|
24
21
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: variants
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Sasha Koss
|
@@ -16,39 +16,8 @@ bindir: bin
|
|
16
16
|
cert_chain: []
|
17
17
|
|
18
18
|
date: 2011-05-01 00:00:00 Z
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
name: activesupport
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 9
|
29
|
-
segments:
|
30
|
-
- 3
|
31
|
-
- 0
|
32
|
-
- 7
|
33
|
-
version: 3.0.7
|
34
|
-
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: actionpack
|
38
|
-
prerelease: false
|
39
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
-
none: false
|
41
|
-
requirements:
|
42
|
-
- - ">="
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
hash: 9
|
45
|
-
segments:
|
46
|
-
- 3
|
47
|
-
- 0
|
48
|
-
- 7
|
49
|
-
version: 3.0.7
|
50
|
-
type: :runtime
|
51
|
-
version_requirements: *id002
|
19
|
+
dependencies: []
|
20
|
+
|
52
21
|
description: Be boss of your views
|
53
22
|
email:
|
54
23
|
- kossnocorp@gmail.com
|
@@ -62,7 +31,9 @@ files:
|
|
62
31
|
- .autotest
|
63
32
|
- .gitignore
|
64
33
|
- .rspec
|
34
|
+
- .travis.yml
|
65
35
|
- Gemfile
|
36
|
+
- README.md
|
66
37
|
- Rakefile
|
67
38
|
- lib/variants.rb
|
68
39
|
- lib/variants/helpers.rb
|