feature_definitions 0.2.0 → 0.3.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,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - "2.0.0"
7
+ - jruby-18mode # JRuby in 1.8 mode
8
+ - jruby-19mode # JRuby in 1.9 mode
9
+ - rbx-18mode
10
+ - rbx-19mode
data/Gemfile CHANGED
@@ -2,13 +2,16 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
- gem 'lazy_const'
5
+ gem 'lazy_const', '~> 0.1.3'
6
6
 
7
7
  # Add dependencies to develop your gem here.
8
8
  # Include everything needed to run rake, tests, features, etc.
9
9
  group :development do
10
10
  gem "minitest", ">= 0"
11
- gem "rdoc", "~> 3.12"
12
11
  gem "bundler"
13
- gem "jeweler", "~> 1.8.4"
12
+ gem 'rake'
13
+ unless ENV['TRAVIS']
14
+ gem "rdoc", "~> 3.12"
15
+ gem "jeweler", "~> 1.8.4"
16
+ end
14
17
  end
data/README.rdoc CHANGED
@@ -1,25 +1,55 @@
1
1
  = feature_definitions
2
2
 
3
+ {<img src="https://travis-ci.org/rmg/feature_definitions.png?branch=master" alt="Build Status" />}[https://travis-ci.org/rmg/feature_definitions]
4
+
3
5
  A minimal useful feature toggle mechanism.
4
6
 
7
+ Thanks to Travis-CI, tested on:
8
+ - "1.8.7"
9
+ - "1.9.2"
10
+ - "1.9.3"
11
+ - "2.0.0"
12
+ - jruby-18mode # JRuby in 1.8 mode
13
+ - jruby-19mode # JRuby in 1.9 mode
14
+ - rbx-18mode
15
+ - rbx-19mode
16
+
5
17
  == Installation
6
18
 
7
19
  gem 'feature_definitions'
8
20
 
9
21
  == Example
10
22
 
23
+ === Using constructor
24
+ lib/features.rb:
25
+ class Features < FeatureDefinitions
26
+ AWESOME = self.new {|flags| flags.is_awesome? }
27
+ end
28
+
29
+ app/main.rb:
30
+ Features.context = args_to_flags
31
+
32
+ app/elsewhere.rb
33
+ def some_method
34
+ Features.AWESOME.enabled? do
35
+ # do feature
36
+ end
37
+ # else noop
38
+ end
39
+
40
+ === Rails (and define_feature helper)
11
41
  Rails is *NOT* required, but this is how you would use this gem with Rails.
12
42
 
13
43
  app/model/features.rb:
14
44
  class Features < FeatureDefinitions
15
- define_feature :AWESOME, using: [:current_user] do |user|
45
+ define_feature :AWESOME do |user|
16
46
  user.is_awesome?
17
47
  end
18
48
  end
19
49
 
20
50
  app/controller/application_controller.rb:
21
51
  class ApplicationController < ActionController::Base
22
- before_filter { |controller| Features.context = controller }
52
+ before_filter { |controller| Features.context = controller.current_user }
23
53
  end
24
54
 
25
55
  app/views/some/view.erb.html:
data/Rakefile CHANGED
@@ -11,19 +11,31 @@ rescue Bundler::BundlerError => e
11
11
  end
12
12
  require 'rake'
13
13
 
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
- gem.name = "feature_definitions"
18
- gem.homepage = "http://github.com/rmg/feature_definitions"
19
- gem.license = "MIT"
20
- gem.summary = %Q{Simple and flexible feature definition}
21
- gem.description = %Q{Simple and flexible feature toggle defintion}
22
- gem.email = "r.m.graham@gmail.com"
23
- gem.authors = ["Ryan Graham"]
24
- # dependencies defined in Gemfile
14
+ unless ENV['TRAVIS']
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "feature_definitions"
19
+ gem.homepage = "http://github.com/rmg/feature_definitions"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Simple and flexible feature definition}
22
+ gem.description = %Q{Simple and flexible feature toggle defintion}
23
+ gem.email = "r.m.graham@gmail.com"
24
+ gem.authors = ["Ryan Graham"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rdoc/task'
30
+ Rake::RDocTask.new do |rdoc|
31
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
32
+
33
+ rdoc.rdoc_dir = 'rdoc'
34
+ rdoc.title = "feature_definitions #{version}"
35
+ rdoc.rdoc_files.include('README*')
36
+ rdoc.rdoc_files.include('lib/**/*.rb')
37
+ end
25
38
  end
26
- Jeweler::RubygemsDotOrgTasks.new
27
39
 
28
40
  require 'rake/testtask'
29
41
  Rake::TestTask.new(:test) do |test|
@@ -34,12 +46,3 @@ end
34
46
 
35
47
  task :default => :test
36
48
 
37
- require 'rdoc/task'
38
- Rake::RDocTask.new do |rdoc|
39
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
-
41
- rdoc.rdoc_dir = 'rdoc'
42
- rdoc.title = "feature_definitions #{version}"
43
- rdoc.rdoc_files.include('README*')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
45
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "feature_definitions"
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Graham"]
@@ -18,12 +18,14 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ ".travis.yml",
21
22
  "Gemfile",
22
23
  "LICENSE.txt",
23
24
  "README.rdoc",
24
25
  "Rakefile",
25
26
  "VERSION",
26
27
  "feature_definitions.gemspec",
28
+ "lazy_const.gemspec",
27
29
  "lib/feature_definitions.rb",
28
30
  "test/helper.rb",
29
31
  "test/test_feature_definitions.rb"
@@ -38,23 +40,26 @@ Gem::Specification.new do |s|
38
40
  s.specification_version = 3
39
41
 
40
42
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
- s.add_runtime_dependency(%q<lazy_const>, [">= 0"])
43
+ s.add_runtime_dependency(%q<lazy_const>, ["~> 0.1.3"])
42
44
  s.add_development_dependency(%q<minitest>, [">= 0"])
43
- s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
44
45
  s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<rake>, [">= 0"])
