Sha256: e5bf1d6f9d57e7de296696e56e2b49dafc834f8c719744cbbc50f774ff182d3b

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'securerandom'

module Jot
  module Ruby
    class ImplBase
      class << self
        attr_reader :registry_key

        def registry_key=(new_name)
          old_name = @registry_key
          @registry_key = new_name.to_s
          ::Jot::Ruby.impl_registry[@registry_key] = self
          ::Jot::Ruby.impl_registry.delete old_name
        end

        def inherited(base)
          super
          base.prepend Prepender
          base.registry_key = SecureRandom.hex
          ::Jot::Ruby.impl_registry[base.registry_key] = base
        end
      end

      def self.operation_class
        @operation_class ||= begin
          klass = Class.new(Operation)
          if self.constants.include? :OperationMethods
            klass.include self::OperationMethods
          else
            raise Errors::NoImplError, "impl has no OperationMethods module"
          end
          klass.prepend Operation::OriginalOperationMethods
          klass
        end
      end

      RAW_METHODS = %i[opFromJSON deserialize diff].freeze
      DEFAULT_OPERATIONS = [
        :NO_OP,                   # Does nothing
        :SET, :LIST,              # General operations
        :MATH,                    # Math
        :SPLICE, :ATINDEX, :MAP,  # Stings and Arrays
        :PUT, :REM, :APPLY,       # Object operations
        :COPY                     # Affect structure
      ].freeze
      DEFAULT_METHODS = RAW_METHODS + DEFAULT_OPERATIONS

      extend Utils::Snippets
      not_implemented *DEFAULT_METHODS

      module Prepender
        DEFAULT_METHODS.each do |method_name|
          define_method method_name do |*args|
            self.class.operation_class.new(super(*args))
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jot-ruby-0.1.4 lib/jot/ruby/impl_base.rb