Skip to content

Commit 2a0fe18

Browse files
committed
changed from pseudocode to code with output and removed unnecessary pages
1 parent 99d0a4b commit 2a0fe18

16 files changed

+800
-877
lines changed

docusaurus/docs/tutorial-ruby-couchbase-orm/01-introduction.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
---
2-
sidebar_position: 01
3-
---
4-
51
# Introduction
62

7-
Welcome to the documentation for Couchbase ORM, a Object-Relational Mapping (ORM) library for Ruby that simplifies interactions with Couchbase Server. This guide will walk you through the features and usage of Couchbase ORM, helping you build efficient and scalable Ruby applications with Couchbase.
3+
Welcome to the documentation for Couchbase ORM, an Object-Relational Mapping (ORM) library for Ruby that simplifies interactions with Couchbase Server. This guide will walk you through the features and usage of Couchbase ORM, helping you build efficient and scalable Ruby applications with Couchbase.
84

95
## 1.1. What is Couchbase ORM?
106

docusaurus/docs/tutorial-ruby-couchbase-orm/02-installation.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
---
2-
sidebar_position: 02
3-
---
4-
51
# Installation
62

73
Installing Couchbase ORM is a straightforward process. In this section, we'll guide you through the prerequisites and the step-by-step installation procedure.
@@ -10,13 +6,13 @@ Installing Couchbase ORM is a straightforward process. In this section, we'll gu
106

117
Before installing Couchbase ORM, ensure that you have the following prerequisites in place:
128

13-
1. **Ruby**: Couchbase ORM requires Ruby version 2.5 or higher. You can check your Ruby version by running the following command in your terminal:
9+
1. **Ruby**: Couchbase ORM requires Ruby version 2.7 or higher. You can check your Ruby version by running the following command in your terminal:
1410

1511
```
1612
ruby -v
1713
```
1814

19-
If you don't have Ruby installed or your version is older than 2.5, you can install the latest version from the official Ruby website: [https://www.ruby-lang.org](https://www.ruby-lang.org)
15+
If you don't have Ruby installed or your version is older than 2.7, you can install the latest version from the official Ruby website: [https://www.ruby-lang.org](https://www.ruby-lang.org)
2016

