Sha256: cb1c6f0f10bb687cfe268220cfdc32a5cc8e8d10672617f00ecbf1c68ac6d2c8

Contents?: true

Size: 1.94 KB

Versions: 4

Compression:

Stored size: 1.94 KB

Contents

module CDQ

  class CDQObject

    include CDQ

    def contexts
      @@context_manager ||= CDQContextManager.new(store_manager: stores)
    end

    def stores
      @@store_manager ||= CDQStoreManager.new(model_manager: models)
    end

    def models
      @@model_manager ||= CDQModelManager.new
    end

    def reset!(opts = {})
      @@context_manager.reset!
      @@context_manager = nil
      @@store_manager.reset!
      @@store_manager = nil
    end

    def setup(opts = {})
      if opts[:context]
        contexts.push(opts[:context])
        return true
      elsif opts[:store]
        stores.current = opts[:store]
      elsif opts[:model]
        models.current = opts[:model]
      end
      contexts.new(NSMainQueueConcurrencyType)
      true
    end

    def save(*args)
      contexts.save(*args)
    end

    protected

    def with_error_object(default, &block)
      error = Pointer.new(:object)
      result = block.call(error)
      if error[0]
        raise "Error while fetching: #{error[0].debugDescription}"
      end
      result || default
    end

    def constantize(camel_cased_word)
      names = camel_cased_word.split('::')
      names.shift if names.empty? || names.first.empty?

      names.inject(Object) do |constant, name|
        if constant == Object
          constant.const_get(name)
        else
          candidate = constant.const_get(name)
          next candidate if constant.const_defined?(name, false)
          next candidate unless Object.const_defined?(name)

          # Go down the ancestors to check it it's owned
          # directly before we reach Object or the end of ancestors.
          constant = constant.ancestors.inject do |const, ancestor|
            break const    if ancestor == Object
            break ancestor if ancestor.const_defined?(name, false)
            const
          end

          # owner is in Object, so raise
          constant.const_get(name, false)
        end
      end
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cdq-0.1.6 motion/cdq/object.rb
cdq-0.1.5 motion/cdq/object.rb
cdq-0.1.2 motion/cdq/object.rb
cdq-0.1.1 motion/cdq/object.rb