Module: Schaltroom

Defined in:
lib/schaltroom.rb,
lib/schaltroom/config.rb,
lib/schaltroom/feature.rb,
lib/schaltroom/version.rb,
lib/schaltroom/repository.rb

Overview

Feature Toggle library

Defined Under Namespace

Modules: VERSION Classes: Config, Feature, Repository

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#callsObject

Returns the value of attribute calls.



11
12
13
# File 'lib/schaltroom.rb', line 11

def calls
  @calls
end

Class Method Details

.[](item) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
# File 'lib/schaltroom.rb', line 29

def self.[](item)
  handle_caller(caller, __method__, item)
  feature = Schaltroom.find_feature(item)
  raise ArgumentError, "Feature: #{item} is unknown. Please add it to your Schaltroom.config" if feature.nil?
  if feature[:type] == :boolean
    return feature[:value]
  else
    Feature.new(feature)
  end
end

.active?(name) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
# File 'lib/schaltroom.rb', line 73

def self.active?(name)
  handle_caller(caller, __method__, name)
  feature = find_feature(name)
  return false unless feature
  feature[:value]
end

.active_before?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
# File 'lib/schaltroom.rb', line 95

def self.active_before?(name, version)
  handle_caller(caller, __method__, name)
  Gem::Version.new(version) > Gem::Version.new(find_feature(name)[:value])
end

.active_between?(name, first_version, last_version) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/schaltroom.rb', line 105

def self.active_between?(name, first_version, last_version)
  handle_caller(caller, __method__, name)
  Gem::Version.new(first_version) <= Gem::Version.new(find_feature(name)[:value]) && Gem::Version.new(last_version) >= Gem::Version.new(find_feature(name)[:value])
end

.active_since?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/schaltroom.rb', line 85

def self.active_since?(name, version)
  handle_caller(caller, __method__, name)
  Gem::Version.new(version) <= Gem::Version.new(find_feature(name)[:value])
end

.add_feature(name, type, value) ⇒ Object



17
18
19
# File 'lib/schaltroom.rb', line 17

def self.add_feature(name, type, value)
 @repository.add_feature(name, type, value)
end

.add_gem(gem) ⇒ Object



54
55
56
# File 'lib/schaltroom.rb', line 54

def self.add_gem(gem)
  @repository.add_feature(gem.to_s.to_sym, :version, gem::VERSION::STRING)
end

.configObject



21
22
23
24
25
26
27
# File 'lib/schaltroom.rb', line 21

def self.config
  if block_given?
    yield @config
  else
    @config
  end
end

.delete_feature(name) ⇒ Object



64
65
66
67
# File 'lib/schaltroom.rb', line 64

def self.delete_feature(name)
  sym = name.is_a?(Symbol) ? name : name.to_s.to_sym
  self.repository.delete(sym)
end

.featuresObject



69
70
71
# File 'lib/schaltroom.rb', line 69

def self.features
  self.repository if block_given?
end

.find_feature(name) ⇒ Object



58
59
60
61
62
# File 'lib/schaltroom.rb', line 58

def self.find_feature(name)
  return self.repository[name] if name.is_a?(Symbol)

  self.repository[name.to_s.to_sym]
end

.gem_versionObject



4
5
6
# File 'lib/schaltroom/version.rb', line 4

def self.gem_version
  Gem::Version.new VERSION::STRING
end

.handle_caller(caller_stack, method, feature_name) ⇒ Object



110
111
112
113
114
115
# File 'lib/schaltroom.rb', line 110

def self.handle_caller(caller_stack, method, feature_name)
  return unless @config.monitoring == :enabled

  @calls << { feature_name: feature_name, method: method.to_sym, caller: caller_stack[0] }
  @calls = @calls.uniq
end

.inactive?(name) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/schaltroom.rb', line 80

def self.inactive?(name)
  handle_caller(caller, __method__, name)
  !active?(name)
end

.inactive_before?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/schaltroom.rb', line 100

def self.inactive_before?(name, version)
  handle_caller(caller, __method__, name)
  Gem::Version.new(version) <= Gem::Version.new(find_feature(name)[:value])
end

.inactive_since?(name, version) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/schaltroom.rb', line 90

def self.inactive_since?(name, version)
  handle_caller(caller, __method__, name)
  Gem::Version.new(version) > Gem::Version.new(find_feature(name)[:value])
end

.repositoryObject



40
41
42
43
44
45
46
# File 'lib/schaltroom.rb', line 40

def self.repository
  if block_given?
    yield @repository
  else
    @repository
  end
end

.resetObject



48
49
50
51
52
# File 'lib/schaltroom.rb', line 48

def self.reset
  @config = Config.new
  @repository = Repository.new
  @calls = []
end

.with_config_and_repository(config, repository) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/schaltroom.rb', line 117

def self.with_config_and_repository(config, repository)
  return unless block_given?
  current_config = @config
  current_repository = @repository
  @config = config
  @repository = @repository.merge(repository)
  yield
  @config = current_config
  @repository = current_repository
end