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: _sessions/c4s2/5_homework.md
+71-1Lines changed: 71 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -12,4 +12,74 @@ title: Homework
12
12
We have only really begun to touch on what you can do with classes in ruby. There are really important concepts in object oriented programming (e.g. inheritance, class methods, class variables, mixins, etc.) that we haven't really mentioned at all. We'll meet some of these things in the next few weeks, but if you want a more comprehensive overview you can take a look at one of the following:
Next session we will look at storing information in a database. The database we'll be using is [MongoDB](http://www.mongodb.org/).
20
+
21
+
To use mongodb you need to have it running on your laptop. The database will run on one of your localhost ports (like sinatra), so that other processes can connect to it.
22
+
23
+
The preparation falls into four parts:
24
+
25
+
1. Install MongoDB.
26
+
2. Start running the database.
27
+
3. Install the gems to allow ruby to connect to MongoDB.
28
+
4. Test your installation.
29
+
30
+
We will look at these parts separately.
31
+
32
+
### Install MongoDB
33
+
34
+
If you installed `homebrew` ("The Hard Way", Mac only), your task is now easy. At the command line write:
35
+
36
+
brew install mongo
37
+
38
+
If you didn't install homebrew (i.e. try doing the above and it doens't work), you will need to [download MongoDB](http://www.mongodb.org/downloads) from the site and follow the installation instructions for your system.
39
+
40
+
{% exercise %}
41
+
Install MongoDB either via
42
+
43
+
$ brew install mongo
44
+
45
+
or by downloading and installing from the [MongoDB website](http://www.mongodb.org/downloads).
46
+
{% endexercise %}
47
+
48
+
### Start MongoDB running
49
+
50
+
You can start MongoDB running from the command line:
51
+
52
+
$ mongod
53
+
54
+
You will need to **keep this command line open** and open a new one to continue the instructions.
55
+
56
+
By default mongo will run on [localhost:28017](http://localhost:28017/). If you visit that link in your browser you should see a mongo stats page.
57
+
58
+
{% exercise %}
59
+
1. Start MongoDB:
60
+
61
+
$ mongod
62
+
63
+
2. Check the service is running at [localhost:28017](http://localhost:28017/).
64
+
{% endexercise %}
65
+
66
+
### Install the gems and test
67
+
68
+
I've set up a project with the gems you need, so this should be straightforward:
Copy file name to clipboardExpand all lines: _sessions/c4s3/1_intro_to_mongo.md
+98-29Lines changed: 98 additions & 29 deletions
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,6 @@
2
2
title: About MongoDB
3
3
---
4
4
5
-
{% exercise %}
6
-
Clone the code for this session:
7
-
8
-
$ git clone https://github.com/code61/mongo1.git
9
-
10
-
{% endexercise %}
11
5
12
6
In this session we will be using a database - a specialised piece of software for storing and retrieving data. Databases become important when you have large amounts of data, which you want to be able to access quickly and which you want to keep consistent.
13
7
@@ -20,6 +14,69 @@ MongoDB is one of many different databases that we could have chosen. We went wi
20
14
21
15
MongoDB is a *NOSQL*, or *document-based*, database. In the past, the more traditional style *SQL*, or *relational*, databases were used in most applications. NOSQL databases have risen in popularity in the last year or two, in part due to their ability to offer increased performance in certain common scenarios, by allowing developers to bend the rigid SQL database structures. We will go into this in slightly more depth later in the course. For the time being, with data we're storing in the next few sessions it won't make much difference whether our db is SQL or NOSQL.
22
16
17
+
### Using Mongoid
18
+
19
+
Mongoid is a ruby library that allows you to interface with MongoDB. The idea is very simple - you can think of Mongoid as mapping **classes** in your application to **tables** in the database. Take a look at the following example:
20
+
21
+
{% highlight ruby %}
22
+
class Athlete
23
+
include Mongoid::Document
24
+
25
+
field :name, type: String
26
+
field :country, type: String
27
+
field :age, type: Integer
28
+
field :height, type: Integer
29
+
field :weight, type: Integer
30
+
field :sex, type: String
31
+
field :dob, type: Time
32
+
field :gold, type: Integer
33
+
field :silver, type: Integer
34
+
field :bronze, type: Integer
35
+
field :sport, type: Integer
36
+
field :events, type: Array
37
+
38
+
def total_medals
39
+
bronze + silver + bronze
40
+
end
41
+
end
42
+
{% endhighlight %}
43
+
44
+
This is fairly similar to the classes we looked at last week - we have data and methods that act on the data. The two different parts are:
45
+
46
+
*`include Mongoid::Document`: this activates this class as a mongoid-enabled class.
47
+
* The `field :name, type: String` etc.: these setup the getting and setting methods that we had to do by hand last week, and tell mongoid what sort of object it is, which is useful for storage purposes.
48
+
49
+
You can use this class like this:
50
+
{% highlight ruby %}
51
+
a = Athlete.new
52
+
a.name = "Michael Phelps"
53
+
a.country = "US"
54
+
a.sport = "Swimming"
55
+
a.gold = 2
56
+
a.silver = 2
57
+
a.bronze = 0
58
+
59
+
a.name #=> "Michael Phelps"
60
+
a.total_medals #=> 4
61
+
{% endhighlight %}
62
+
63
+
This is not that exciting - we could have done all this last week. It gets more interesting when we start using some of the extra methods that Mongoid has added to the `Athlete` class:
@@ -31,35 +88,47 @@ You will need to keep this command line open and continue the instructions in a
31
88
32
89
By default mongo will run on [localhost:28017](http://localhost:28017/). If you visit that link in your browser you should see a mongo stats page.
33
90
34
-
### Using it in ruby
91
+
{% exercise %}
92
+
1. Start MongoDB running on your laptop.
93
+
1. Clone down the code for the session: [https://github.com/code61/sinatra_c4s3](https://github.com/code61/sinatra_c4s3)
94
+
2. Run `bundle install` to get the required gems.
95
+
3. Open up `irb`.
96
+
4. Type `require './utils'` (this loads in the `Athlete` class and sets up your MongoDB connection).
97
+
5. Try the following
98
+
{% highlight ruby %}
99
+
a = Athlete.new
100
+
a.name = "Michael Phelps"
101
+
a.country = "US"
102
+
a.sport = "Swimming"
103
+
a.gold = 2
104
+
a.silver = 2
105
+
a.bronze = 0
35
106
36
-
To interface with MongoDB from ruby we will be using the [Mongoid](http://mongoid.org/) gem. You should have installed the gem already, but if not,
107
+
a.name
108
+
a.country
37
109
38
-
$ gem install mongoid
110
+
a.save
39
111
40
-
should do the trick.
112
+
Athlete.count
113
+
b = Athlete.first
41
114
42
-
We then need to set some configuration options in our ruby project. Among other things this tells the project which of the mongodb databases on your computer to use - usually you'd want each project to have its own database. The following configuration options should go in a `mongoid.yml` file:
115
+
b.name
116
+
b.name = "Tom Close"
43
117
44
-
{% highlight yaml %}
45
-
development:
46
-
sessions:
47
-
default:
48
-
hosts:
49
-
- localhost:27017
50
-
database: mongo_examples
51
-
options:
52
-
raise_not_found_error: false
53
-
{% endhighlight %}
118
+
a.name # has this updated?
54
119
55
-
You then need to include the following code in your `app.rb` (or similar) file:
120
+
b.save
121
+
a.name # has it updated now?
56
122
57
-
{% highlight ruby %}
58
-
require 'mongoid'
59
-
require 'json'
123
+
a.reload
124
+
a.name
60
125
61
-
Mongoid.load!("mongoid.yml", :development)
62
-
{% endhighlight %}
63
-
64
-
We've seen the `require` statements before - they just tell ruby that we'd like to use the `mongoid` library (and also `json`). The line `Mongoid.load!("mongoid.yml", :development)` tells `mongoid` where to find the configuration options. The `:development` bit refers to our environment - you will probably want to have different configuration options when you're deploying your app to heroku; mongoid allows this by specifying `:production` and `:development` environments.
126
+
# different way of creating an athlete, using a hash
127
+
c = Athlete.new(:name => "Chad Le Clos", :country => "South Africa",
0 commit comments