Sha256: 343d0ee1baa07efd27279e4fad195bfb5d2b271536de1fecaaa2fc20cefeb243

Contents?: true

Size: 1.6 KB

Versions: 12

Compression:

Stored size: 1.6 KB

Contents

require_relative '../env'

module PantographCore
  class Feature
    class << self
      attr_reader :features

      def register(env_var: nil, description: nil)
        feature = self.new(description: description, env_var: env_var)
        @features[feature.env_var] = feature
      end

      def enabled?(env_var)
        feature = @features[env_var]
        return false if feature.nil?
        return PantographCore::Env.truthy?(feature.env_var)
      end

      def register_class_method(klass: nil, symbol: nil, disabled_symbol: nil, enabled_symbol: nil, env_var: nil)
        return if klass.nil? || symbol.nil? || disabled_symbol.nil? || enabled_symbol.nil? || env_var.nil?
        klass.define_singleton_method(symbol) do |*args|
          if Feature.enabled?(env_var)
            klass.send(enabled_symbol, *args)
          else
            klass.send(disabled_symbol, *args)
          end
        end
      end

      def register_instance_method(klass: nil, symbol: nil, disabled_symbol: nil, enabled_symbol: nil, env_var: nil)
        return if klass.nil? || symbol.nil? || disabled_symbol.nil? || enabled_symbol.nil? || env_var.nil?
        klass.send(:define_method, symbol.to_s) do |*args|
          if Feature.enabled?(env_var)
            send(enabled_symbol, *args)
          else
            send(disabled_symbol, *args)
          end
        end
      end
    end

    @features = {}

    attr_reader :env_var, :description
    def initialize(env_var: nil, description: nil)
      raise "Invalid Feature" if env_var.nil? || description.nil?
      @env_var = env_var
      @description = description
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pantograph-0.1.15 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.14 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.13 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.12 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.10 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.8 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.7 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.6 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.4 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.3 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.1 pantograph_core/lib/pantograph_core/feature/feature.rb
pantograph-0.1.0 pantograph_core/lib/pantograph_core/feature/feature.rb