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 ADDED
@@ -0,0 +1 @@
1
+ script: 'bundle exec rspec spec'
data/Gemfile CHANGED
@@ -3,5 +3,7 @@ source "http://rubygems.org"
3
3
  # Specify your gem's dependencies in variants.gemspec
4
4
  gemspec
5
5
 
6
+ gem 'rails', '~> 2.3.0'
7
+
6
8
  gem 'rspec'
7
9
  gem 'ZenTest'
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.
@@ -1,32 +1,27 @@
1
1
  module Variants
2
2
  module Helpers
3
- extend ActiveSupport::Concern
3
+ def self.included base
4
+ base.send(:include, InstanceMethods)
5
+ end
4
6
 
5
7
  module InstanceMethods
6
- def variant name, condition = nil
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
- if self.respond_to?(:params) and not self.params[name.to_sym].nil?
12
- return self.params[name.to_sym].to_s == 'true'
13
+ unless self.params[name].nil?
14
+ return self.params[name].to_s == 'true'
13
15
  end
14
16
  end
15
17
 
16
- # If condition is presented, return it
17
- unless condition.nil?
18
- return condition
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
- # Else return false
29
- return false
23
+ # Try to call condition method
24
+ return self.send(name, *args)
30
25
  end
31
26
 
32
27
  alias_method :v, :variant
@@ -1,3 +1,3 @@
1
1
  module Variants
2
- VERSION = "0.1.0"
2
+ VERSION = '0.2.0'
3
3
  end
@@ -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
- it 'should respond to variant' do
14
- self.should respond_to(:variant)
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
- it 'should respond to v' do
18
- self.should respond_to(:v)
19
- end
52
+ context 'calling condition method' do
20
53
 
21
- it 'v should be alias to variant' do
22
- self.method(:v).should eq self.method(:variant)
23
- end
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
- it 'should accept symbol' do
26
- lambda {
27
- self.variant :variant_name
28
- }.should_not raise_error
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
- it 'should accept boolean' do
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
- it 'should return false if variant and condition is not presented' do
44
- self.variant(:variant_name).should eql false
45
- end
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
- it 'should return condition value if variant is not overrided' do
48
- self.variant(:variant_name, true).should eql true
49
- self.variant(:variant_name, false).should eql false
50
- end
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
- it 'should call method_name? for :method_name' do
53
- self.stub!(:variant_name?).and_return(true)
54
- self.variant(:variant_name).should eql true
55
- end
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
- it 'should return condition before calling method' do
58
- self.stub!(:variant_name?).and_return(true)
59
- self.variant(:variant_name, false).should eql false
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
- it 'should convert params[:variant_name] to boolean' do
69
- self.stub!(:params).and_return( { :variant_name => 'true' } )
70
- self.variant(:variant_name).should eql true
71
- end
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: 27
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.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
- - !ruby/object:Gem::Dependency
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