hemp 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.hound.yml +4 -0
- data/.rubocop.yml +233 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +256 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/hemp.gemspec +45 -0
- data/lib/hemp.rb +5 -0
- data/lib/hemp/aliases.rb +13 -0
- data/lib/hemp/application.rb +59 -0
- data/lib/hemp/base_record.rb +96 -0
- data/lib/hemp/controller.rb +54 -0
- data/lib/hemp/dependencies.rb +10 -0
- data/lib/hemp/extensions/hash.rb +10 -0
- data/lib/hemp/extensions/object.rb +9 -0
- data/lib/hemp/orm/class_instance_vars.rb +21 -0
- data/lib/hemp/orm/database_error.rb +10 -0
- data/lib/hemp/orm/property.rb +49 -0
- data/lib/hemp/orm/record_interface.rb +99 -0
- data/lib/hemp/orm/sql_helper.rb +14 -0
- data/lib/hemp/routing/base.rb +83 -0
- data/lib/hemp/routing/route.rb +53 -0
- data/lib/hemp/routing/route_extensions.rb +22 -0
- data/lib/hemp/routing/route_split.rb +37 -0
- data/lib/hemp/routing/route_syntax_error.rb +10 -0
- data/lib/hemp/version.rb +3 -0
- metadata +217 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0aceb1043398fcbc2ee403ad550539c8647492b6
|
4
|
+
data.tar.gz: 4fb5d9141e880dda3075dc91fdae0c91217e10c7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f4ceb28170e939e20bfb350b4c4f4224927c2fd2035cc381afad14fbf5411fec91f1d811890e9b2e553f8bcbf5bc9b0517b73cc5d23ff3ff453328d3ad3e6d14
|
7
|
+
data.tar.gz: 30072432e50fdcfebc57ac84c0bd41183380b5f831dd866d6174f13aec2758596eb47e6d3638a5bc4ca3246d550697aa0ea8a79c32e3e7aec6af4f6c4f406777
|
data/.gitignore
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
/.rspec
|
11
|
+
/lib/hemp/.DS_store
|
12
|
+
/test/coverage
|
13
|
+
/*/.DS_Store
|
14
|
+
/.byebug_history
|
15
|
+
/app
|
16
|
+
/db
|
17
|
+
/test/integration/.DS_Store
|
18
|
+
/test/integration/coverage
|
19
|
+
/test/integration/.byebug_history
|
data/.hound.yml
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- "vendor/**/*"
|
4
|
+
- "db/schema.rb"
|
5
|
+
UseCache: false
|
6
|
+
Style/CollectionMethods:
|
7
|
+
Description: Preferred collection methods.
|
8
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
|
9
|
+
Enabled: true
|
10
|
+
PreferredMethods:
|
11
|
+
collect: map
|
12
|
+
collect!: map!
|
13
|
+
find: detect
|
14
|
+
find_all: select
|
15
|
+
reduce: inject
|
16
|
+
Style/DotPosition:
|
17
|
+
Description: Checks the position of the dot in multi-line method calls.
|
18
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
|
19
|
+
Enabled: true
|
20
|
+
EnforcedStyle: trailing
|
21
|
+
SupportedStyles:
|
22
|
+
- leading
|
23
|
+
- trailing
|
24
|
+
Style/FileName:
|
25
|
+
Description: Use snake_case for source file names.
|
26
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
|
27
|
+
Enabled: false
|
28
|
+
Exclude: []
|
29
|
+
Style/GuardClause:
|
30
|
+
Description: Check for conditionals that can be replaced with guard clauses
|
31
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
|
32
|
+
Enabled: false
|
33
|
+
MinBodyLength: 1
|
34
|
+
Style/IfUnlessModifier:
|
35
|
+
Description: Favor modifier if/unless usage when you have a single-line body.
|
36
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
|
37
|
+
Enabled: false
|
38
|
+
MaxLineLength: 80
|
39
|
+
Style/OptionHash:
|
40
|
+
Description: Don't use option hashes when you can use keyword arguments.
|
41
|
+
Enabled: false
|
42
|
+
Style/PercentLiteralDelimiters:
|
43
|
+
Description: Use `%`-literal delimiters consistently
|
44
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
45
|
+
Enabled: false
|
46
|
+
PreferredDelimiters:
|
47
|
+
"%": "()"
|
48
|
+
"%i": "()"
|
49
|
+
"%q": "()"
|
50
|
+
"%Q": "()"
|
51
|
+
"%r": "{}"
|
52
|
+
"%s": "()"
|
53
|
+
"%w": "()"
|
54
|
+
"%W": "()"
|
55
|
+
"%x": "()"
|
56
|
+
Style/PredicateName:
|
57
|
+
Description: Check the names of predicate methods.
|
58
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
|
59
|
+
Enabled: true
|
60
|
+
NamePrefix:
|
61
|
+
- is_
|
62
|
+
- has_
|
63
|
+
- have_
|
64
|
+
NamePrefixBlacklist:
|
65
|
+
- is_
|
66
|
+
Exclude:
|
67
|
+
- spec/**/*
|
68
|
+
Style/RaiseArgs:
|
69
|
+
Description: Checks the arguments passed to raise/fail.
|
70
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
|
71
|
+
Enabled: false
|
72
|
+
EnforcedStyle: exploded
|
73
|
+
SupportedStyles:
|
74
|
+
- compact
|
75
|
+
- exploded
|
76
|
+
Style/SignalException:
|
77
|
+
Description: Checks for proper usage of fail and raise.
|
78
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
|
79
|
+
Enabled: false
|
80
|
+
EnforcedStyle: semantic
|
81
|
+
SupportedStyles:
|
82
|
+
- only_raise
|
83
|
+
- only_fail
|
84
|
+
- semantic
|
85
|
+
Style/SingleLineBlockParams:
|
86
|
+
Description: Enforces the names of some block params.
|
87
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
|
88
|
+
Enabled: false
|
89
|
+
Methods:
|
90
|
+
- reduce:
|
91
|
+
- a
|
92
|
+
- e
|
93
|
+
- inject:
|
94
|
+
- a
|
95
|
+
- e
|
96
|
+
Style/SingleLineMethods:
|
97
|
+
Description: Avoid single-line methods.
|
98
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
|
99
|
+
Enabled: false
|
100
|
+
AllowIfMethodIsEmpty: true
|
101
|
+
Style/StringLiterals:
|
102
|
+
Description: Checks if uses of quotes match the configured preference.
|
103
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
104
|
+
Enabled: true
|
105
|
+
EnforcedStyle: double_quotes
|
106
|
+
SupportedStyles:
|
107
|
+
- single_quotes
|
108
|
+
- double_quotes
|
109
|
+
Style/StringLiteralsInInterpolation:
|
110
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
111
|
+
match the configured preference.
|
112
|
+
Enabled: true
|
113
|
+
EnforcedStyle: single_quotes
|
114
|
+
SupportedStyles:
|
115
|
+
- single_quotes
|
116
|
+
- double_quotes
|
117
|
+
Style/TrailingComma:
|
118
|
+
Description: Checks for trailing comma in parameter lists and literals.
|
119
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
|
120
|
+
Enabled: false
|
121
|
+
EnforcedStyleForMultiline: no_comma
|
122
|
+
SupportedStyles:
|
123
|
+
- comma
|
124
|
+
- no_comma
|
125
|
+
Metrics/AbcSize:
|
126
|
+
Description: A calculated magnitude based on number of assignments, branches, and
|
127
|
+
conditions.
|
128
|
+
Enabled: false
|
129
|
+
Max: 15
|
130
|
+
Metrics/ClassLength:
|
131
|
+
Description: Avoid classes longer than 100 lines of code.
|
132
|
+
Enabled: false
|
133
|
+
CountComments: false
|
134
|
+
Max: 100
|
135
|
+
Metrics/ModuleLength:
|
136
|
+
CountComments: false
|
137
|
+
Max: 100
|
138
|
+
Description: Avoid modules longer than 100 lines of code.
|
139
|
+
Enabled: false
|
140
|
+
Metrics/CyclomaticComplexity:
|
141
|
+
Description: A complexity metric that is strongly correlated to the number of test
|
142
|
+
cases needed to validate a method.
|
143
|
+
Enabled: false
|
144
|
+
Max: 6
|
145
|
+
Metrics/MethodLength:
|
146
|
+
Description: Avoid methods longer than 10 lines of code.
|
147
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
|
148
|
+
Enabled: false
|
149
|
+
CountComments: false
|
150
|
+
Max: 10
|
151
|
+
Metrics/ParameterLists:
|
152
|
+
Description: Avoid parameter lists longer than three or four parameters.
|
153
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
|
154
|
+
Enabled: false
|
155
|
+
Max: 5
|
156
|
+
CountKeywordArgs: true
|
157
|
+
Metrics/PerceivedComplexity:
|
158
|
+
Description: A complexity metric geared towards measuring complexity for a human
|
159
|
+
reader.
|
160
|
+
Enabled: false
|
161
|
+
Max: 7
|
162
|
+
Lint/AssignmentInCondition:
|
163
|
+
Description: Don't use assignment in conditions.
|
164
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
|
165
|
+
Enabled: false
|
166
|
+
AllowSafeAssignment: true
|
167
|
+
Style/InlineComment:
|
168
|
+
Description: Avoid inline comments.
|
169
|
+
Enabled: false
|
170
|
+
Style/AccessorMethodName:
|
171
|
+
Description: Check the naming of accessor methods for get_/set_.
|
172
|
+
Enabled: false
|
173
|
+
Style/Alias:
|
174
|
+
Description: Use alias_method instead of alias.
|
175
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
|
176
|
+
Enabled: false
|
177
|
+
Style/Documentation:
|
178
|
+
Description: Document classes and non-namespace modules.
|
179
|
+
Enabled: false
|
180
|
+
Style/DoubleNegation:
|
181
|
+
Description: Checks for uses of double negation (!!).
|
182
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
183
|
+
Enabled: false
|
184
|
+
Style/EachWithObject:
|
185
|
+
Description: Prefer `each_with_object` over `inject` or `reduce`.
|
186
|
+
Enabled: false
|
187
|
+
Style/EmptyLiteral:
|
188
|
+
Description: Prefer literals to Array.new/Hash.new/String.new.
|
189
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
|
190
|
+
Enabled: false
|
191
|
+
Style/ModuleFunction:
|
192
|
+
Description: Checks for usage of `extend self` in modules.
|
193
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
|
194
|
+
Enabled: false
|
195
|
+
Style/OneLineConditional:
|
196
|
+
Description: Favor the ternary operator(?:) over if/then/else/end constructs.
|
197
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
|
198
|
+
Enabled: false
|
199
|
+
Style/PerlBackrefs:
|
200
|
+
Description: Avoid Perl-style regex back references.
|
201
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
|
202
|
+
Enabled: false
|
203
|
+
Style/Send:
|
204
|
+
Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
|
205
|
+
may overlap with existing methods.
|
206
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
|
207
|
+
Enabled: false
|
208
|
+
Style/SpecialGlobalVars:
|
209
|
+
Description: Avoid Perl-style global variables.
|
210
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
|
211
|
+
Enabled: false
|
212
|
+
Style/VariableInterpolation:
|
213
|
+
Description: Don't interpolate global, instance and class variables directly in
|
214
|
+
strings.
|
215
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
|
216
|
+
Enabled: false
|
217
|
+
Style/WhenThen:
|
218
|
+
Description: Use when x then ... for one-line cases.
|
219
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
|
220
|
+
Enabled: false
|
221
|
+
Lint/EachWithObjectArgument:
|
222
|
+
Description: Check for immutable argument given to each_with_object.
|
223
|
+
Enabled: true
|
224
|
+
Lint/HandleExceptions:
|
225
|
+
Description: Don't suppress exception.
|
226
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
|
227
|
+
Enabled: false
|
228
|
+
Lint/LiteralInCondition:
|
229
|
+
Description: Checks of literals used in conditions.
|
230
|
+
Enabled: false
|
231
|
+
Lint/LiteralInInterpolation:
|
232
|
+
Description: Checks for literals used in interpolation.
|
233
|
+
Enabled: false
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Oduah Tobi
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,256 @@
|
|
1
|
+
# Hemp
|
2
|
+
[![Build Status](https://semaphoreci.com/api/v1/projects/3124863c-96d9-4e13-933a-23ef8ea0b600/663694/badge.svg)](https://semaphoreci.com/tobi-oduah/hemp-web-framework) [![Code Climate](https://codeclimate.com/github/andela-toduah/hemp-web-framework/badges/gpa.svg)](https://codeclimate.com/github/andela-toduah/hemp-web-framework) [![Test Coverage](https://codeclimate.com/github/andela-toduah/hemp-web-framework/badges/coverage.svg)](https://codeclimate.com/github/andela-toduah/hemp-web-framework/coverage)
|
3
|
+
|
4
|
+
Hemp is a minimalistic web framework designed to understand better, how the Rails web framework works. As with Rails, Hemp is built using Ruby and follows the Model-View-Controller design pattern. Hemp framework comes with a whittled-down ORM similar to the ActiveRecord design.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'hemp'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hemp
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Hemp, like Rails is modeled with the MVC design framework in mind. Therefore, folder structures are very similar. Models, Views, Controllers are organized in respective folders under the app directory.
|
24
|
+
|
25
|
+
To learn more about the MVC design pattern, see https://en.wikipedia.org/wiki/Model-view-controller
|
26
|
+
|
27
|
+
```
|
28
|
+
hemp_project
|
29
|
+
│
|
30
|
+
└───app
|
31
|
+
| └───assets
|
32
|
+
| └───controllers
|
33
|
+
| └───models
|
34
|
+
| └───views
|
35
|
+
└───config
|
36
|
+
| └─── routes.rb
|
37
|
+
| └───application.rb
|
38
|
+
└───db
|
39
|
+
| └───development.sqlite3
|
40
|
+
└───config.ru
|
41
|
+
```
|
42
|
+
|
43
|
+
Database storage is persisted using the SQLite3 database engine, which is the only supported database engine currently.
|
44
|
+
To view a sample web app implemeted with the Hemp framework, see https://github.com/andela-toduah/hemp_todo
|
45
|
+
|
46
|
+
## Initial Setup
|
47
|
+
The Hemp framework is built on top of Rack - a simple interface between webservers that support Ruby and Ruby frameworks. Thus, web apps built with the framework are initialized same way Rack apps are initlialized. Through a `config.ru` file, which is parsed to determine appropriate configuration settings. A sample `config.ru` is:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require "hemp"
|
51
|
+
RACK_ROOT = __dir__
|
52
|
+
|
53
|
+
module HempTodo
|
54
|
+
class Application < Hemp::Application
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
TodoApplication = HempTodo::Application.new
|
59
|
+
|
60
|
+
TodoApplication.pot.prepare do
|
61
|
+
resources :fellows
|
62
|
+
get "/", to: "fellows#index"
|
63
|
+
end
|
64
|
+
|
65
|
+
Hemp::Dependencies.load_files
|
66
|
+
|
67
|
+
use Rack::Static, urls: ["/css", "/images"], root: "app/assets"
|
68
|
+
use Rack::MethodOverride
|
69
|
+
run TodoApplication
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
The first line of the sample config file requires the Hemp framework.
|
74
|
+
|
75
|
+
The second line sets a congstant used by the framework in searching for important files. This is required in order to map locations of important components in the web application. Thus it should be set by apps built on top of Hemp.
|
76
|
+
|
77
|
+
A sample Application class which inherits from the BaseApplication class provided by Hemp is then declared and initialized. This sample class is used to start up the web server, and inherits methods from the BaseApplication class to provide a rack-compatible response to requests. The declaration for the Application class could be moved into a separate class and then required in the config file.
|
78
|
+
|
79
|
+
On instantiating the Application class, routes need to be set. This is done in line 11 of the config file. A block with the route methods called appropriately is passed to the prepare method exposed by the application. This block is evaluated, and routes are saved for processing. The declaration for the routes could be moved into a separate class and then required in the config file.
|
80
|
+
|
81
|
+
On evaluating routes, required files need to be loaded. This is done by calling the `load_files` method of the Dependencies module, provided by Hemp. This loads current controllers/models, and adds the path to these folders to the `LOAD_PATH`.
|
82
|
+
|
83
|
+
On the last line of the config file, the application is passed to the run method, which is evaluated by Rack to start up the web application on the default port - 9292.
|
84
|
+
|
85
|
+
## Key Features
|
86
|
+
|
87
|
+
### Routing
|
88
|
+
Routing with Hemp deals with directing requests to the appropriate controllers. A sample route file is:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
TodoApplication.pot.prepare do
|
92
|
+
get "/", to: "fellows#index"
|
93
|
+
get "/login", to: "sessions#login"
|
94
|
+
post "/login", to: "sessions#create"
|
95
|
+
resources :fellows
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
Hemp supports GET, DELETE, PATCH, POST, PUT requests.
|
100
|
+
|
101
|
+
In the sample config file, the second line indicates that GET requests to the root path of the application should be handled by the `index action of the FellowsController`.
|
102
|
+
|
103
|
+
Thus an appropriate view named index.html.erb in the fellows folder is expected in the views folder. Instance variables set in the index action of the controller are passed to the Erubis template engine which renders the view.
|
104
|
+
|
105
|
+
Resources creates a REST-compatible set of routes which handles CRUD requests dealing with fellows. The declaration is equivalent to writing these set of routes:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
get "/fellows", to: "fellows#index"
|
109
|
+
get "/fellows/new", to: "fellows#new"
|
110
|
+
post "/fellows", to: "fellows#create"
|
111
|
+
get "/fellows/:id", to: "fellows#show"
|
112
|
+
get "/fellows/:id/edit", to: "fellows#edit"
|
113
|
+
patch "/fellows/:id", to: "fellows#update"
|
114
|
+
put "/fellows/:id", to: "fellows#update"
|
115
|
+
delete "/fellows/:id", to: "fellows#destroy"
|
116
|
+
```
|
117
|
+
|
118
|
+
|
119
|
+
### Models
|
120
|
+
All models to be used with the Hemp framework are to inherit from the BaseRecord class provided by Hemp, in order to access the rich ORM functionalities provided. Although not as succinct as ActiveRecord, the BaseRecord class acts as an interface between the model class and its database representation. A sample model file is provided below:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
class Fellow < Hemp::BaseRecord
|
124
|
+
to_table :fellows
|
125
|
+
property :id, type: :integer, primary_key: true
|
126
|
+
property :first_name, type: :text, nullable: false
|
127
|
+
property :email, type: :boolean, nullable: false
|
128
|
+
|
129
|
+
create_table
|
130
|
+
end
|
131
|
+
```
|
132
|
+
The `to_table` method provided stores the table name used while creating the table record in the database.
|
133
|
+
|
134
|
+
The `property` method is provided to declare table columns, and their attributes. The first argument to `property` is the column name, while subsequent hash arguments are used to provide information about attributes.
|
135
|
+
|
136
|
+
The `type` argument represents the data type of the column. Supported data types by Hemp are:
|
137
|
+
|
138
|
+
* integer (for numeric values)
|
139
|
+
* boolean (for boolean values [true or false])
|
140
|
+
* text (for alphanumeric values)
|
141
|
+
|
142
|
+
The `primary_key` argument is used to specify that the column should be used as the primary key of the table. If this is an integer, the value is auto-incremented by the database.
|
143
|
+
|
144
|
+
The `nullable` argument is used to specify whether a column should have null values, or not.
|
145
|
+
|
146
|
+
While creating models, the id property declaration is optional. If this is is not provided, the Hemp ORM adds it automatically, and sets it as the primary key. Thus, it should only be set if you'd like to use a different type as the primary key.
|
147
|
+
|
148
|
+
On passing in the table name, and its properties, a call should be made to the `create_table` method to persist the model to database by creating the table.
|
149
|
+
|
150
|
+
|
151
|
+
### Controllers
|
152
|
+
Controllers are key to the MVC structure, as they handle receiving requests, interacting with the database, and providing responses. Controllers are placed in the controllers folder, which is nested in the app folder.
|
153
|
+
|
154
|
+
All controllers should inherit from the BaseController class provided by Hemp to inherit methods which simplify accessing request parameters and returning responses by rendering views.
|
155
|
+
|
156
|
+
A sample structure for a controller file is:
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
class FellowsController < Hemp::BaseController
|
160
|
+
def index
|
161
|
+
@fellows = Fellow.all
|
162
|
+
end
|
163
|
+
|
164
|
+
def new
|
165
|
+
end
|
166
|
+
|
167
|
+
def show
|
168
|
+
fellow
|
169
|
+
render :show_full
|
170
|
+
end
|
171
|
+
|
172
|
+
def destroy
|
173
|
+
fellow.destroy
|
174
|
+
redirect_to "/"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
Instance variables set by the controllers are passed to the routes while rendering responses.
|
180
|
+
|
181
|
+
Explicitly calling `render` to render template files is optional. If it's not called by the controller action, then it's done automatically by the framework with an argument that's the same name as the action. Thus, you can decide to call `render` explicitly when you want to render a view with a name different from the action.
|
182
|
+
|
183
|
+
|
184
|
+
### Views
|
185
|
+
Currently, view templates are handled through the Tilt gem, with the Erubis template engine. See https://github.com/rtomayko/tilt for more details.
|
186
|
+
|
187
|
+
Views are mapped to actions in controllers. Thus the folder structure for storing views depends on the location of the controller/action. A view to be rendered for the new action in the SessionsController for example is saved as `new.html.erb` in the sessions folder, nested in the views folder. A sample structure for a view file is:
|
188
|
+
|
189
|
+
```erb
|
190
|
+
<div class="big-line-break"></div>
|
191
|
+
<div class="fellow-list">
|
192
|
+
<div class="container">
|
193
|
+
<div class="row">
|
194
|
+
<div class="col l3"> </div>
|
195
|
+
<div class="col l5">
|
196
|
+
<form method="POST">
|
197
|
+
<input type="text" class="white-text" name="first_name" value=<%= fellow.first_name %> />
|
198
|
+
<input type="text" class="white-text" name="last_name" value=<%= fellow.last_name %> />
|
199
|
+
<input type="text" class="white-text" name="email" value=<%= fellow.email %> />
|
200
|
+
<input type="text" class="white-text" name="stack" value=<%= fellow.stack %> />
|
201
|
+
<input name="_method" type="hidden" value="put" />
|
202
|
+
<input type="submit" class="btn right" value="Update Fellow"/>
|
203
|
+
</form>
|
204
|
+
</div>
|
205
|
+
</div>
|
206
|
+
</div>
|
207
|
+
</div>
|
208
|
+
</div>
|
209
|
+
```
|
210
|
+
|
211
|
+
### External Dependencies
|
212
|
+
The Hemp framework has a few dependencies. These are listed below, with links to source pages for each.
|
213
|
+
|
214
|
+
* sqlite3 - https://github.com/sparklemotion/sqlite3-ruby
|
215
|
+
* erubis - https://rubygems.org/gems/erubis
|
216
|
+
* bundler - https://github.com/bundler/bundler
|
217
|
+
* rake - https://github.com/ruby/rake
|
218
|
+
* rack - https://github.com/rack/rack
|
219
|
+
* rack-test - https://github.com/brynary/rack-test
|
220
|
+
* facets - https://github.com/rubyworks/facets
|
221
|
+
* minitest - https://github.com/seattlerb/minitest
|
222
|
+
* tilt - https://github.com/rtomayko/tilt
|
223
|
+
|
224
|
+
## Testing
|
225
|
+
|
226
|
+
Before running tests, run the following command to install dependencies
|
227
|
+
|
228
|
+
$ bundle install
|
229
|
+
|
230
|
+
To test the web framework, run the following command to carry out all tests:
|
231
|
+
|
232
|
+
$ bundle exec rake
|
233
|
+
|
234
|
+
## Contributing
|
235
|
+
|
236
|
+
1. Fork it by visiting - https://github.com/andela-toduah/hemp-web-framework/fork
|
237
|
+
|
238
|
+
2. Create your feature branch
|
239
|
+
|
240
|
+
$ git checkout -b new_feature
|
241
|
+
|
242
|
+
3. Contribute to code
|
243
|
+
|
244
|
+
4. Commit changes made
|
245
|
+
|
246
|
+
$ git commit -a -m 'descriptive_message_about_change'
|
247
|
+
|
248
|
+
5. Push to branch created
|
249
|
+
|
250
|
+
$ git push origin new_feature
|
251
|
+
|
252
|
+
6. Then, create a new Pull Request
|
253
|
+
|
254
|
+
## License
|
255
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
256
|
+
|