class Dock::Couchbase

Public Instance Methods

all(options = {}) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 14
def all(options = {})
    view_name, view_options, options = view_for_options(options)
    conditions, order, limit, offset = extract_conditions!(options)
    stream = model.send(view_name, view_options)
    # deal with everything which wasn't handled via the view
    stream = apply_conditions(stream, conditions)
    stream = apply_order(stream, order)
    stream = stream.drop(offset) if offset
    stream = stream.take(limit) if limit
    stream.to_a # make sure to return an array
end
associations() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 45
def associations()

end
belongs_to() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 61
def belongs_to()

end
column_names() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 51
def column_names()
  model.properties.map(&:name)
end
count(options = {}) click to toggle source

Checked

# File lib/dock/adapters/couchbase.rb, line 55
def count(options = {})
  all(options).count
end
create(attributes = {}) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 5
def create(attributes = {})
  model.create!(attributes)
end
cyclic?() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 73
def cyclic?
  false
end
destroy(object) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 42
def destroy(object)
    object.destroy if valid_object?(object)
end
embedded?() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 70
def embedded?
  false
end
encoding() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 58
def encoding
  'UTF-8'
end
find(id) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 8
def find(id)
  model.find wrap_key(id)
end
find!(id) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 11
def find!(id)
  model.find_by_id(wrap_key(id))
end
first(options = {}) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 25
def first(options = {})
  id = options.delete(:id)
  conditions, _ = extract_conditions!(options.dup)
  if id
    apply_conditions([get(id)], conditions).first
  else
    find_all(options).first
  end
end
has_many() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 64
def has_many()

end
id() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 34
def id
  model.id
end
model_name() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 48
def model_name()
  model.class.name
end
properties() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 79
def properties()

end
scoped?() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 67
def scoped?
  false
end
supports_joins?() click to toggle source
# File lib/dock/adapters/couchbase.rb, line 76
def supports_joins?
  false
end
update(search_key=:id, find_by, update_key, by_value) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 37
def update(search_key=:id, find_by, update_key, by_value)
  entry = model.find(find_by)
  entry.update(update_key => by_value)
  entry.save
end

Private Instance Methods

apply_conditions(stream, conditions) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 83
def apply_conditions(stream, conditions)
  return stream if conditions.empty?
  stream.select { |item| satisfies_conditions? item, conditions }
end
apply_order(stream, order) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 92
def apply_order(stream, order)
  return stream if order.empty?

  stream.to_a.sort_by do |item|
    sort = []
    order = order.to_enum
    o = order.next
    loop do
      case o
      when Array
        value = item.send(o[0])
        value = invert_value(value) if o[1] == :desc
        sort.push(value)
      else
        value = item.send(o[0])
        case order.peek
        when :asc
          begin
            order.next
          rescue StopIteration
            break
          end
        when :desc
          value = invert_value(value)
          begin
            order.next
          rescue StopIteration
            break
          end
        end
        sort.push(value)
      end
      begin
        o = order.next
      rescue StopIteration
        break
      end
    end
    sort
  end
end
invert_value(value) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 134
def invert_value(value)
  case value
  when String
    inverse = []
    value.each_codepoint { |c| inverse.push(-c) }
    inverse
  else
    -value
  end
end
satisfies_conditions?(item, conditions) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 88
def satisfies_conditions?(item, conditions)
  conditions.all? { |field, value| item.send(field) == value }
end
view_for_options(options) click to toggle source
# File lib/dock/adapters/couchbase.rb, line 145
def view_for_options(options)
  view_name = :all
  view_options = { :stale => false }
  conditions, order, limit, offset = extract_conditions!(options.dup)

  # TODO would be nice to merge multiple conditions into one view name
  # for example users have a rating, and the comprised key is [user,
  # rating] if the view is named "by_user_and_rating" we could then merge
  # this into one and even apply the ordering in one go
  remaining_conditions = conditions.reject { |condition, value|
    if klass.respond_to?("by_#{condition}")
      view_name = "by_#{condition}".to_sym
      view_options[:key] = value
    end
  }

  options = { :conditions => remaining_conditions, :order => order }

  if remaining_conditions.empty?
    # merge limit, and offset conditions into view query
    view_options[:limit] = limit if limit
    view_options[:skip] = offset if offset
  else
    options[:limit] = limit if limit
    options[:offset] = offset if offset
  end

  raise MissingViewException.new(view_name) unless klass.respond_to?(view_name)

  [view_name, view_options, options]
end