47
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
45
48
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
46
49
  else
47
- s.add_dependency(%q<lazy_const>, [">= 0"])
50
+ s.add_dependency(%q<lazy_const>, ["~> 0.1.3"])
48
51
  s.add_dependency(%q<minitest>, [">= 0"])
49
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
50
52
  s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<rake>, [">= 0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
51
55
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
52
56
  end
53
57
  else
54
- s.add_dependency(%q<lazy_const>, [">= 0"])
58
+ s.add_dependency(%q<lazy_const>, ["~> 0.1.3"])
55
59
  s.add_dependency(%q<minitest>, [">= 0"])
56
- s.add_dependency(%q<rdoc>, ["~> 3.12"])
57
60
  s.add_dependency(%q<bundler>, [">= 0"])
61
+ s.add_dependency(%q<rake>, [">= 0"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
58
63
  s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
59
64
  end
60
65
  end
@@ -0,0 +1,66 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "lazy_const"
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Ryan Graham"]
12
+ s.date = "2013-03-19"
13
+ s.description = "Allows you to define lazy evaluated, constant-like instances of your class."
14
+ s.email = "r.m.graham@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".travis.yml",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "feature_definitions.gemspec",
28
+ "lazy_const.gemspec",
29
+ "lib/feature_definitions.rb",
30
+ "test/helper.rb",
31
+ "test/test_feature_definitions.rb"
32
+ ]
33
+ s.homepage = "http://github.com/rmg/lazy_const"
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.23"
37
+ s.summary = "A little Ruby gem for defining lazy evaluated constants."
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<lazy_const>, ["~> 0.1.3"])
44
+ s.add_development_dependency(%q<minitest>, [">= 0"])
45
+ s.add_development_dependency(%q<bundler>, [">= 0"])
46
+ s.add_development_dependency(%q<rake>, [">= 0"])
47
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
48
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
49
+ else
50
+ s.add_dependency(%q<lazy_const>, ["~> 0.1.3"])
51
+ s.add_dependency(%q<minitest>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<rake>, [">= 0"])
54
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<lazy_const>, ["~> 0.1.3"])
59
+ s.add_dependency(%q<minitest>, [">= 0"])
60
+ s.add_dependency(%q<bundler>, [">= 0"])
61
+ s.add_dependency(%q<rake>, [">= 0"])
62
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
64
+ end
65
+ end
66
+
@@ -2,17 +2,24 @@ require 'lazy_const'
2
2
 
