Sha256: 91b6e2087b8aa9cdf444d38ee5543f6a706e763a10d5300ac6572628c64491bd

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

require 'i18n'

module IsoBsdI18n

    # A Division encapsulates the classification of wheels into 
    # specified non-intersecting groups.
    class Division
      
      # @param [Hash, #each_pair] group_list Hash mapping division to array of sizes in the division
      def initialize(group_list)
        @raw_groups = group_list
        @data = DivisionData.new(@raw_groups)
        self.extend @data.to_mod
      end

      # @return [Hash] 
      def hash
        @raw_groups
      end

      def == (other)
        @raw_groups == other.hash
      end

      private

      # Defining methods on the fly for Division instances
      # http://blog.jayfields.com/2008/02/ruby-dynamically-define-method.html
      class DivisionData
        def initialize(data)
          @h = data
          @h ||= {}
          @groups = {} # holder for caching
        end

        def to_mod
          h = @h
          grps = @groups
          Module.new do
            h.each_pair do |gname, collection|
              define_method gname do
                col = grps[gname]
                col ||= SizeCollection.new(collection)
                grps[gname] = col
              end
              
              define_method "#{gname}?" do |bsd|
                col = grps[gname]
                col ||= SizeCollection.new(collection)
                col.include?(bsd)
              end
            end
          end
        end
      end # class DivisionData

    end # class Division
end # module BsdIsoI18n

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
iso_bsd-i18n-1.0.0 lib/iso_bsd-i18n/division.rb