Skip to content
Browse files

Update examples to use new dsl

  • Loading branch information...
1 parent 44943da commit de6dbaa14d171747be81db9f37e1e7c970619c73 @jnunemaker committed
Showing with 42 additions and 37 deletions.
  1. +8 −3 examples/basic.rb
  2. +6 −5 examples/group.rb
  3. +9 −10 examples/individual_actor.rb
  4. +10 −11 examples/percentage_of_actors.rb
  5. +9 −8 examples/percentage_of_random.rb
View
11 examples/basic.rb
@@ -3,10 +3,17 @@
require 'flipper'
require 'flipper/adapters/memory'
+# pick an adapter
adapter = Flipper::Adapters::Memory.new
-search = Flipper::Feature.new(:search, adapter)
+
+# get a handy dsl instance
+flipper = Flipper.new(adapter)
+
+# grab a feature
+search = flipper[:search]
perform = lambda do
+ # check if that feature is enabled
if search.enabled?
puts 'Search away!'
else
@@ -15,8 +22,6 @@
end
perform.call
-
puts 'Enabling Search...'
search.enable
-
perform.call
View
11 examples/group.rb
@@ -4,14 +4,15 @@
require 'flipper/adapters/memory'
adapter = Flipper::Adapters::Memory.new
-stats = Flipper::Feature.new(:stats, adapter)
+flipper = Flipper.new(adapter)
+stats = flipper[:stats]
-# Define group
-admins = Flipper.register(:admins) do |actor|
+# Register group
+Flipper.register(:admins) do |actor|
actor.respond_to?(:admin?) && actor.admin?
end
-# Some class that represents what will be trying to do something
+# Some class that represents actor that will be trying to do something
class User
def initialize(admin)
@admin = admin
@@ -29,7 +30,7 @@ def admin?
puts "Stats for non_admin: #{stats.enabled?(non_admin)}"
puts "\nEnabling Stats for admins...\n\n"
-stats.enable(admins)
+stats.enable(flipper.group(:admins))
puts "Stats for admin: #{stats.enabled?(admin)}"
puts "Stats for non_admin: #{stats.enabled?(non_admin)}"
View
19 examples/individual_actor.rb
@@ -4,27 +4,26 @@
require 'flipper/adapters/memory'
adapter = Flipper::Adapters::Memory.new
-stats = Flipper::Feature.new(:stats, adapter)
+flipper = Flipper.new(adapter)
+stats = flipper[:stats]
# Some class that represents what will be trying to do something
class User
+ attr_reader :id
+
def initialize(id)
@id = id
end
-
- def to_flipper_actor
- @to_flipper_actor ||= Flipper::Types::Actor.new(@id)
- end
end
user1 = User.new(1)
user2 = User.new(2)
-puts "Stats for user1: #{stats.enabled?(user1.to_flipper_actor)}"
-puts "Stats for user2: #{stats.enabled?(user2.to_flipper_actor)}"
+puts "Stats for user1: #{stats.enabled?(flipper.actor(user1))}"
+puts "Stats for user2: #{stats.enabled?(flipper.actor(user2))}"
puts "\nEnabling stats for user1...\n\n"
-stats.enable(user1.to_flipper_actor)
+stats.enable(flipper.actor(user1))
-puts "Stats for user1: #{stats.enabled?(user1.to_flipper_actor)}"
-puts "Stats for user2: #{stats.enabled?(user2.to_flipper_actor)}"
+puts "Stats for user1: #{stats.enabled?(flipper.actor(user1))}"
+puts "Stats for user2: #{stats.enabled?(flipper.actor(user2))}"
View
21 examples/percentage_of_actors.rb
@@ -4,33 +4,32 @@
require 'flipper/adapters/memory'
adapter = Flipper::Adapters::Memory.new
-stats = Flipper::Feature.new(:stats, adapter)
+flipper = Flipper.new(adapter)
+stats = flipper[:stats]
# Some class that represents what will be trying to do something
class User
+ attr_reader :id
+
def initialize(id)
@id = id
end
-
- def to_flipper_actor
- @to_flipper_actor ||= Flipper::Types::Actor.new(@id)
- end
end
pitt = User.new(1)
clooney = User.new(10)
-puts "Stats for pitt: #{stats.enabled?(pitt.to_flipper_actor)}"
-puts "Stats for clooney: #{stats.enabled?(clooney.to_flipper_actor)}"
+puts "Stats for pitt: #{stats.enabled?(flipper.actor(pitt))}"
+puts "Stats for clooney: #{stats.enabled?(flipper.actor(clooney))}"
puts "\nEnabling stats for 5 percent...\n\n"
stats.enable(Flipper::Types::PercentageOfActors.new(5))
-puts "Stats for pitt: #{stats.enabled?(pitt.to_flipper_actor)}"
-puts "Stats for clooney: #{stats.enabled?(clooney.to_flipper_actor)}"
+puts "Stats for pitt: #{stats.enabled?(flipper.actor(pitt))}"
+puts "Stats for clooney: #{stats.enabled?(flipper.actor(clooney))}"
puts "\nEnabling stats for 15 percent...\n\n"
stats.enable(Flipper::Types::PercentageOfActors.new(15))
-puts "Stats for pitt: #{stats.enabled?(pitt.to_flipper_actor)}"
-puts "Stats for clooney: #{stats.enabled?(clooney.to_flipper_actor)}"
+puts "Stats for pitt: #{stats.enabled?(flipper.actor(pitt))}"
+puts "Stats for clooney: #{stats.enabled?(flipper.actor(clooney))}"
View
17 examples/percentage_of_random.rb
@@ -3,11 +3,12 @@
require 'flipper'
require 'flipper/adapters/memory'
-def perform_test(number)
- adapter = Flipper::Adapters::Memory.new
- logging = Flipper::Feature.new(:logging, adapter)
- percentage = Flipper::Types::PercentageOfRandom.new(number)
- logging.enable(percentage)
+adapter = Flipper::Adapters::Memory.new
+flipper = Flipper.new(adapter)
+logging = flipper[:logging]
+
+perform_test = lambda do |number|
+ logging.enable flipper.random(number)
total = 1_000
enabled = []
@@ -21,12 +22,12 @@ def perform_test(number)
end
end
- actual = ((enabled.size / total.to_f) * 100).round
+ actual = (enabled.size / total.to_f * 100).round(2)
# puts "#{enabled.size} / #{total}"
- puts "percentage: #{actual} vs #{percentage.value}"
+ puts "percentage: #{actual} vs #{number}"
end
[1, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99, 100].each do |number|
- perform_test(number)
+ perform_test.call number
end

0 comments on commit de6dbaa

Please sign in to comment.
Something went wrong with that request. Please try again.