rollerskates 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7ae0700f032e105dbc7cbb89d83cc9b19b964a4b
4
+ data.tar.gz: 1a72d44264c07dca64df5fcb2f37f0bec46f1281
5
+ SHA512:
6
+ metadata.gz: f6f092c09b853b9fa3c7337454746f0e30e41b258a47e90653b3f9c069e2dcd84e4f256b07a45968a5af2e96e8600fd448c31c0a2fe018c66d86423ac9c696bc
7
+ data.tar.gz: aa7aa9be0a13306ded382ed5cd740f9ae55b3091b54214c22a5a1169ae6261910e5e025f4c3c70fa4b82aac32fd472a2d86424e743fd532de8842255358c6668
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ repo_token: 4KtPLkDI2Xe1gIqYwpP7xXXNVywG58JrG
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /rollerskates-0.1.0.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,234 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "bin/*"
4
+ - "rollerskates.gemspec"
5
+ - "app/assets/*"
6
+ UseCache: false
7
+ Style/CollectionMethods:
8
+ Description: Preferred collection methods.
9
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
10
+ Enabled: true
11
+ PreferredMethods:
12
+ collect: map
13
+ collect!: map!
14
+ find: detect
15
+ find_all: select
16
+ reduce: inject
17
+ Style/DotPosition:
18
+ Description: Checks the position of the dot in multi-line method calls.
19
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
20
+ Enabled: true
21
+ EnforcedStyle: trailing
22
+ SupportedStyles:
23
+ - leading
24
+ - trailing
25
+ Style/FileName:
26
+ Description: Use snake_case for source file names.
27
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
28
+ Enabled: false
29
+ Exclude: []
30
+ Style/GuardClause:
31
+ Description: Check for conditionals that can be replaced with guard clauses
32
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
33
+ Enabled: false
34
+ MinBodyLength: 1
35
+ Style/IfUnlessModifier:
36
+ Description: Favor modifier if/unless usage when you have a single-line body.
37
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
38
+ Enabled: false
39
+ MaxLineLength: 80
40
+ Style/OptionHash:
41
+ Description: Don't use option hashes when you can use keyword arguments.
42
+ Enabled: false
43
+ Style/PercentLiteralDelimiters:
44
+ Description: Use `%`-literal delimiters consistently
45
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
46
+ Enabled: false
47
+ PreferredDelimiters:
48
+ "%": "()"
49
+ "%i": "()"
50
+ "%q": "()"
51
+ "%Q": "()"
52
+ "%r": "{}"
53
+ "%s": "()"
54
+ "%w": "()"
55
+ "%W": "()"
56
+ "%x": "()"
57
+ Style/PredicateName:
58
+ Description: Check the names of predicate methods.
59
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
60
+ Enabled: true
61
+ NamePrefix:
62
+ - is_
63
+ - has_
64
+ - have_
65
+ NamePrefixBlacklist:
66
+ - is_
67
+ Exclude:
68
+ - spec/**/*
69
+ Style/RaiseArgs:
70
+ Description: Checks the arguments passed to raise/fail.
71
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
72
+ Enabled: false
73
+ EnforcedStyle: exploded
74
+ SupportedStyles:
75
+ - compact
76
+ - exploded
77
+ Style/SignalException:
78
+ Description: Checks for proper usage of fail and raise.
79
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
80
+ Enabled: false
81
+ EnforcedStyle: semantic
82
+ SupportedStyles:
83
+ - only_raise
84
+ - only_fail
85
+ - semantic
86
+ Style/SingleLineBlockParams:
87
+ Description: Enforces the names of some block params.
88
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
89
+ Enabled: false
90
+ Methods:
91
+ - reduce:
92
+ - a
93
+ - e
94
+ - inject:
95
+ - a
96
+ - e
97
+ Style/SingleLineMethods:
98
+ Description: Avoid single-line methods.
99
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
100
+ Enabled: false
101
+ AllowIfMethodIsEmpty: true
102
+ Style/StringLiterals:
103
+ Description: Checks if uses of quotes match the configured preference.
104
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
105
+ Enabled: true
106
+ EnforcedStyle: double_quotes
107
+ SupportedStyles:
108
+ - single_quotes
109
+ - double_quotes
110
+ Style/StringLiteralsInInterpolation:
111
+ Description: Checks if uses of quotes inside expressions in interpolated strings
112
+ match the configured preference.
113
+ Enabled: true
114
+ EnforcedStyle: single_quotes
115
+ SupportedStyles:
116
+ - single_quotes
117
+ - double_quotes
118
+ Style/TrailingCommaInLiteral:
119
+ Description: Checks for trailing comma in parameter lists and literals.
120
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
121
+ Enabled: false
122
+ EnforcedStyleForMultiline: no_comma
123
+ SupportedStyles:
124
+ - comma
125
+ - no_comma
126
+ Metrics/AbcSize:
127
+ Description: A calculated magnitude based on number of assignments, branches, and
128
+ conditions.
129
+ Enabled: false
130
+ Max: 15
131
+ Metrics/ClassLength:
132
+ Description: Avoid classes longer than 100 lines of code.
133
+ Enabled: false
134
+ CountComments: false
135
+ Max: 100
136
+ Metrics/ModuleLength:
137
+ CountComments: false
138
+ Max: 100
139
+ Description: Avoid modules longer than 100 lines of code.
140
+ Enabled: false
141
+ Metrics/CyclomaticComplexity:
142
+ Description: A complexity metric that is strongly correlated to the number of test
143
+ cases needed to validate a method.
144
+ Enabled: false
145
+ Max: 6
146
+ Metrics/MethodLength:
147
+ Description: Avoid methods longer than 10 lines of code.
148
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
149
+ Enabled: false
150
+ CountComments: false
151
+ Max: 10
152
+ Metrics/ParameterLists:
153
+ Description: Avoid parameter lists longer than three or four parameters.
154
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
155
+ Enabled: false
156
+ Max: 5
157
+ CountKeywordArgs: true
158
+ Metrics/PerceivedComplexity:
159
+ Description: A complexity metric geared towards measuring complexity for a human
160
+ reader.
161
+ Enabled: false
162
+ Max: 7
163
+ Lint/AssignmentInCondition:
164
+ Description: Don't use assignment in conditions.
165
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
166
+ Enabled: false
167
+ AllowSafeAssignment: true
168
+ Style/InlineComment:
169
+ Description: Avoid inline comments.
170
+ Enabled: false
171
+ Style/AccessorMethodName:
172
+ Description: Check the naming of accessor methods for get_/set_.
173
+ Enabled: false
174
+ Style/Alias:
175
+ Description: Use alias_method instead of alias.
176
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
177
+ Enabled: false
178
+ Style/Documentation:
179
+ Description: Document classes and non-namespace modules.
180
+ Enabled: false
181
+ Style/DoubleNegation:
182
+ Description: Checks for uses of double negation (!!).
183
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
184
+ Enabled: false
185
+ Style/EachWithObject:
186
+ Description: Prefer `each_with_object` over `inject` or `reduce`.
187
+ Enabled: false
188
+ Style/EmptyLiteral:
189
+ Description: Prefer literals to Array.new/Hash.new/String.new.
190
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
191
+ Enabled: false
192
+ Style/ModuleFunction:
193
+ Description: Checks for usage of `extend self` in modules.
194
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
195
+ Enabled: false
196
+ Style/OneLineConditional:
197
+ Description: Favor the ternary operator(?:) over if/then/else/end constructs.
198
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
199
+ Enabled: false
200
+ Style/PerlBackrefs:
201
+ Description: Avoid Perl-style regex back references.
202
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
203
+ Enabled: false
204
+ Style/Send:
205
+ Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
206
+ may overlap with existing methods.
207
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
208
+ Enabled: false
209
+ Style/SpecialGlobalVars:
210
+ Description: Avoid Perl-style global variables.
211
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
212
+ Enabled: false
213
+ Style/VariableInterpolation:
214
+ Description: Don't interpolate global, instance and class variables directly in
215
+ strings.
216
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
217
+ Enabled: false
218
+ Style/WhenThen:
219
+ Description: Use when x then ... for one-line cases.
220
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
221
+ Enabled: false
222
+ Lint/EachWithObjectArgument:
223
+ Description: Check for immutable argument given to each_with_object.
224
+ Enabled: true
225
+ Lint/HandleExceptions:
226
+ Description: Don't suppress exception.
227
+ StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
228
+ Enabled: false
229
+ Lint/LiteralInCondition:
230
+ Description: Checks of literals used in conditions.
231
+ Enabled: false
232
+ Lint/LiteralInInterpolation:
233
+ Description: Checks for literals used in interpolation.
234
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at femi.senjobi@andela.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rollerskates.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Femi Senjobi
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Rollerskates [![Code Climate](https://codeclimate.com/github/andela-fsenjobi/rollerskates/badges/gpa.svg)](https://codeclimate.com/github/andela-fsenjobi/rollerskates) [![Build Status](https://semaphoreci.com/api/v1/femisenjobi/rollerskates/branches/master/badge.svg)](https://semaphoreci.com/femisenjobi/rollerskates) [![Coverage Status](https://coveralls.io/repos/github/andela-fsenjobi/rollerskates/badge.svg?branch=master)](https://coveralls.io/github/andela-fsenjobi/rollerskates?branch=master)
2
+
3
+ You all have heard about rails, right? (obviously). Rails is a gigantic framework capable of supporting massive projects. Like its name, it can support massive trains (trains) with tons of weight (requirements). Here is rollerskates, a micro MVC built with ruby and is just capable of getting you from here to there.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rollerskates'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install rollerskates
21
+
22
+ ## Usage
23
+
24
+ ### Setup
25
+ Ensure that your folder structure follow this pattern:
26
+
27
+ ```
28
+ app_name
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
+ ### Application.rb
44
+ Set up your application this way:
45
+
46
+ ```ruby
47
+ require 'rollerskates'
48
+
49
+ module Todolist
50
+ class Application < Rollerskates::Application
51
+ end
52
+ end
53
+ ```
54
+ Allow your application class to inherit form `Rollerskates::Application` class
55
+
56
+ ### Config.ru
57
+ Set up your routes this way:
58
+
59
+ ```ruby
60
+ require "/config/application.rb"
61
+ TodoApplication = Todolist::Application.new
62
+ use Rack::MethodOverride
63
+ require "/config/routes.rb"
64
+ run TodoApplication
65
+ ```
66
+
67
+ ### Routes
68
+
69
+ ```ruby
70
+ TodoApplication.pot.prepare do
71
+ get "/", to: "welcome#index"
72
+ resources :lists
73
+ resources :items, only: :index
74
+ end
75
+
76
+ ```
77
+ `resources` can also accomodate the only option.
78
+
79
+ ### Controllers
80
+
81
+ ```ruby
82
+ class ItemsController < Rollerskates::BaseController
83
+ def index
84
+ @items = Item.all
85
+ end
86
+
87
+ def new
88
+ end
89
+
90
+ def show
91
+ @item = Item.find(params["id"])
92
+ render :show_full
93
+ end
94
+ end
95
+
96
+ ```
97
+ Just like rails, controller action can take optional `render`. If the view to render is not stated explicitly, Rollerskates will render a view with the corresponding contoller action name.
98
+
99
+ ### Models
100
+
101
+ ```ruby
102
+ class Item < Rollerskates::BaseRecord
103
+ property :name, type: :text, nullable: false
104
+ property :status, type: :boolean, nullable: false
105
+
106
+ create_table
107
+ end
108
+ ```
109
+ Rolletskates automatically adds the `id, created_at and updated_at` fields and thus need not to be specified in the model. Other properties, however should be specified as above.
110
+
111
+ ### Views
112
+
113
+ ```
114
+
115
+ └───views
116
+ | └───items
117
+ | |____ new.erb
118
+ | |____ show.erb
119
+
120
+ ```
121
+ Files in the view folder should be organized according to the controller and action name.
122
+ ## Testing
123
+
124
+ The tests could be run automatically by using
125
+
126
+ ```bash
127
+ $ rspec
128
+ ```
129
+
130
+ ## Contributing
131
+
132
+ Bug reports and pull requests are welcome on GitHub at https://github.com/andela-fsenjobi/rollerskates. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
133
+
134
+
135
+ ## License
136
+
137
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'rollerskates'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,7 @@
1
+ dependencies:
2
+ post:
3
+ - gem install mime-types-data -v '3.2016.0521'
4
+
5
+ machine:
6
+ ruby:
7
+ version: 2.3.0
data/db/app.db ADDED
Binary file
@@ -0,0 +1,23 @@
1
+ module Rollerskates
2
+ class Application
3
+ attr_reader :routes
4
+
5
+ def initialize
6
+ @routes = Routing::Router.new
7
+ end
8
+
9
+ def call(env)
10
+ @request = Rack::Request.new(env)
11
+ route = mapper.map_to_route(@request)
12
+ if route
13
+ response = route.dispatch
14
+ return response.finish(route.method_name)
15
+ end
16
+ [404, {}, ["Route not found"]]
17
+ end
18
+
19
+ def mapper
20
+ @mapper ||= Routing::Mapper.new(routes.endpoints)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ require "erb"
2
+ require "tilt"
3
+
4
+ module Rollerskates
5
+ class BaseController
6
+ def initialize(request)
7
+ @request ||= request
8
+ end
9
+
10
+ def headers
11
+ { "Content-type" => "text/html" }
12
+ end
13
+
14
+ def response(body = [], status = 200, header = headers)
15
+ @response = Rack::Response.new(body, status, header)
16
+ end
17
+
18
+ def redirect_to(path, status)
19
+ response([], status[:status], "Location" => path)
20
+ end
21
+
22
+ def params
23
+ @request.params
24
+ end
25
+
26
+ def get_response
27
+ @response
28
+ end
29
+
30
+ def render(*args)
31
+ response(render_template(*args))
32
+ end
33
+
34
+ def render_template(view_name, _locals = {})
35
+ views_folder = [APP_ROOT, "app", "views"].join("/")
36
+ file_name = File.join(views_folder, controller_name, "#{view_name}.erb")
37
+ layout_name = File.join(views_folder, "layouts", "application.erb")
38
+ layout = Tilt.new(layout_name)
39
+ template = Tilt.new(file_name)
40
+ layout.render(self) { template.render(self) }
41
+ end
42
+
43
+ def finish(method_name, _status = nil)
44
+ render(method_name, {}) unless get_response
45
+ get_response
46
+ end
47
+
48
+ def controller_name
49
+ self.class.to_s.gsub(/Controller$/, "").snakize
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,116 @@
1
+ require "rollerskates/helpers/model_helper"
2
+
3
+ module Rollerskates
4
+ class BaseModel < Rollerskates::ModelHelper
5
+ def initialize(values = {})
6
+ hash_to_properties(values) unless values.empty?
7
+ end
8
+
9
+ def hash_to_properties(hash)
10
+ hash.each do |column, value|
11
+ instance_variable_set("@#{column}", value)
12
+ end
13
+ end
14
+
15
+ def update(hash)
16
+ hash_to_properties(hash)
17
+ @updated_at = Time.now.to_s
18
+ end
19
+
20
+ def save
21
+ create_columns_placeholders_values
22
+ if id
23
+ database.execute(update_query, values_for_update)
24
+ else
25
+ add_created_at_and_updated_at
26
+ database.execute "INSERT INTO #{table_name} (#{@columns.join(', ')})\
27
+ VALUES (#{@placeholders.join(', ')})", @values
28
+ end
29
+ end
30
+
31
+ def placeholders_for_update
32
+ placeholders = @columns.map { |col| col + " = ?" }
33
+ placeholders.join(", ")
34
+ end
35
+
36
+ def values_for_update
37
+ @values << id
38
+ end
39
+
40
+ def create_columns_placeholders_values
41
+ @model = self
42
+ @columns = []
43
+ @placeholders = []
44
+ @values = []
45
+
46
+ all_columns.each do |column|
47
+ value = @model.send(column)
48
+ next unless value
49
+ @columns << column.to_s
50
+ @placeholders << "?"
51
+ @values << value
52
+ end
53
+ end
54
+
55
+ def add_created_at_and_updated_at
56
+ @columns << %w(created_at updated_at)
57
+ @placeholders << ["?", "?"]
58
+ @values << [Time.now.to_s, Time.now.to_s]
59
+ end
60
+
61
+ def self.create(values)
62
+ new(values).save
63
+ end
64
+
65
+ def self.all
66
+ data = database.execute "SELECT #{all_columns.join(', ')}\
67
+ FROM #{table_name}"
68
+ data.map do |row|
69
+ row_to_object(row)
70
+ end
71
+ end
72
+
73
+ def self.count
74
+ data = database.execute "SELECT COUNT(*) FROM #{table_name}"
75
+ data.flatten.first
76
+ end
77
+
78
+ def self.first
79
+ data = database.execute "SELECT #{all_columns.join(', ')} \
80
+ FROM #{table_name} ORDER BY id ASC LIMIT 1"
81
+ row_to_object(data.flatten)
82
+ end
83
+
84
+ def self.last
85
+ data = database.execute "SELECT #{all_columns.join(', ')} \
86
+ FROM #{table_name} ORDER BY id DESC LIMIT 1"
87
+ row_to_object(data.flatten)
88
+ end
89
+
90
+ def self.find(id)
91
+ data = database.execute "SELECT #{all_columns.join(', ')}\
92
+ FROM #{table_name} WHERE id = ?", id
93
+ row_to_object data.flatten
94
+ end
95
+
96
+ def destroy
97
+ database.execute "DELETE FROM #{table_name} WHERE id = ?", id
98
+ end
99
+
100
+ def self.destroy(id)
101
+ database.execute "DELETE FROM #{table_name} WHERE id = ?", id
102
+ end
103
+
104
+ def self.destroy_all
105
+ database.execute "DELETE FROM #{table_name}"
106
+ end
107
+
108
+ def self.row_to_object(row)
109
+ model = model_name.new
110
+ all_columns.each_with_index do |attribute, index|
111
+ model.send("#{attribute}=", row[index])
112
+ end
113
+ model
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,6 @@
1
+ class Object
2
+ def self.const_missing(const)
3
+ require const.to_s.snakize
4
+ Object.const_get const
5
+ end
6
+ end
@@ -0,0 +1,92 @@
1
+ require "sqlite3"
2
+
3
+ module Rollerskates
4
+ class ModelHelper
5
+ class << self; attr_accessor :properties, :db; end
6
+
7
+ def database
8
+ @db ||= SQLite3::Database.new File.join("db", "app.db")
9
+ end
10
+
11
+ def add_property(property)
12
+ self.class.properties ||= [
13
+ "id integer PRIMARY KEY AUTOINCREMENT",
14
+ "created_at datetime NOT NULL",
15
+ "updated_at datetime NOT NULL"
16
+ ]
17
+ self.class.properties << property
18
+ end
19
+
20
+ def self.property(field, options)
21
+ new.add_property "#{field} #{parse_constraints(options)}"
22
+ end
23
+
24
+ def self.create_table
25
+ query = "CREATE TABLE IF NOT EXISTS #{table_name}\
26
+ (#{properties.join(', ')})"
27
+ database.execute(query)
28
+
29
+ all_columns.each { |var| attr_accessor var }
30
+ end
31
+
32
+ def self.parse_constraints(constraints)
33
+ attributes = ""
34
+ constraints.each do |attr, value|
35
+ attributes += send(attr.to_s, value)
36
+ end
37
+
38
+ attributes
39
+ end
40
+
41
+ def self.type(value)
42
+ "#{value.to_s.upcase} "
43
+ end
44
+
45
+ def self.primary_key(value)
46
+ return "PRIMARY KEY " if value
47
+ " "
48
+ end
49
+
50
+ def self.nullable(value)
51
+ return "NOT NULL " if value
52
+ "NULL "
53
+ end
54
+
55
+ def self.default(value)
56
+ "DEFAULT `#{value}` "
57
+ end
58
+
59
+ def self.auto_increment(value)
60
+ "AUTOINCREMENT " if value
61
+ end
62
+
63
+ def update_query
64
+ "UPDATE #{table_name} SET #{placeholders_for_update} WHERE id = ?"
65
+ end
66
+
67
+ def table_name
68
+ self.class.to_s.downcase.pluralize
69
+ end
70
+
71
+ def model_name
72
+ self.class
73
+ end
74
+
75
+ def self.method_missing(method, *args)
76
+ new.send(method, *args)
77
+ end
78
+
79
+ def all_columns
80
+ @all_columns ||= database.prepare "SELECT * FROM #{table_name}"
81
+ @all_columns.columns.map(&:to_sym)
82
+ end
83
+
84
+ def method_missing(method, *args)
85
+ @model.send(method, *args)
86
+ end
87
+
88
+ def self.all_columns
89
+ new.all_columns
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,31 @@
1
+ module Rollerskates
2
+ module Routing
3
+ class Mapper
4
+ def initialize(endpoints)
5
+ @endpoints = endpoints
6
+ end
7
+
8
+ def map_to_route(request)
9
+ @request = request
10
+ path = request.path_info
11
+ path[-1] = "" if path[-1] == "/" && path != "/"
12
+ method = request.request_method.downcase.to_sym
13
+ result = @endpoints[method].detect do |endpoint|
14
+ match_path_with_pattern path, endpoint
15
+ end
16
+ return Route.new(@request, result[:klass_and_method]) if result
17
+ end
18
+
19
+ def match_path_with_pattern(path, endpoint)
20
+ regexp, placeholders = endpoint[:pattern]
21
+ if path =~ regexp
22
+ match_data = Regexp.last_match
23
+ placeholders.each do |placeholder|
24
+ @request.update_param(placeholder, match_data[placeholder])
25
+ end
26
+ true
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ module Rollerskates
2
+ module Routing
3
+ class Route
4
+ attr_reader :klass_name, :request, :method_name
5
+
6
+ def initialize(request, klass_and_method)
7
+ @klass_name, @method_name = klass_and_method
8
+ @request = request
9
+ end
10
+
11
+ def klass
12
+ klass_name.constantize
13
+ end
14
+
15
+ def dispatch
16
+ response = klass.new(request)
17
+ response.send(method_name)
18
+ response
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,95 @@
1
+ module Rollerskates
2
+ module Routing
3
+ class Router
4
+ def draw(&block)
5
+ instance_eval(&block)
6
+ end
7
+
8
+ [:get, :post, :put, :patch, :delete].each do |method_name|
9
+ define_method(method_name) do |path, options|
10
+ path = "/#{path}" unless path[0] == "/"
11
+ klass_and_method = controller_and_action_for(options[:to])
12
+ @route_data = { path: path,
13
+ pattern: pattern_for(path.dup),
14
+ klass_and_method: klass_and_method
15
+ }
16
+ endpoints[method_name] << @route_data
17
+ end
18
+ end
19
+
20
+ def default_actions
21
+ [
22
+ { action: :index, method: :get },
23
+ { action: :create, method: :post },
24
+ { action: :new, method: :get, suffix: :new },
25
+ { action: :show, method: :get, placeholder: :id },
26
+ { action: :edit, method: :get, placeholder: :id, suffix: :edit },
27
+ { action: :destroy, method: :delete, placeholder: :id },
28
+ { action: :update, method: :put, placeholder: :id },
29
+ { action: :update, method: :patch, placeholder: :id }
30
+ ]
31
+ end
32
+
33
+ def actions(options = {})
34
+ return resources_only(options[:only]) if options[:only]
35
+ return resources_except(options[:except]) if options[:except]
36
+ default_actions
37
+ end
38
+
39
+ def resources_only(*options)
40
+ default_actions.select do |action|
41
+ options.include? action[:action]
42
+ end
43
+ end
44
+
45
+ def resources_except(*options)
46
+ default_actions.reject do |action|
47
+ options.include? action[:action]
48
+ end
49
+ end
50
+
51
+ def method_path_to(route, controller_name)
52
+ suffix = route[:suffix] ? "/#{route[:suffix]}" : ""
53
+ placeholder = route[:placeholder] ? "/:#{route[:placeholder]}" : ""
54
+ [
55
+ route[:method],
56
+ "/#{controller_name}#{placeholder}#{suffix}",
57
+ "#{controller_name}##{route[:action]}"
58
+ ]
59
+ end
60
+
61
+ def resources(controller_name, options = {})
62
+ actions(options).each do |route|
63
+ method, path, action = method_path_to(route, controller_name)
64
+ send(method, path, to: action)
65
+ end
66
+ end
67
+
68
+ def root(location)
69
+ get "/", to: location
70
+ end
71
+
72
+ def endpoints
73
+ @endpoints ||= Hash.new { |hash, key| hash[key] = [] }
74
+ end
75
+
76
+ private
77
+
78
+ def pattern_for(path)
79
+ placeholders = []
80
+ pattern = path.to_s.gsub!(/(:\w+)/) do |match|
81
+ placeholders << match[1..-1].freeze
82
+ "(?<#{placeholders.last}>[^/?#]+)"
83
+ end
84
+ pattern = pattern ? pattern : path
85
+ [/^#{pattern}$/, placeholders]
86
+ end
87
+
88
+ def controller_and_action_for(path_to)
89
+ controller_path, action = path_to.split('#')
90
+ controller = "#{controller_path.camelize}Controller"
91
+ [controller, action.to_sym]
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,33 @@
1
+ class String
2
+ def snakize
3
+ gsub!("::", "/")
4
+ gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
5
+ gsub!(/([a-z0-9])([A-Z])/, '\1_\2')
6
+ tr!("-", "_")
7
+ downcase!
8
+ self
9
+ end
10
+
11
+ def camelize
12
+ return self if self !~ /_/ && self =~ /[A-Z]+.*/
13
+ split("_").map(&:capitalize).join
14
+ end
15
+
16
+ def constantize
17
+ Object.const_get(self)
18
+ end
19
+
20
+ def pluralize
21
+ gsub!(/([^aeiouy]|qu)y$/i, '\1ies')
22
+ gsub!(/(ss|z|ch|sh|x)$/i, '\1es')
23
+ gsub!(/(is)$/i, "es")
24
+ gsub!(/(f|fe)$/i, "ves")
25
+ gsub!(/(ex|ix)$/i, "ices")
26
+ gsub!(/(a)$/i, "ae")
27
+ gsub!(/(um|on)$/i, "a")
28
+ gsub!(/(us)$/i, "i")
29
+ gsub!(/(eau)$/i, "eaux")
30
+ gsub!(/([^saeix])$/i, '\1s')
31
+ self
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Rollerskates
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,9 @@
1
+ require "rollerskates/dependencies"
2
+ require "rollerskates/utility"
3
+ require "rollerskates/version"
4
+ require "rollerskates/routing/router"
5
+ require "rollerskates/routing/route"
6
+ require "rollerskates/routing/mapper"
7
+ require "rollerskates/base_controller"
8
+ require "rollerskates/base_model"
9
+ require "rollerskates/application"
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rollerskates/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rollerskates"
8
+ spec.version = Rollerskates::VERSION
9
+ spec.authors = ["Femi Senjobi"]
10
+ spec.email = ["femi.senjobi@andela.com"]
11
+
12
+ spec.summary = "This is a micro-MVC framework"
13
+ spec.description = "This is a ruby MVC framework to speed up development time."
14
+ spec.homepage = "https://gitbub.com/andela-fsenjobi/rollerskates"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.9"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_development_dependency "coveralls"
34
+ spec.add_development_dependency "capybara"
35
+
36
+ spec.add_runtime_dependency "rack", "~> 1.0"
37
+ spec.add_runtime_dependency "tilt"
38
+ spec.add_runtime_dependency "sqlite3"
39
+ end
metadata ADDED
@@ -0,0 +1,183 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollerskates
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Femi Senjobi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: capybara
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rack
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: tilt
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: This is a ruby MVC framework to speed up development time.
126
+ email:
127
+ - femi.senjobi@andela.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".coveralls.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".travis.yml"
137
+ - CODE_OF_CONDUCT.md
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - circle.yml
145
+ - db/app.db
146
+ - lib/rollerskates.rb
147
+ - lib/rollerskates/application.rb
148
+ - lib/rollerskates/base_controller.rb
149
+ - lib/rollerskates/base_model.rb
150
+ - lib/rollerskates/dependencies.rb
151
+ - lib/rollerskates/helpers/model_helper.rb
152
+ - lib/rollerskates/routing/mapper.rb
153
+ - lib/rollerskates/routing/route.rb
154
+ - lib/rollerskates/routing/router.rb
155
+ - lib/rollerskates/utility.rb
156
+ - lib/rollerskates/version.rb
157
+ - rollerskates.gemspec
158
+ homepage: https://gitbub.com/andela-fsenjobi/rollerskates
159
+ licenses:
160
+ - MIT
161
+ metadata:
162
+ allowed_push_host: https://rubygems.org
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.4.5.1
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: This is a micro-MVC framework
183
+ test_files: []