class Shamu::Features::Toggle
A configured feature toggle.
Constants
- ParsingState
- TOGGLE_KEYS
- TYPES
Public Class Methods
new( attributes )
click to toggle source
Calls superclass method
Shamu::Attributes::new
# File lib/shamu/features/toggle.rb, line 79 def initialize( attributes ) fail ArgumentError, "Must provide a retire_at attribute for '#{ attributes[ 'name' ] }' toggle." unless attributes["retire_at"] # rubocop:disable Metrics/LineLength fail ArgumentError, "Type must be one of #{ TYPES } for '#{ attributes[ 'name' ] }' toggle." unless TYPES.include?( attributes["type"] ) # rubocop:disable Metrics/LineLength super end
Private Class Methods
load( path )
click to toggle source
Loads all the toggles from the YAML file at the given path.
@param [String] path. @return [Hash<String,Toggle>] of toggles by name.
# File lib/shamu/features/toggle.rb, line 97 def load( path ) toggles = {} load_from_path( path, toggles, ParsingState.new( nil, nil ) ) toggles end
load_from_path( path, toggles, state )
click to toggle source
# File lib/shamu/features/toggle.rb, line 106 def load_from_path( path, toggles, state ) path = File.expand_path( path, state.file_path ) File.open( path, "r" ) do |file| yaml = YAML.load( file.read ) # rubocop:disable Security/YAMLLoad parse_node( yaml, toggles, ParsingState.new( state.name, File.dirname( path ) ) ) end end
parse_child_nodes( node, toggles, state )
click to toggle source
# File lib/shamu/features/toggle.rb, line 123 def parse_child_nodes( node, toggles, state ) node.each do |key, child| if key == "import" load_from_path( child, toggles, state ) else child_state = ParsingState.new( [ state.name, key ].compact.join( "/" ), state.file_path ) parse_node( child, toggles, child_state ) end end end
parse_node( node, toggles, state )
click to toggle source
# File lib/shamu/features/toggle.rb, line 114 def parse_node( node, toggles, state ) if toggle?( node ) params = node.merge!( "name" => state.name ) toggles[state.name] = Toggle.new( params ) else parse_child_nodes( node, toggles, state ) end end
toggle?( node )
click to toggle source
Determines if the yaml entry with the given key is a toggle, or if it's children themselves may be toggles.
# File lib/shamu/features/toggle.rb, line 138 def toggle?( node ) return unless node.is_a? Hash node.keys.all? { |k| TOGGLE_KEYS.include?( k ) } end
Public Instance Methods
enabled?( context )
click to toggle source
@param [Context] context the feature evaluation context. @return [Boolean] true if the toggle should be enabled.
# File lib/shamu/features/toggle.rb, line 67 def enabled?( context ) if selector = matching_selector( context ) !selector.reject end end
retired?( context )
click to toggle source
@param [Context] context the feature evaluation context. @return [Boolean] true if the toggle is retired and should be always on.
# File lib/shamu/features/toggle.rb, line 75 def retired?( context ) !retire_at || context.time > retire_at end
Private Instance Methods
matching_selector( context )
click to toggle source
# File lib/shamu/features/toggle.rb, line 87 def matching_selector( context ) selectors.find { |s| s.match?( context ) } end