3
3
  class FeatureDefinitions
4
4
  extend LazyConst
5
- attr_reader :opts, :test_proc
5
+ attr_reader :test_proc
6
6
 
7
- def self.define_feature(name, opts = {}, &block)
8
- lazy_const(name){ new(opts, block.to_proc) }
7
+ def self.define_feature(name, &block)
8
+ lazy_const(name) { new(&block) }
9
9
  end
10
10
 
11
- def initialize(opts, proc)
12
- @opts = opts
13
- @test_proc = proc
11
+ PASSTHROUGH = Proc.new { |arg| arg }
12
+
13
+ def initialize(&block)
14
+ if block_given?
15
+ @test_proc = block.to_proc
16
+ else
17
+ @test_proc = PASSTHROUGH
18
+ end
14
19
  end
15
20
 
21
+ @@context = nil
22
+
16
23
  def self.context=(context)
17
24
  @@context = context
18
25
  end
@@ -22,9 +29,8 @@ class FeatureDefinitions
22
29
  end
23
30
 
24
31
  def enabled?(&block)
25
- args = opts[:using].map { |sym| context.public_send(sym) }
26
- test_proc.call(*args).tap do |verdict|
27
- yield if block_given? and verdict
32
+ test_proc.call(context).tap do |verdict|
33
+ yield if verdict and block_given?
28
34
  end
29
35
  end
30
36
  end
@@ -1,30 +1,87 @@
1
1
  require 'helper'
2
2
 
3
- class Features < FeatureDefinitions
4
- define_feature :AWESOME, using: [:is_awesome?] do |awesome|
5
- awesome
6
- end
3
+ class ConstFeatures < FeatureDefinitions
4
+ ALWAYS_ENABLED_FEATURE = self.new { |context| true }
5
+ ALWAYS_DISABLED_FEATURE = self.new { |context| false }
7
6
  end
8
7
 
9
8
  class TestFeatureDefinitions < MiniTest::Unit::TestCase
9
+ def test_feature_definition_helper
10
+ feature_class = Class.new(FeatureDefinitions) do
11
+ define_feature :FEATURE_NAME do |context|
12
+ true
13
+ end
14
+ end
15
+ assert feature_class.respond_to? :FEATURE_NAME
16
+ assert feature_class.FEATURE_NAME.enabled?
17
+ end
18
+ def test_feature_definition_constructor
19
+ feature_class = Class.new(FeatureDefinitions)
20
+ feature = feature_class.new { |context| true }
21
+ assert feature.enabled?
22
+ end
23
+ def test_constant_feature_definitions
24
+ assert ConstFeatures::ALWAYS_ENABLED_FEATURE.enabled?
25
+ refute ConstFeatures::ALWAYS_DISABLED_FEATURE.enabled?
26
+ end
27
+ end
28
+
29
+ class TestFeatureDefinitionsUsage < MiniTest::Unit::TestCase
30
+ def setup
31
+ @feature_class = Class.new(FeatureDefinitions) do
32
+ define_feature :AWESOME do |context|
33
+ context.is_awesome?
34
+ end
35
+ end
36
+ end
37
+ def test_feature_enabled
38
+ @feature_class.context = OpenStruct.new(:is_awesome? => true)
39
+ assert @feature_class.AWESOME.enabled?
40
+ end
41
+ def test_feature_disabled
42
+ @feature_class.context = OpenStruct.new(:is_awesome? => false)
43
+ refute @feature_class.AWESOME.enabled?
44
+ end
45
+ def test_feature_toggle
46
+ @feature_class.context = OpenStruct.new(:is_awesome? => true)
47
+ assert @feature_class.AWESOME.enabled?
48
+ @feature_class.context = OpenStruct.new(:is_awesome? => false)
49
+ refute @feature_class.AWESOME.enabled?
50
+ end
51
+ def test_feature_toggle_with_block
52
+ @feature_class.context = OpenStruct.new(:is_awesome? => true)
53
+ called = false
54
+ @feature_class.AWESOME.enabled? do
55
+ called = true
56
+ end
57
+ assert called
58
+ end
59
+ end
60
+
61
+ class TestFeatureDefaultBlock < MiniTest::Unit::TestCase
62
+ def setup
63
+ @feature_class = Class.new(FeatureDefinitions) do
64
+ define_feature :AWESOME
65
+ end
66
+ end
10
67
  def test_feature_enabled
