forked from acaprojects/couchbase-orm
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from Mapotempo/add_yarddoc
add yarddoc (N1QL, view and couchbase orm top level project)
- Loading branch information
Showing
4 changed files
with
213 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,20 +20,46 @@ def self.sanitize(value) | |
end | ||
|
||
module ClassMethods | ||
# Defines a query N1QL for the model | ||
# | ||
# @param [Symbol, String, Array] names names of the views | ||
# @param [Hash] options options passed to the {Couchbase::N1QL} | ||
# | ||
# @example Define some N1QL queries for a model | ||
# class Post < CouchbaseOrm::Base | ||
# n1ql :by_rating, emit_key: :rating | ||
# end | ||
# | ||
# Post.by_rating do |response| | ||
# # ... | ||
# end | ||
# TODO: add range keys [:startkey, :endkey] | ||
# Defines a N1QL query method with dynamic method creation. | ||
# | ||
# N1QL (Non-first Normal Form Query Language) is a powerful query language for Couchbase | ||
# that allows you to perform complex queries on your data. | ||
# For more details, see the [Couchbase N1QL Documentation](https://docs.couchbase.com/server/current/n1ql/n1ql-intro/index.html). | ||
# | ||
# @param name [Symbol, String] The name of the N1QL query method. | ||
# @param query_fn [Proc, nil] An optional function to customize the query. | ||
# @param emit_key [Symbol, Array<Symbol>] The key(s) to emit from the query (optional, defaults to an empty array). | ||
# @param custom_order [String, nil] An optional parameter to define custom ordering for the query. | ||
# @param options [Hash] Additional options for the N1QL query. | ||
# @return [void] | ||
# @raise [ArgumentError] if the class already responds to the given name. | ||
# | ||
# @example Define a N1QL query to find documents by `email` | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# n1ql :find_by_email, emit_key: :email | ||
# end | ||
# | ||
# # This creates a query method `find_by_email` | ||
# users = User.find_by_email(key: '[email protected]') | ||
# | ||
# @example Define a N1QL query with custom ordering | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# n1ql :ordered_by_creation, emit_key: :created_at, custom_order: 'ORDER BY created_at DESC' | ||
# end | ||
# | ||
# # This creates a query method `ordered_by_creation` | ||
# users = User.ordered_by_creation | ||
# | ||
# @example Define a N1QL query with a custom query function | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# n1ql :custom_query, query_fn: ->(keys) { "SELECT * FROM `bucket` WHERE #{keys.map { |k| "`#{k}` = ?" }.join(' AND ')}" }, emit_key: [:email, :username] | ||
# end | ||
# | ||
# # This creates a query method `custom_query` | ||
# users = User.custom_query(key: { email: '[email protected]', username: 'johndoe' }) | ||
def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options) | ||
raise ArgumentError.new("#{self} already respond_to? #{name}") if self.respond_to?(name) | ||
|
||
|
@@ -65,8 +91,38 @@ def n1ql(name, query_fn: nil, emit_key: [], custom_order: nil, **options) | |
end | ||
N1QL_DEFAULTS = { include_docs: true }.freeze | ||
|
||
# add a n1ql query and lookup method to the model for finding all records | ||
# using a value in the supplied attr. | ||
# Sets up a Couchbase N1QL query and a corresponding finder method for the given attribute. | ||
# | ||
# N1QL (Non-first Normal Form Query Language) is a powerful query language for Couchbase | ||
# that allows you to perform complex queries on your data. | ||
# For more details, see the [Couchbase N1QL Documentation](https://docs.couchbase.com/server/current/n1ql/n1ql-intro/index.html). | ||
# | ||
# @param attr [Symbol] The attribute to create the N1QL query and finder method for. | ||
# @param validate [Boolean] Whether to validate the presence of the attribute (default: true). | ||
# @param find_method [Symbol, String, nil] The name of the finder method to be created (optional). | ||
# @param n1ql_method [Symbol, String, nil] The name of the N1QL query method to be created (optional). | ||
# @return [void] | ||
# @raise [ArgumentError] if the class already responds to the given name. | ||
# | ||
# @example Define an index N1QL query for the `email` attribute | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# index_n1ql :email | ||
# end | ||
# | ||
# # This creates a N1QL query method `by_email` and a finder method `find_by_email` | ||
# users = User.by_email(key: ['[email protected]']) | ||
# user = User.find_by_email('[email protected]') | ||
# | ||
# @example Define an index N1QL query for the `username` attribute with custom method names | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# index_n1ql :username, find_method: :find_user_by_username, n1ql_method: :by_username_n1ql | ||
# end | ||
# | ||
# # This creates a N1QL query method `by_username_n1ql` and a finder method `find_user_by_username` | ||
# users = User.by_username_n1ql(key: ['john_doe']) | ||
# user = User.find_user_by_username('john_doe') | ||
def index_n1ql(attr, validate: true, find_method: nil, n1ql_method: nil) | ||
n1ql_method ||= "by_#{attr}" | ||
find_method ||= "find_#{n1ql_method}" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,20 +8,52 @@ module Views | |
extend ActiveSupport::Concern | ||
|
||
module ClassMethods | ||
# Defines a view for the model | ||
# | ||
# @param [Symbol, String, Array] names names of the views | ||
# @param [Hash] options options passed to the {Couchbase::View} | ||
# | ||
# @example Define some views for a model | ||
# class Post < CouchbaseOrm::Base | ||
# view :all | ||
# view :by_rating, emit_key: :rating | ||
# end | ||
# | ||
# Post.by_rating do |response| | ||
# # ... | ||
# end | ||
# Defines a Couchbase view with dynamic method creation. | ||
# | ||
# Couchbase views allow you to create custom queries on your data using map and reduce functions. | ||
# They are defined in design documents and can be queried to retrieve documents or aggregated data. | ||
# For more details, see the [Couchbase Views Basics](https://docs.couchbase.com/server/current/learn/views/views-basics.html). | ||
# | ||
# @param name [Symbol, String] The name of the view. | ||
# @param map [String, nil] The map function for the view (optional). | ||
# @param emit_key [Symbol, Array<Symbol>, nil] The key(s) to emit from the map function (optional). | ||
# @param reduce [String, nil] The reduce function for the view (optional). | ||
# @param options [Hash] Additional options for the view query. | ||
# @return [void] | ||
# @raise [ArgumentError] if the class already responds to the given name. | ||
# | ||
# @example Define a simple view to emit documents by `created_at` | ||
# class MyModel | ||
# include CouchbaseOrm::Model | ||
# view :by_created_at, emit_key: :created_at | ||
# end | ||
# | ||
# # You can now use the dynamically defined method: | ||
# results = MyModel.by_created_at | ||
# | ||
# @example Define a view with multiple emit keys | ||
# class MyModel | ||
# include CouchbaseOrm::Model | ||
# view :by_user_and_date, emit_key: [:user_id, :created_at] | ||
# end | ||
# | ||
# # Use the defined view method | ||
# results = MyModel.by_user_and_date | ||
# | ||
# @example Define a view with a custom map function | ||
# class MyModel | ||
# include CouchbaseOrm::Model | ||
# view :by_custom_map, map: <<-MAP | ||
# function (doc) { | ||
# if (doc.type === "my_model") { | ||
# emit(doc.custom_field, null); | ||
# } | ||
# } | ||
# MAP | ||
# end | ||
# | ||
# # Use the defined view method | ||
# results = MyModel.by_custom_map | ||
def view(name, map: nil, emit_key: nil, reduce: nil, **options) | ||
raise ArgumentError.new("#{self} already respond_to? #{name}") if self.respond_to?(name) | ||
|
||
|
@@ -81,8 +113,37 @@ def view(name, map: nil, emit_key: nil, reduce: nil, **options) | |
end | ||
ViewDefaults = {include_docs: true}.freeze | ||
|
||
# add a view and lookup method to the model for finding all records | ||
# using a value in the supplied attr. | ||
# Sets up a Couchbase view and a corresponding finder method for the given attribute. | ||
# | ||
# Couchbase views allow you to create custom queries on your data using map and reduce functions. | ||
# They are defined in design documents and can be queried to retrieve documents or aggregated data. | ||
# For more details, see the [Couchbase Views Basics](https://docs.couchbase.com/server/current/learn/views/views-basics.html). | ||
# | ||
# @param attr [Symbol] The attribute to create the view and finder method for. | ||
# @param validate [Boolean] Whether to validate the presence of the attribute (default: true). | ||
# @param find_method [Symbol, String, nil] The name of the finder method to be created (optional). | ||
# @param view_method [Symbol, String, nil] The name of the view method to be created (optional). | ||
# @return [void] | ||
# | ||
# @example Define an index view for the `email` attribute | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# index_view :email | ||
# end | ||
# | ||
# # This creates a view method `by_email` and a finder method `find_by_email` | ||
# users_by_email = User.by_email(key: '[email protected]') | ||
# user = User.find_by_email('[email protected]') | ||
# | ||
# @example Define an index view for the `username` attribute with custom method names | ||
# class User | ||
# include CouchbaseOrm::Model | ||
# index_view :username, find_method: :find_user_by_username, view_method: :by_username_view | ||
# end | ||
# | ||
# # This creates a view method `by_username_view` and a finder method `find_user_by_username` | ||
# users_by_username = User.by_username_view(key: 'john_doe') | ||
# user = User.find_user_by_username('john_doe') | ||
def index_view(attr, validate: true, find_method: nil, view_method: nil) | ||
view_method ||= "by_#{attr}" | ||
find_method ||= "find_#{view_method}" | ||
|
@@ -150,6 +211,8 @@ def ensure_design_document! | |
end | ||
end | ||
|
||
private | ||
|
||
def include_docs(view_result) | ||
if view_result.rows.length > 1 | ||
self.find(view_result.rows.map(&:id)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# Author:: Couchbase <[email protected]> | ||
# Copyright:: 2012 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. | ||
# | ||
|
||
require 'rails/generators/named_base' | ||
require 'rails/generators/active_model' | ||
|
||
|