Sha256: 729d654d0eaeecc49e1d88db02de24f99d5a4be91059f8c1e4073480b61412cb

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# Author:: Couchbase <info@couchbase.com>
# Copyright:: 2011 Couchbase, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

module Couchbase
  class Document
    # Undefine as much methods as we can to free names for views
    instance_methods.each do |m|
      undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$|^class$|)/
    end

    attr_accessor :data, :views

    def initialize(connection, data)
      @data = data
      @connection = connection
      @views = []
      begin
        if design_doc?
          data['views'].each do |name, funs|
            @views << name
            self.instance_eval <<-EOV, __FILE__, __LINE__ + 1
              def #{name}(params = {})
                endpoint = "\#{@connection.next_node.couch_api_base}/\#{@data['_id']}/_view/#{name}"
                View.new(@connection, endpoint, params)
              end
            EOV
          end
        end
      rescue NoMethodError
      end
    end

    def self.wrap(connection, data)
      Document.new(connection, data['doc'] || data)
    end

    def [](key)
      @data[key]
    end

    def []=(key, value)
      @data[key] = value
    end

    def design_doc?
      !!(@data['_id'] =~ %r(_design/))
    end

    def has_views?
      !!(design_doc? && !@views.empty?)
    end

    def inspect
      %(#<#{self.class.name}:#{self.object_id} #{@data.inspect}>)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
couchbase-0.9.8 lib/couchbase/document.rb
couchbase-0.9.7 lib/couchbase/document.rb