Sha256: 5e4b6417c66b8b5be720031bd19eb0dc9340542aac5cfc4ec0fa7dbf932172dd

Contents?: true

Size: 1.23 KB

Versions: 27

Compression:

Stored size: 1.23 KB

Contents

module EasyML::Features
  def transform(df, feature)
    raise NotImplementedError
  end

  def self.included(base)
    base.extend(ClassMethods)
    Registry.register(base)
  end

  module ClassMethods
    def features
      @features ||= []
    end

    def feature(**kwargs)
      features << kwargs.merge!(feature_class: self.to_s)
    end
  end

  def missing_any?(list1, list2)
    (list1 - list2).any?
  end

  class Registry
    class << self
      def register(feature_class, namespace: nil)
        namespace = namespace&.to_sym
        registry[namespace] ||= {}
        registry[namespace][feature_class] = feature_class
      end

      def list(namespace: nil)
        require_files
        namespace ? registry[namespace.to_sym] : registry
      end

      def require_files
        Dir.glob(Rails.root.join("app/features/**/*.rb")).each { |f| require_dependency f }
      end

      def list_flat
        (list.try(:values) || []).flat_map(&:values).flat_map(&:features)
      end

      def find(name)
        list_flat.detect { |feature| feature[:name] == name || feature[:feature_class] == name }
      end

      def clear
        @registry = {}
      end

      private

      def registry
        @registry ||= {}
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
easy_ml-0.2.0.pre.rc7 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc6 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc5 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc4 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc3 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc2 lib/easy_ml/features.rb
easy_ml-0.2.0.pre.rc1 lib/easy_ml/features.rb