Sha256: dacda702648b00250a09ab0c72f7cd17c7db7a6c8d7392e7cd9b72a76ad1b786

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

module Docks
  module Containers
    class PatternLibrary
      attr_reader :patterns

      def initialize
        @patterns = {}
        @summary = false
      end

      def <<(pattern)
        @patterns[pattern.name.to_s] ||= pattern
      end

      def [](pattern_name)
        @patterns[Docks.pattern_id(pattern_name)]
      end

      def has_pattern?(pattern_name)
        !self[pattern_name].nil?
      end

      def to_json; @patterns.to_json end

      def group_by(grouper, &block)
        grouped_patterns = {}
        grouper = grouper.to_sym

        @patterns.each do |name, pattern|
          grouper_value = pattern.send(grouper)
          next if grouper_value.nil?
          grouped_patterns[grouper_value] ||= []
          grouped_patterns[grouper_value] << pattern
        end

        if block_given?
          grouped_patterns.each(&block)
        else
          grouped_patterns
        end
      end

      def groups(&block)
        group_by(:group, &block)
      end

      def find(descriptor)
        descriptor = Descriptor.new(descriptor, assume: :pattern)
        pattern = @patterns[descriptor.pattern]
        return false if pattern.nil?

        symbol = pattern.find(descriptor)
        symbol = nil if !symbol || symbol == pattern
        ::OpenStruct.new(pattern: pattern, symbol: symbol)
      end

      def summarized?; @summary end
      alias_method :summary?, :summarized?

      def summary
        return self if summarized?

        summary = self.class.new
        summary.instance_variable_set(:@summary, true)
        summary.instance_variable_set(:@patterns, Hash[@patterns.map { |name, pattern| [name, pattern.summary] }])
        summary
      end
      alias_method :summarize, :summary

      def summary!
        @summary = true
        @patterns = Hash[@patterns.map { |name, pattern| [name, pattern.summary] }]
      end
      alias_method :summarize!, :summary!
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
docks_app-0.0.3 lib/docks/containers/pattern_library_container.rb
docks_app-0.0.2 lib/docks/containers/pattern_library_container.rb
docks_app-0.0.1 lib/docks/containers/pattern_library_container.rb