<commit_before>#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module BuildrPlus #nodoc

  class Feature < BaseElement
    attr_reader :key
    attr_reader :required_features

    def initialize(key, required_features, options = {}, &block)
      @key = key
      @required_features = required_features

      module_name = ::BuildrPlus::Naming.pascal_case(key)
      ::BuildrPlus.class_eval "module #{module_name}\n end"
      module_instance = ::BuildrPlus.const_get(module_name)

      @component_map = {}

      @component_map[:Config] = module_instance.class

      module_instance.class_eval 'module ProjectExtension; include Extension; end'
      @component_map[:ProjectExtension] = module_instance.const_get(:ProjectExtension)

      FeatureManager.register_feature(self)

      super(options, &block)
    end

    def activated?
      BuildrPlus::ExtensionRegistry.registered?(component_by_key(:ProjectExtension))
    end

    def activate
      BuildrPlus::ExtensionRegistry.register(component_by_key(:ProjectExtension))
    end

    def deactivate
      BuildrPlus::ExtensionRegistry.deregister(component_by_key(:ProjectExtension))
    end

    def enhance(component_key, &block)
      component_by_key(component_key).class_eval &block
    end

    def component_by_key(component_key)
      component = @component_map[component_key]
      BuildrPlus.error("Unknown component key #{component_key.name}") unless component
      component
    end

    def self.is_class_feature?(component_key)
      [:Config].include?(component_key)
    end

    def self.feature_components
      [:Config, :ProjectExtension]
    end
  end

  class FeatureManager
    class << self
      def register_feature(feature)
        BuildrPlus.error("Attempting to redefine feature #{feature.key}") if feature_map[feature.key.to_s]
        feature_map[feature.key.to_s] = feature
      end

      def ensure_activated(key)
        raise "Expected feature #{key} to be activated" unless activated?(key)
      end

      def activated?(key)
        feature_by_name(key).activated?
      end

      def feature?(key)
        !!feature_map[key.to_s]
      end

      def feature_names
        feature_map.keys
      end

      def feature_by_name(key)
        feature = feature_map[key.to_s]
        BuildrPlus.error("Unknown feature '#{key}'") unless feature
        feature
      end

      def feature(definition, options = {}, &block)<commit_msg>Fixed problem where feature activated? fails if feature is unknown.<commit_after>#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module BuildrPlus #nodoc

  class Feature < BaseElement
    attr_reader :key
    attr_reader :required_features

    def initialize(key, required_features, options = {}, &block)
      @key = key
      @required_features = required_features

      module_name = ::BuildrPlus::Naming.pascal_case(key)
      ::BuildrPlus.class_eval "module #{module_name}\n end"
      module_instance = ::BuildrPlus.const_get(module_name)

      @component_map = {}

      @component_map[:Config] = module_instance.class

      module_instance.class_eval 'module ProjectExtension; include Extension; end'
      @component_map[:ProjectExtension] = module_instance.const_get(:ProjectExtension)

      FeatureManager.register_feature(self)

      super(options, &block)
    end

    def activated?
      BuildrPlus::ExtensionRegistry.registered?(component_by_key(:ProjectExtension))
    end

    def activate
      BuildrPlus::ExtensionRegistry.register(component_by_key(:ProjectExtension))
    end

    def deactivate
      BuildrPlus::ExtensionRegistry.deregister(component_by_key(:ProjectExtension))
    end

    def enhance(component_key, &block)
      component_by_key(component_key).class_eval &block
    end

    def component_by_key(component_key)
      component = @component_map[component_key]
      BuildrPlus.error("Unknown component key #{component_key.name}") unless component
      component
    end

    def self.is_class_feature?(component_key)
      [:Config].include?(component_key)
    end

    def self.feature_components
      [:Config, :ProjectExtension]
    end
  end

  class FeatureManager
    class << self
      def register_feature(feature)
        BuildrPlus.error("Attempting to redefine feature #{feature.key}") if feature_map[feature.key.to_s]
        feature_map[feature.key.to_s] = feature
      end

      def ensure_activated(key)
        raise "Expected feature #{key} to be activated" unless activated?(key)
      end

      def activated?(key)
        feature?(key) && feature_by_name(key).activated?
      end

      def feature?(key)
        !!feature_map[key.to_s]
      end

      def feature_names
        feature_map.keys
      end

      def feature_by_name(key)
        feature = feature_map[key.to_s]
        BuildrPlus.error("Unknown feature '#{key}'") unless feature
        feature
      end

      def feature(definition, options = {}, &block)
{'fraction_non_alphanumeric': 0.08508845829823083, 'fraction_numerical': 0.0013479359730412806, 'mean_word_length': 3.433159073935773, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '800975', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1619, 'n_words': 441}