Sha256: 09ba7095d03f778436aaabb1989351f02c4a5df7ea409bb94c1e1149c3f152af

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

module Trax
  module Model
    class Registry
      class_attribute :models

      self.models ||= ::Hashie::Mash.new

      class << self
        delegate :key?, :to => :models
        delegate :each, :to => :models
        delegate :all, :to => :collection
      end

      def self.collection
        models.try(:values)
      end

      def self.register_trax_model(model)
        unless models.key?(model.trax_registry_key)
          models[model.trax_registry_key] = model
        end
      end

      def self.model_type_for_uuid(str)
        prefix = str[0..1]

        uuid_map.fetch(prefix)
      end

      def self.next_prefix
        current_prefix = models.values
                               .reject!{|model| !model.try(:uuid_prefix) }
                               .map(&:uuid_prefix)
                               .sort
                               .last

        current_prefix.next
      end

      def self.model_prefixes
        models.try(:keys)
      end

      def self.previous_prefix
        current_prefix = models.values
                               .reject!{|model| !model.try(:uuid_prefix) }
                               .map(&:uuid_prefix)
                               .sort
                               .last

        current_prefix.previous
      end

      def self.uuid_map
        models.values.reject{|model| model.try(:uuid_prefix) == nil }.inject(::Hashie::Mash.new) do |result, model|
          result[model.uuid_prefix] = model
          result
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
trax_model-0.0.91 lib/trax/model/registry.rb
trax_model-0.0.9 lib/trax/model/registry.rb
trax_model-0.0.8 lib/trax/model/registry.rb
trax_model-0.0.7 lib/trax/model/registry.rb
trax_model-0.0.6 lib/trax/model/registry.rb
trax_model-0.0.5 lib/trax/model/registry.rb
trax_model-0.0.4 lib/trax/model/registry.rb
trax_model-0.0.3 lib/trax/model/registry.rb