-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from lanej/deprecate-path-lookups
Deprecate constant lookups via .const_missing
- Loading branch information
Showing
11 changed files
with
142 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
|
||
group :test do | ||
gem "pry" | ||
gem "pry-nav" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
id: | ||
lt: | ||
5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class Feature::ConstantLookup | ||
Error = Class.new(NameError) do | ||
attr_reader :sym | ||
|
||
def initialize(sym) | ||
@sym = sym | ||
super(sym.join('::')) | ||
end | ||
end | ||
|
||
|
||
# Return a tree walker that translates Module#const_missing(sym) into the next child node | ||
# | ||
# So Features::Cat::BearDog walks as: | ||
# * next_features = Feature.features # root | ||
# * const_missing(:Cat) => next_features = next_features['cat'] | ||
# * const_missing(:BearDog) => next_features['bear_dog'] | ||
# | ||
# Defined at Toggles.features_dir + "/cat/bear_dog.yaml" | ||
# | ||
# @raise [Error] if constant cannot be resolved | ||
def self.from(features, path) | ||
Class.new { | ||
class << self | ||
attr_accessor :features | ||
attr_accessor :path | ||
|
||
def const_missing(sym) | ||
subtree_or_feature = features.fetch( | ||
# translate class name into path part i.e :BearDog #=> 'bear_dog' | ||
sym.to_s.gsub(/([a-z])([A-Z])/) { |s| s.chars.join('_') }.downcase.to_sym, | ||
) | ||
if subtree_or_feature.is_a?(Hash) | ||
Feature::ConstantLookup.from(subtree_or_feature, path + [sym]) | ||
else | ||
subtree_or_feature | ||
end | ||
rescue KeyError | ||
raise Error.new(path + [sym]) | ||
end | ||
end | ||
}.tap do |resolver| | ||
resolver.features = features | ||
resolver.path = path | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module Toggles | ||
VERSION = '0.3.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
|
||
require "toggles" | ||
|
||
Bundler.require(:test) | ||
|
||
RSpec.configure do |config| | ||
config.order = "random" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
describe "Feature::Type" do | ||
specify 'deprecated' do | ||
aggregate_failures do | ||
expect(Feature::Type.enabled_for?(user_id: 1)).to eq true | ||
expect(Feature::Type.enabled_for?(user_id: 25)).to eq false | ||
expect(Feature::Type.enabled_for?(user_id: nil)).to eq false | ||
end | ||
end | ||
|
||
specify do | ||
expect(Feature::Type.enabled_for?(user_id: 1)).to eq true | ||
expect(Feature::Type.enabled_for?(user_id: 25)).to eq false | ||
expect(Feature::Type.enabled_for?(user_id: nil)).to eq false | ||
aggregate_failures do | ||
expect(Feature.enabled?(:type, user_id: 1)).to eq(true) | ||
expect(Feature.disabled?(:type, user_id: 1)).to eq(false) | ||
expect(Feature.enabled?(:type, user_id: 25)).to eq(false) | ||
expect(Feature.enabled?(:nested_foo, :bar_baz, id: 25)).to eq(false) | ||
expect(Feature.enabled?(:nested_foo, :bar_baz, id: 1)).to eq(true) | ||
expect(Feature.disabled?(:type, user_id: 25)).to eq(true) | ||
expect(Feature.disabled?(:nested_foo, :bar_baz, id: 25)).to eq(true) | ||
expect(Feature.disabled?(:nested_foo, :bar_baz, id: 1)).to eq(false) | ||
expect { Feature.disabled?(:nested_foo, :bar_boz, id: 1) }.to raise_error(Feature::Unknown) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
lib = File.expand_path('lib', __dir__) | ||
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) | ||
require 'toggles/version' | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "toggles" | ||
s.version = "0.1.2" | ||
s.authors = ["Andrew Tribone", "James Brown"] | ||
s.summary = "YAML backed feature toggles" | ||
s.email = "[email protected]" | ||
s.homepage = "https://github.com/EasyPost/toggles" | ||
s.license = "ISC" | ||
s.name = 'toggles' | ||
s.version = Toggles::VERSION | ||
s.authors = ['Andrew Tribone', 'James Brown', 'Josh Lane'] | ||
s.summary = 'YAML backed feature toggles' | ||
s.email = '[email protected]' | ||
s.homepage = 'https://github.com/EasyPost/toggles' | ||
s.license = 'ISC' | ||
s.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) | ||
s.test_files = s.files.grep(/^(spec)\//) | ||
s.test_files = s.files.grep(%r{^(spec)/}) | ||
s.description = <<-EOF | ||
YAML-backed implementation of the feature flags pattern. Build a | ||
hierarchy of features in YAML files in the filesystem, apply various | ||
conditions using boolean logic and a selection of filters, and easily | ||
check whether a given feature should be applied. | ||
EOF | ||
|
||
s.add_development_dependency "bundler" | ||
s.add_development_dependency "pry" | ||
s.add_development_dependency "pry-nav" | ||
s.add_development_dependency "pry-remote" | ||
s.add_development_dependency "rake" | ||
s.add_development_dependency "rspec" | ||
s.add_development_dependency "rspec-its" | ||
s.add_development_dependency "rspec-temp_dir" | ||
s.add_development_dependency 'bundler' | ||
s.add_development_dependency 'rake' | ||
s.add_development_dependency 'rspec' | ||
s.add_development_dependency 'rspec-its' | ||
s.add_development_dependency 'rspec-temp_dir' | ||
end |