2117
2. **Couchbase Server**: Couchbase ORM works with Couchbase Server, a NoSQL database. Make sure you have Couchbase Server installed and running on your system. You can download Couchbase Server from the official website: [https://www.couchbase.com/downloads](https://www.couchbase.com/downloads)
2218

@@ -42,15 +38,15 @@ To install Couchbase ORM, you have a couple of options:
4238

4339
Then, run the following command to install the gem:
4440

45-
```
41+
```sh
4642
bundle install
4743
```
4844

4945
Bundler will take care of installing Couchbase ORM and its dependencies.
5046

5147
2. **Using RubyGems**: If you're not using Bundler, you can install Couchbase ORM directly using RubyGems. Run the following command in your terminal:
5248

53-
```
49+
```sh
5450
gem install couchbase-orm
5551
```
5652

@@ -97,9 +93,9 @@ To use Couchbase ORM in your Ruby application, you need to configure the connect
9793
```ruby
9894
Couchbase ORM.configure do |config|
9995
config.connection_string = 'couchbase://localhost'
100-
config.bucket = 'my_app'
101-
config.username = 'my_username'
102-
config.password = 'my_password'
96+
config.bucket = 'travel-sample'
97+
config.username = 'Administrator'
98+
config.password = 'password'
10399
end
104100
```
105101

docusaurus/docs/tutorial-ruby-couchbase-orm/03-defining-models.md

Lines changed: 99 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
---
2-
sidebar_position: 03
3-
---
4-
51
# Defining Models
62

73
In CouchbaseOrm, models are defined as Ruby classes that inherit from `CouchbaseOrm::Base`. Each model represents a document type in Couchbase Server and encapsulates the data and behavior of the objects in your application.
@@ -25,11 +21,21 @@ Here's an example of defining attributes in the `User` model:
2521

2622
```ruby
2723
class User < CouchbaseOrm::Base
28-
attribute :name, :string
2924
attribute :email, :string
25+
attribute :name, :string
3026
attribute :age, :integer
31-
attribute :active, :boolean
27+
attribute :height, :float
28+
attribute :is_active, :boolean
29+
attribute :birth_date, :date
3230
attribute :created_at, :datetime
31+
attribute :updated_at, :datetime
32+
attribute :appointment_time, :time
33+
attribute :hobbies, :array, type: :string
34+
attribute :metadata, type: Hash
35+
attribute :avatar, :binary
36+
37+
attribute :created_at, :datetime
38+
attribute :updated_at, :datetime
3339
end
3440
```
3541

@@ -43,34 +49,35 @@ In this example, we define several attributes for the `User` model, each with a
4349
- `:datetime`: Represents a date and time value.
4450
- `:time`: Represents a time value.
4551
- `:array`: Represents an array of values.
46-
- `:hash`: Represents a hash (dictionary) of key-value pairs.
4752
- `:binary`: Represents a binary data value.
4853

54+
- `Hash`: Represents a hash (dictionary) of key-value pairs.
55+
4956
CouchbaseOrm automatically handles the serialization and deserialization of these attribute types when storing and retrieving documents from Couchbase Server.
5057

5158
## 3.3. Attribute Options
5259

5360
In addition to specifying the attribute type, you can also provide additional options to customize the behavior of the attributes. Some commonly used options include:
5461

5562
- `default`: Specifies a default value for the attribute if no value is provided.
56-
- `alias`: Defines an alternate name for the attribute in the document.
57-
- `readonly`: Marks the attribute as read-only, preventing it from being modified.
63+
5864

5965
Here's an example of using attribute options:
6066

6167
```ruby
6268
class User < CouchbaseOrm::Base
6369
attribute :name, :string, default: 'Unknown'
64-
attribute :email, :string, alias: :contact_email
65-
attribute :age, :integer, readonly: true
70+
# attribute :email, :string, alias: :contact_email
71+
# attribute :age, :integer, readonly: true
6672
end
6773
```
6874

69-
In this example, the `name` attribute has a default value of `'Unknown'`, the `email` attribute is aliased as `contact_email` in the document, and the `age` attribute is marked as read-only.
75+
In this example, the `name` attribute has a default value of `'Unknown'`.
76+
<!-- the `email` attribute is aliased as `contact_email` in the document, and the `age` attribute is marked as read-only. -->
7077

7178
## 3.4. Timestamps
7279

73-
CouchbaseOrm provides built-in support for timestamp attributes. By default, if you define attributes named `created_at` and `updated_at` with the `:datetime` type, CouchbaseOrm will automatically populate these attributes with the current date and time when a document is created or updated.
80+
CouchbaseOrm provides built-in support for timestamp attributes. By default, if you define attributes named `created_at` and `updated_at` with the `:datetime` type, CouchbaseOrm will automatically populate these attributes with the current date and time when a document is created or updated. To enable this feature, add the `created_at` and `updated_at` attributes to your model.The fields are automatically updated when the document is saved.`document.save`
7481

7582
```ruby
7683
class User < CouchbaseOrm::Base
@@ -99,16 +106,56 @@ Here are some commonly used callbacks:
99106
To define a callback, use the corresponding callback method and provide a block or method name to be executed. For example:
100107

101108
```ruby
102-
class User < CouchbaseOrm::Base
103-
before_create :set_default_role
109+
class Document < CouchbaseOrm::Base
110+
attribute :title, :string
111+
attribute :content, :string
112+
113+
before_create :before_create_callback
114+
after_create :after_create_callback
115+
before_save :before_save_callback
116+
after_save :after_save_callback
117+
before_update :before_update_callback
118+
after_update :after_update_callback
119+
before_destroy :before_destroy_callback
120+
after_destroy :after_destroy_callback
121+
122+
private
123+
124+
def before_create_callback
125+
puts "Running before_create callback for #{title}"
126+
end
127+
128+
def after_create_callback
129+
puts "Running after_create callback for #{title}"
130+
end
131+
132+
def before_save_callback
133+
puts "Running before_save callback for #{title}"
134+
end
135+
136+
def after_save_callback
137+
puts "Running after_save callback for #{title}"
138+
end
104139

105-
def set_default_role
106-
self.role ||= 'user'
140+
def before_update_callback
141+
puts "Running before_update callback for #{title}"
142+
end
143+
144+
def after_update_callback
145+
puts "Running after_update callback for #{title}"
146+
end
147+
148+
def before_destroy_callback
149+
puts "Running before_destroy callback for #{title}"
150+
end
151+
152+
def after_destroy_callback
153+
puts "Running after_destroy callback for #{title}"
107154
end
108155
end
109156
```
110157

111-
In this example, the `set_default_role` method is defined as a `before_create` callback. It sets a default role for the user if no role is provided before creating a new document.
158+
In this example, the `Document` model defines several callbacks that are triggered at different points in the document's lifecycle. The callback methods are implemented as private instance methods within the model class.
112159

113160
Callbacks allow you to encapsulate logic related to the document lifecycle and maintain a clean and organized codebase.
114161

@@ -119,14 +166,27 @@ CouchbaseOrm includes built-in validation capabilities to ensure the integrity a
119166
To define validations, use the `validates` method followed by the attribute name and the desired validation rules. For example:
120167

121168
```ruby
122-
class User < CouchbaseOrm::Base
123-
attribute :name, :string
169+
class Book < CouchbaseOrm::Base
170+
attribute :title, :string
171+
attribute :author, :string
172+
attribute :pages, :integer
173+
attribute :genre, :string
124174
attribute :email, :string
125-
attribute :age, :integer
126175

127-
validates :name, presence: true
176+
validates_presence_of :title
177+
validates :author, presence: true
178+
validates :pages, numericality: { greater_than: 0 }
179+
validates :genre, inclusion: { in: %w[Fiction Non-Fiction] }
180+
validates :author, format: { with: /\A[a-zA-Z]+\z/, message: 'only allows letters' }
181+
validates :pages, length: { maximum: 500 }
182+
validates :genre, exclusion: { in: %w[Science-Fiction] }
128183
validates :email, format: { with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i }
129-
validates :age, numericality: { greater_than_or_equal_to: 18 }
184+
185+
validate :custom_validation
186+
187+
private
188+
189+
...
130190
end
131191
```
132192

@@ -139,22 +199,34 @@ In this example, we define several validations for the `User` model:
139199
CouchbaseOrm provides a wide range of built-in validation helpers, such as:
140200

141201
- `presence`: Ensures that the attribute is not blank.
142-
- `uniqueness`: Ensures that the attribute value is unique among all documents.
202+
- `validates_presence_of`: An alias for `presence`.
203+
<!-- - `uniqueness`: Ensures that the attribute value is unique among all documents. -->
143204
- `format`: Validates the attribute value against a regular expression.
144205
- `length`: Validates the length of the attribute value.
145206
- `numericality`: Validates that the attribute value is a valid number.
146207
- `inclusion`: Ensures that the attribute value is included in a given set.
147208
- `exclusion`: Ensures that the attribute value is not included in a given set.
148209

210+
149211
You can also define custom validation methods by adding methods to your model class and using the `validate` method to trigger them. For example:
150212

151213
```ruby
152-
class User < CouchbaseOrm::Base
214+
class Book < CouchbaseOrm::Base
215+
attribute :title, :string
216+
...
217+
153218
validate :custom_validation
154219

220+
private
221+
155222
def custom_validation
156-
if some_condition
157-
errors.add(:base, 'Custom validation failed')
223+
puts 'Running custom validation...'
224+
if title&.include?('Funny')
225+
errors.add(:title, 'should not contain the word "Funny"')
226+
else
227+
# print the title
228+
puts "Title: #{title}"
229+
puts 'Custom validation passed'
158230
end
159231
end
160232
end

0 commit comments

Comments
 (0)