Sha256: 9b814a35677e67e0fae21cfd309f5b115d131c0709467402367d597cd1fa6ba2

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 KB

Contents

require 'feature'

module Feature
  class Feature
    def initialize(name, options)
      @name = name
      @options = options
    end

    # Check if the given feature is enabled. If it hasn't been set explicitly,
    # it will fall back to the default provided in the config. If no default
    # was provided, it will return true.
    def enabled?
      backend.enabled?(@name, @options)
    end

    # Check if the feature is enabled for a specific id.
    def enabled_for?(id)
      if id.is_a?(Enumerable)
        raise ArgumentError, "expected an id, got an enumerable"
      end
      backend.enabled?(@name, @options.merge(:for => id))
    end

    # Check if the feature is enabled for all of a given group of ids.
    def enabled_for_all?(ids)
      unless ids.is_a?(Enumerable)
        raise ArgumentError, "expected enumerable, got a #{ids.class}"
      end
      backend.enabled?(@name, @options.merge(for_all: ids))
    end

    # Check if the feature is enabled for any of a given group of ids.
    def enabled_for_any?(ids)
      unless ids.is_a?(Enumerable)
        raise ArgumentError, "expected enumerable, got a #{ids.class}"
      end
      backend.enabled?(@name, @options.merge(for_any: ids))
    end

    # Enable the given feature globally.
    def enable
      backend.enable(@name)
    end

    # Disable the given feature globally.
    def disable
      backend.disable(@name)
    end

    def backend
      ::Feature.backend
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gc_feature-0.1.2 lib/feature/feature.rb
gc_feature-0.1.1 lib/feature/feature.rb
gc_feature-0.1.0 lib/feature/feature.rb