11
- Features.context = OpenStruct.new(is_awesome?: true)
12
- assert Features.AWESOME.enabled?
68
+ @feature_class.context = true
69
+ assert @feature_class.AWESOME.enabled?
13
70
  end
14
71
  def test_feature_disabled
15
- Features.context = OpenStruct.new(is_awesome?: false)
16
- refute Features.AWESOME.enabled?
72
+ @feature_class.context = false
73
+ refute @feature_class.AWESOME.enabled?
17
74
  end
18
75
  def test_feature_toggle
19
- Features.context = OpenStruct.new(is_awesome?: true)
20
- assert Features.AWESOME.enabled?
21
- Features.context = OpenStruct.new(is_awesome?: false)
22
- refute Features.AWESOME.enabled?
76
+ @feature_class.context = true
77
+ assert @feature_class.AWESOME.enabled?
78
+ @feature_class.context = false
79
+ refute @feature_class.AWESOME.enabled?
23
80
  end
24
81
  def test_feature_toggle_with_block
25
- Features.context = OpenStruct.new(is_awesome?: true)
82
+ @feature_class.context = true
26
83
  called = false
27
- Features.AWESOME.enabled? do
84
+ @feature_class.AWESOME.enabled? do
28
85
  called = true
29
86
  end
30
87
  assert called
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: feature_definitions
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Graham
@@ -15,17 +15,17 @@ dependencies:
15
15
  version_requirements: !ruby/object:Gem::Requirement
16
16
  none: false
17
17
  requirements:
18
- - - ! '>='
18
+ - - ~>
19
19
  - !ruby/object:Gem::Version
20
- version: '0'
20
+ version: 0.1.3
21
21
  type: :runtime
22
22
  name: lazy_const
23
23
  requirement: !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
- - - ! '>='
26
+ - - ~>
27
27
  - !ruby/object:Gem::Version
28
- version: '0'
28
+ version: 0.1.3
29
29
  prerelease: false
30
30
  - !ruby/object:Gem::Dependency
31
31
  version_requirements: !ruby/object:Gem::Requirement
@@ -47,17 +47,17 @@ dependencies:
47
47
  version_requirements: !ruby/object:Gem::Requirement
48
48
  none: false
49
49
  requirements:
50
- - - ~>
50
+ - - ! '>='
51
51
  - !ruby/object:Gem::Version
52
- version: '3.12'
52
+ version: '0'
53
53
  type: :development
54
- name: rdoc
54
+ name: bundler
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  none: false
57
57
  requirements:
58
- - - ~>
58
+ - - ! '>='
59
59
  - !ruby/object:Gem::Version
60
- version: '3.12'
60
+ version: '0'
61
61
  prerelease: false
62
62
  - !ruby/object:Gem::Dependency
63
63
  version_requirements: !ruby/object:Gem::Requirement
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  type: :development
70
- name: bundler
70
+ name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ~>
83
+ - !ruby/object:Gem::Version
84
+ version: '3.12'
85
+ type: :development
86
+ name: rdoc
87
+ requirement: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ version: '3.12'
93
+ prerelease: false
78
94
  - !ruby/object:Gem::Dependency
79
95
  version_requirements: !ruby/object:Gem::Requirement
80
96
  none: false
@@ -100,12 +116,14 @@ extra_rdoc_files:
100
116
  - README.rdoc
101
117
  files:
102
118
  - .document
119
+ - .travis.yml
103
120
  - Gemfile
104
121
  - LICENSE.txt
105
122
  - README.rdoc
106
123
  - Rakefile
107
124
  - VERSION
108
125
  - feature_definitions.gemspec
126
+ - lazy_const.gemspec
109
127
  - lib/feature_definitions.rb
110
128
  - test/helper.rb
111
129
  - test/test_feature_definitions.rb
@@ -123,7 +141,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
141
  - !ruby/object:Gem::Version
124
142
  segments:
125
143
  - 0
126
- hash: -3803903361324450403
144
+ hash: -4330562717910034088
127
145
  version: '0'
128
146
  required_rubygems_version: !ruby/object:Gem::Requirement
129
147
  none: false