You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/01-introduction.md
+1-5Lines changed: 1 addition & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,6 @@
1
-
---
2
-
sidebar_position: 01
3
-
---
4
-
5
1
# Introduction
6
2
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.
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/02-installation.md
+7-11Lines changed: 7 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,3 @@
1
-
---
2
-
sidebar_position: 02
3
-
---
4
-
5
1
# Installation
6
2
7
3
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
10
6
11
7
Before installing Couchbase ORM, ensure that you have the following prerequisites in place:
12
8
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:
14
10
15
11
```
16
12
ruby -v
17
13
```
18
14
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)
20
16
21
17
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)
22
18
@@ -42,15 +38,15 @@ To install Couchbase ORM, you have a couple of options:
42
38
43
39
Then, run the following command to install the gem:
44
40
45
-
```
41
+
```sh
46
42
bundle install
47
43
```
48
44
49
45
Bundler will take care of installing Couchbase ORM and its dependencies.
50
46
51
47
2.**Using RubyGems**: If you're not using Bundler, you can install Couchbase ORM directly using RubyGems. Run the following command in your terminal:
52
48
53
-
```
49
+
```sh
54
50
gem install couchbase-orm
55
51
```
56
52
@@ -97,9 +93,9 @@ To use Couchbase ORM in your Ruby application, you need to configure the connect
Copy file name to clipboardExpand all lines: docusaurus/docs/tutorial-ruby-couchbase-orm/03-defining-models.md
+99-27Lines changed: 99 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,3 @@
1
-
---
2
-
sidebar_position: 03
3
-
---
4
-
5
1
# Defining Models
6
2
7
3
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:
25
21
26
22
```ruby
27
23
classUser < CouchbaseOrm::Base
28
-
attribute :name, :string
29
24
attribute :email, :string
25
+
attribute :name, :string
30
26
attribute :age, :integer
31
-
attribute :active, :boolean
27
+
attribute :height, :float
28
+
attribute :is_active, :boolean
29
+
attribute :birth_date, :date
32
30
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
33
39
end
34
40
```
35
41
@@ -43,34 +49,35 @@ In this example, we define several attributes for the `User` model, each with a
43
49
-`:datetime`: Represents a date and time value.
44
50
-`:time`: Represents a time value.
45
51
-`:array`: Represents an array of values.
46
-
-`:hash`: Represents a hash (dictionary) of key-value pairs.
47
52
-`:binary`: Represents a binary data value.
48
53
54
+
-`Hash`: Represents a hash (dictionary) of key-value pairs.
55
+
49
56
CouchbaseOrm automatically handles the serialization and deserialization of these attribute types when storing and retrieving documents from Couchbase Server.
50
57
51
58
## 3.3. Attribute Options
52
59
53
60
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:
54
61
55
62
-`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
+
58
64
59
65
Here's an example of using attribute options:
60
66
61
67
```ruby
62
68
classUser < CouchbaseOrm::Base
63
69
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
66
72
end
67
73
```
68
74
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. -->
70
77
71
78
## 3.4. Timestamps
72
79
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`
74
81
75
82
```ruby
76
83
classUser < CouchbaseOrm::Base
@@ -99,16 +106,56 @@ Here are some commonly used callbacks:
99
106
To define a callback, use the corresponding callback method and provide a block or method name to be executed. For example:
100
107
101
108
```ruby
102
-
classUser < CouchbaseOrm::Base
103
-
before_create :set_default_role
109
+
classDocument < 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
+
defbefore_create_callback
125
+
puts"Running before_create callback for #{title}"
126
+
end
127
+
128
+
defafter_create_callback
129
+
puts"Running after_create callback for #{title}"
130
+
end
131
+
132
+
defbefore_save_callback
133
+
puts"Running before_save callback for #{title}"
134
+
end
135
+
136
+
defafter_save_callback
137
+
puts"Running after_save callback for #{title}"
138
+
end
104
139
105
-
defset_default_role
106
-
self.role ||='user'
140
+
defbefore_update_callback
141
+
puts"Running before_update callback for #{title}"
142
+
end
143
+
144
+
defafter_update_callback
145
+
puts"Running after_update callback for #{title}"
146
+
end
147
+
148
+
defbefore_destroy_callback
149
+
puts"Running before_destroy callback for #{title}"
150
+
end
151
+
152
+
defafter_destroy_callback
153
+
puts"Running after_destroy callback for #{title}"
107
154
end
108
155
end
109
156
```
110
157
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.
112
159
113
160
Callbacks allow you to encapsulate logic related to the document lifecycle and maintain a clean and organized codebase.
114
161
@@ -119,14 +166,27 @@ CouchbaseOrm includes built-in validation capabilities to ensure the integrity a
119
166
To define validations, use the `validates` method followed by the attribute name and the desired validation rules. For example:
0 commit comments