microframe 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b26273e0e3a66da31bd5ef27ec8e035a639ef6c
4
- data.tar.gz: c783982497042b856f64fccf544c71dac1de3482
3
+ metadata.gz: a3b1e837b9d372578d058c4a3a5089f1b6745751
4
+ data.tar.gz: e3c33d4c092ddcbe0b199d32665502b596d4b5c6
5
5
  SHA512:
6
- metadata.gz: f9b7a5966a249c8567ed6fe9056874f216069e4662fe0a6dc4a2ad6f4911f00db1cf1768f0cabc4f7746cd46ae1f68d404dd7988388d24287195a08686a827db
7
- data.tar.gz: dc024b84ebcbf29345824c63640206e76ab0cbe1bba7b41a07f620dae4dd1435f2818c4658bc411ff892e0bf0cecc07b10e9220df00911fe22837d8cac9f28e9
6
+ metadata.gz: 02819a23156e8797d73e3c6a170c0babb0ccc86d57dccd6db8bdf2088bc536f7913a6737c7c2d3a3616b94a0f82058a2db5198ba9cb4937b7e41ac80074e1546
7
+ data.tar.gz: 6b191a57a39e6fe0b19883a7c14cdc1533b05b09243de2698491f287cd5f71328dce604be33c48f0379b0e933b6192055e79d36246d74dc7e20643757ed6ca49
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Microframe
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/andela-ooranagwa/microframe/badges/gpa.svg)](https://codeclimate.com/github/andela-ooranagwa/microframe) [![Test Coverage](https://codeclimate.com/github/andela-ooranagwa/microframe/badges/coverage.svg)](https://codeclimate.com/github/andela-ooranagwa/microframe/coverage)
4
+
3
5
  Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/microframe`. To experiment with that code, run `bin/console` for an interactive prompt.
4
6
 
5
7
  TODO: Delete this and the text above, and describe your gem
@@ -38,4 +40,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
38
40
  ## License
39
41
 
40
42
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
-
@@ -0,0 +1,4 @@
1
+ machine:
2
+ ruby:
3
+ version:
4
+ 2.1.7
@@ -10,11 +10,8 @@ module Microframe
10
10
 
11
11
  def call(env)
12
12
  @request = Rack::Request.new(env)
13
- verb = request.request_method
14
- path = request.path_info
15
13
  routes.request = request
16
-
17
- routes.handle_request(verb, path)
14
+ routes.handle_request
18
15
  end
19
16
  end
20
17
  end
@@ -14,21 +14,22 @@ module Microframe
14
14
  def new(name)
15
15
  @app_name = name
16
16
  directory("app_sample", "#{app_name}")
17
+ init_with_shell_cmds
17
18
  end
18
19
 
19
20
  desc "generate TYPE NAME OPTIONS", "Generates microframe resource of TYPE (i.e. model, controller or view) with the given NAME extra options specific to TYPE can also be provided"
20
21
  def generate(type, name, *xtras)
21
22
  @type = type.downcase
22
23
  @name = name.downcase
23
- @xtras = xtras
24
+ @xtras ||= xtras
24
25
 
25
26
  if type == "controller"
26
27
  template("sample_controller.tt", File.join(target_root, "controllers", "#{name}_controller.rb"))
27
- generate("view", name)
28
+ create_views
28
29
  elsif type == "model"
29
30
  template("sample_model.tt", File.join(target_root, "models", "#{name}.rb"))
30
31
  elsif type == "view"
31
- directory("folder", File.join(target_root, "views", "name"))
32
+ create_views
32
33
  end
33
34
  end
34
35
 
@@ -43,5 +44,15 @@ module Microframe
43
44
  "app/"
44
45
  end
45
46
 
47
+ def create_views
48
+ empty_directory(File.join(target_root, "views", name))
49
+ xtras.each { |f| template("blank.tt", File.join(target_root, "views", name, "#{f}.html.erb")) }
50
+ end
51
+
52
+ def init_with_shell_cmds
53
+ Dir.chdir(app_name)
54
+ run "bundle install"
55
+ run "git init"
56
+ end
46
57
  end
47
58
  end
@@ -1,9 +1,11 @@
1
+ APP_PATH = __dir__
2
+
1
3
  require "bundler"
2
4
  Bundler.require
3
5
 
4
6
  <%= app_name.capitalize %>Application = Microframe::Application.new
5
7
 
6
- require File.join(File.dirname(__FILE__), "config", "routes")
8
+ require File.join(APP_PATH, "config", "routes")
7
9
 
8
10
  use Rack::Reloader
9
11
  run <%= app_name.capitalize %>Application
@@ -1,4 +1,5 @@
1
1
  class <%= name.capitalize %> < Microframe::ORM::Base
2
+ property :id, type: :integer, primary_key: true
2
3
  <% if !xtras.empty? %>
3
4
  <% xtras.each do |pty| %>
4
5
  <% pty = pty.split(":") %>
@@ -1,6 +1,10 @@
1
+ require File.join(__dir__, "helpers")
2
+ require File.join(__dir__, "view_object")
3
+
1
4
  module Microframe
2
5
  class ApplicationController
3
- attr_reader :request, :params, :view_rendered, :errors, :child, :action
6
+ include Helpers
7
+ attr_reader :request, :params, :view_rendered, :errors, :child, :action, :view_vars
4
8
 
5
9
  def initialize(request, child, action)
6
10
  @request = request
@@ -13,22 +17,29 @@ module Microframe
13
17
  { view: action, layout: "application" }
14
18
  end
15
19
 
20
+ def redirect_to(location)
21
+ @view_rendered = true
22
+ [302, {"Location" => location}, []]
23
+ end
24
+
16
25
  def render(options = {})
17
26
  @view_rendered = true
18
27
  view = get_view(options[:view])
19
28
  layout = get_layout(options[:layout])
20
-
29
+ obj = set_up_view_object
30
+ status = 200
31
+
21
32
  if(render_error?(view, layout))
22
- response = Tilt.new(File.join(".", "public", "404.html.erb"))
23
- response = response.render(Object.new, errors: @errors)
33
+ response = Tilt.new(File.join(APP_PATH, "public", "404.html.erb"))
34
+ status = 404
35
+ response = response.render(obj, errors: @errors)
24
36
  else
25
37
  template = Tilt::ERBTemplate.new(layout)
26
38
  view = Tilt::ERBTemplate.new(view)
27
- vars = set_instance_variables_for_views
28
- response = template.render(self, vars){ view.render(self, vars)}
39
+ response = template.render(obj){ view.render(obj)}
29
40
  end
30
41
 
31
- [200, {}, [response]]
42
+ [status, {}, [response]]
32
43
  end
33
44
 
34
45
  def render_view
@@ -42,18 +53,25 @@ module Microframe
42
53
  @errors.size > 0
43
54
  end
44
55
 
45
- def get_view(view)
56
+ def get_view(view = nil)
46
57
  view ||= default_render_option[:view]
47
- file = File.join(".", "app", "views", child, "#{view}.html.erb")
58
+ file = File.join(APP_PATH, "app", "views", child, "#{view}.html.erb")
48
59
  unless File.file? file
49
- file = File.join(".", "app", "views", "#{view}.html.erb")
60
+ file = File.join(APP_PATH, "app", "views", "#{view}.html.erb")
50
61
  end
51
62
  file
52
63
  end
53
64
 
54
- def get_layout(layout)
65
+ def get_layout(layout = nil)
55
66
  layout ||= default_render_option[:layout]
56
- File.join(".", "app", "views", "layouts", layout + ".html.erb")
67
+ File.join(APP_PATH, "app", "views", "layouts", layout + ".html.erb")
68
+ end
69
+
70
+ def render_partial(partial)
71
+ partial = partial.split("/")
72
+ partial[-1] = "_#{partial[-1]}"
73
+ partial = Tilt::ERBTemplate.new(get_view(partial.join("/")))
74
+ partial.render(self)
57
75
  end
58
76
 
59
77
  def set_instance_variables_for_views
@@ -65,7 +83,15 @@ module Microframe
65
83
  end
66
84
 
67
85
  def protected_instance_variables_for_views
68
- [@request]
86
+ [:@request, :@action, :@view_rendered, :@child]
87
+ end
88
+
89
+ def set_up_view_object
90
+ obj = ViewObject.new(self)
91
+ obj.instance_exec(set_instance_variables_for_views) do |inst_vars|
92
+ inst_vars.each{|key, value| instance_variable_set("@#{key}", value) }
93
+ end
94
+ obj
69
95
  end
70
96
  end
71
97
  end
@@ -0,0 +1,54 @@
1
+ module Microframe
2
+ class FormHelper
3
+ attr_reader :target, :target_id, :form_started,:target_name
4
+
5
+ def initialize (target, target_link)
6
+ @target = target
7
+ @target_name = target.class.to_s.downcase
8
+ @link = target_link
9
+ @form_started = false
10
+ end
11
+
12
+ def start_form
13
+ @form_started = true
14
+ @target_id = target.id ? target.id : nil
15
+ @link ||= "/#{target_name}s/#{target_id || ""}"
16
+ "<form action='#{@link}' method='post'>"
17
+ end
18
+
19
+ def label(name)
20
+ gatekeeper "<label>#{name}</label>"
21
+ end
22
+
23
+ def text_area(name)
24
+ gatekeeper "<textarea name = '#{target_name}[#{name}]'>#{target.send(name)}</textarea>"
25
+ end
26
+
27
+ def text_field(name)
28
+ gatekeeper "<input type = 'text' name = '#{target_name}[#{name}]' value = '#{target.send(name)}'/>"
29
+ end
30
+
31
+ def submit
32
+ output = ""
33
+ output += "<input type = 'hidden' name = '_method' value = 'put'/>" if target_id
34
+ output += "<input type = 'submit' value = 'save' />"
35
+ output += "</form>"
36
+ gatekeeper output
37
+ end
38
+
39
+ def check_box(name, val=nil)
40
+ val ||= target.send(name)
41
+ gatekeeper "<input type = 'checkbox' name = '#{target_name}[#{name}]' checked = '#{val}' value = 'true'/>"
42
+ end
43
+
44
+ def hidden(val)
45
+ name = val.class.to_s.downcase
46
+ val = val.id
47
+ gatekeeper "<input type = 'hidden' name = '#{name}_id' value = '#{val}'/>"
48
+ end
49
+
50
+ def gatekeeper(output)
51
+ form_started ? output : start_form + output
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,33 @@
1
+ require File.join(__dir__, "form_helper")
2
+
3
+ module Microframe
4
+ module Helpers
5
+ def link_to(link, target, options = {})
6
+ if options[:method]
7
+ target = target.is_a?(String) ? target : "/#{target.class.to_s.downcase}s/#{target.id}"
8
+
9
+ "<form action='#{target}' method='post'><input type='hidden' name='_method' value='#{options[:method]}'/><input type='submit' value='#{link}' /></form>"
10
+ else
11
+ data_options = ""
12
+ options[:data].each { |key, val| data_options << "data-#{key}='#{val}'"} if options[:data]
13
+ "<a href='#{target}' #{data_options} >#{link}</a>"
14
+ end
15
+ end
16
+
17
+ def form_for(target, link=nil, &block)
18
+ yield(FormHelper.new(target, link))
19
+ end
20
+
21
+ def image_tag(image, ext = "png")
22
+ File.join(APP_PATH, "app", "assets", "images", "#{image}.#{ext}")
23
+ end
24
+
25
+ def javascript_tag(js)
26
+ File.join(APP_PATH, "app", "assets", "javascripts", "#{js}.js")
27
+ end
28
+
29
+ def stylesheet_tag(style, ext = "css")
30
+ File.join(APP_PATH, "app", "assets", "stylesheets", "#{style}.#{ext}")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ module Microframe
2
+ class ViewObject
3
+ include Helpers
4
+ def initialize(obj)
5
+ @main_obj = obj
6
+ end
7
+
8
+ def render_partial(option)
9
+ @main_obj.render_partial(option)
10
+ end
11
+ end
12
+ end
@@ -1,15 +1,11 @@
1
1
  require File.join(__dir__,"class_queries")
2
- Dir[File.join(".", "app", "models", "*.rb")].each { |file| require file}
2
+ require File.join(__dir__,"instance_queries")
3
+
4
+ Dir[File.join(APP_PATH, "app", "models", "*.rb")].each { |file| require file}
3
5
 
4
6
  module Microframe
5
7
  module ORM
6
8
  class Base
7
-
8
- def update_queryset(key, value)
9
- @queryset ||= Hash.new(Array.new)
10
- @queryset[key] = @queryset[key] << value
11
- end
12
-
13
9
  def table_name
14
10
  self.class.table_name
15
11
  end
@@ -1,42 +1,42 @@
1
- require File.join(__dir__,"query_utils")
2
- require File.join(__dir__,"instance_queries")
1
+ require File.join(__dir__,"queryset")
3
2
  require File.join(__dir__,"relationships")
4
3
 
5
4
  module Microframe
6
5
  module ORM
7
6
  class Base
8
- @@queryset = {}
7
+ @@create_table_query = []
9
8
  class << self
10
- include QueryUtils
11
9
  include Relationships
12
10
 
13
11
  def inherited(base)
14
12
  base.include InstanceQueries
15
- base.include QueryUtils
16
13
  end
17
14
 
18
15
  def property(col_name, options = {})
19
- @@create_table_query ||= []
20
16
  options[:type] = options[:type].to_s.upcase
21
17
  options[:nullable] = options[:nullable] ? "NULL" : "NOT NULL"
22
18
  options[:primary_key] = options[:primary_key] ? "PRIMARY KEY AUTOINCREMENT" : ""
23
- @@create_table_query << col_name.to_s + " " + options.values.join(" ")
19
+ @@create_table_query << (col_name.to_s + " " + options.values.join(" "))
20
+ end
21
+
22
+ def get_create_table_query
23
+ @@create_table_query
24
24
  end
25
25
 
26
26
  def create_table
27
27
  query = "CREATE TABLE IF NOT EXISTS #{table_name} (#{@@create_table_query.join(", ")})"
28
28
  if Connection.execute(query)
29
- @@create_table_query = nil
29
+ @@create_table_query = []
30
30
  define_attributes
31
31
  end
32
32
  end
33
33
 
34
34
  def define_attributes
35
+ save_data = []
35
36
  Connection.retrieve_columns(table_name).each do |column|
37
+ save_data << column
36
38
  define_method("#{column}=") do |val|
37
39
  instance_var = "@#{column}"
38
- instance_variable_set("@save_queryset", {}) unless @save_queryset
39
- @save_queryset[column] = val unless column == "id"
40
40
  instance_variable_set(instance_var, val)
41
41
  end
42
42
 
@@ -45,6 +45,10 @@ module Microframe
45
45
  instance_variable_get(instance_var)
46
46
  end
47
47
  end
48
+
49
+ define_method("models_columns") do
50
+ save_data
51
+ end
48
52
  end
49
53
 
50
54
  def create(options={})
@@ -52,7 +56,7 @@ module Microframe
52
56
  values = options.values
53
57
  placeholders = Array.new(values.size, "?").join(", ")
54
58
  Connection.connect.execute("INSERT INTO #{table_name} (#{keys}) VALUES (#{placeholders})", values)
55
- self
59
+ self.last
56
60
  end
57
61
 
58
62
  def table_name
@@ -60,51 +64,43 @@ module Microframe
60
64
  end
61
65
 
62
66
  def all
63
- add_query("SELECT", "*") unless @@queryset && @@queryset["SELECT"]
64
- fetch_result
67
+ init_queryset(:select, "*").fetch
65
68
  end
66
69
 
67
70
  def find(id)
68
71
  find_by(id: id)
69
72
  end
70
73
 
71
- def find_by(options = {})
72
- where(options)
74
+ def find_by(options)
75
+ init_queryset(:find_by, options).fetch.first
73
76
  end
74
77
 
75
- def where(options = {})
76
- sql = ""
77
- options.each {|key, val| sql << "#{key.to_s} = '#{val}'"}
78
- add_query("WHERE", sql)
78
+ def where(options)
79
+ init_queryset(:where, options)
79
80
  end
80
81
 
81
82
  def select(val)
82
- add_query("SELECT", val)
83
+ init_queryset(:select, val)
83
84
  end
84
85
 
85
86
  def count
86
- result = all
87
- result.size
87
+ all.size
88
88
  end
89
89
 
90
90
  def first
91
- limit(1)
92
- fetch_result
91
+ limit(1).fetch.first
93
92
  end
94
93
 
95
94
  def last
96
- limit(1).order("id DESC")
97
- fetch_result
95
+ limit(1).order("id DESC").fetch.first
98
96
  end
99
97
 
100
98
  def limit(val)
101
- @@queryset["LIMIT"] = val
102
- self
99
+ init_queryset(:limit, val)
103
100
  end
104
101
 
105
102
  def order(val)
106
- @@queryset["ORDER BY"] = val
107
- self
103
+ init_queryset(:order, val)
108
104
  end
109
105
 
110
106
  def destroy(id)
@@ -113,19 +109,12 @@ module Microframe
113
109
 
114
110
  def destroy_all(xtra = "; VACUUM")
115
111
  query = "DELETE FROM #{table_name} #{xtra}"
116
- execute(query)
112
+ Connection.connect.execute(query)
117
113
  self
118
114
  end
119
115
 
120
- def update_queryset(key, value)
121
- @@queryset ||= {}
122
- @@queryset[key] = @@queryset[key] ? @@queryset[key] << value : [value]
123
- end
124
-
125
- def fetch_result
126
- result = process_query(@@queryset)
127
- @@queryset = {}
128
- parse_result_to_objects(result)
116
+ def init_queryset(mtd, option)
117
+ Queryset.new(self).send(mtd, option)
129
118
  end
130
119
  end
131
120
  end
@@ -1,10 +1,12 @@
1
+ require File.join(APP_PATH, "db", "config")
2
+
1
3
  module Microframe
2
4
  module ORM
3
5
  class Connection
4
6
  class << self
5
7
 
6
8
  def connect
7
- @@db ||= SQLite3::Database.open(File.join("db", @@dbname + ".sqlite"))
9
+ @@db ||= SQLite3::Database.open(File.join(APP_PATH, "db", @@dbname + ".sqlite"))
8
10
  @@db.results_as_hash = true
9
11
  connection
10
12
  end
@@ -13,10 +15,6 @@ module Microframe
13
15
  @@db.execute2("SELECT * FROM #{table} WHERE id = 0")[0]
14
16
  end
15
17
 
16
- def set_dbname(name)
17
- @@dbname = name
18
- end
19
-
20
18
  def connection
21
19
  @@db ||= connect
22
20
  end
@@ -1,41 +1,27 @@
1
1
  module Microframe
2
2
  module ORM
3
3
  module InstanceQueries
4
+ include QueryUtils
4
5
  def save
5
- @save_queryset["id"] = id
6
- keys = @save_queryset.keys.join(", ")
7
- values = @save_queryset.values
8
- @save_queryset = nil
6
+ queryset = {}
7
+ models_columns.each { |col| queryset[col] = send(col) }
8
+ keys = queryset.keys.join(", ")
9
+ values = queryset.values
9
10
  placeholders = Array.new(values.size, "?").join(", ")
10
- result = Microframe::ORM::Connection.connect.execute("REPLACE INTO #{table_name} (#{keys}) VALUES (#{placeholders})", values)
11
+ result = Connection.connection.execute("REPLACE INTO #{table_name} (#{keys}) VALUES (#{placeholders})", values)
11
12
  result ? self.class.last : self
12
13
  end
13
14
 
14
15
  def update(options = {})
15
- @save_queryset ||= {}
16
- @save_queryset.merge(options)
16
+ options.each{ |col, val| send("#{col}=", val) }
17
17
  save
18
18
  end
19
- # def order
20
- # end
21
-
22
- # def limit
23
- # end
24
-
25
- # def offset
26
- # end
27
-
28
- # def update
29
- # end
30
19
 
31
20
  def destroy
32
21
  query = "DELETE FROM #{table_name} WHERE id =#{id}"
33
- execute(query)
34
- self
22
+ execute(query)
23
+ self
35
24
  end
36
-
37
- # def group
38
- # end
39
25
  end
40
26
  end
41
27
  end
@@ -1,11 +1,6 @@
1
1
  module Microframe
2
2
  module ORM
3
3
  module QueryUtils
4
- def add_query(key, value)
5
- update_queryset(key, value)
6
- self
7
- end
8
-
9
4
  def process_query(queryset)
10
5
  return unless queryset
11
6
  query = build_query(queryset)
@@ -13,7 +8,7 @@ module Microframe
13
8
  end
14
9
 
15
10
  def execute(query)
16
- Microframe::ORM::Connection.execute(query)
11
+ Connection.execute(query)
17
12
  end
18
13
 
19
14
  def process_select(queryhash)
@@ -22,8 +17,8 @@ module Microframe
22
17
  end
23
18
 
24
19
  def process_from(queryhash)
25
- queryhash["FROM"] ||= self.to_s.downcase
26
- "FROM #{queryhash["FROM"]}s"
20
+ queryhash["FROM"] ||= table_name
21
+ "FROM #{queryhash["FROM"]}"
27
22
  end
28
23
 
29
24
  def process_where(queryhash)
@@ -57,7 +52,7 @@ module Microframe
57
52
  def parse_result_to_objects(result)
58
53
  hash_objects = []
59
54
  result.each do |hash|
60
- obj = self.new
55
+ obj = @model.new
61
56
  hash.each do |key, val|
62
57
  if key.is_a? String
63
58
  obj.instance_variable_set("@#{key}", val)
@@ -65,7 +60,7 @@ module Microframe
65
60
  end
66
61
  hash_objects << obj
67
62
  end
68
- result.size == 1 ? hash_objects.first : hash_objects
63
+ hash_objects
69
64
  end
70
65
  end
71
66
  end
@@ -0,0 +1,66 @@
1
+ require File.join(__dir__,"query_utils")
2
+
3
+ module Microframe
4
+ module ORM
5
+ class Queryset
6
+ include QueryUtils
7
+
8
+ attr_reader :queryset, :table_name
9
+
10
+ def initialize(model)
11
+ @queryset = {}
12
+ @table_name = model.table_name
13
+ @model = model
14
+ end
15
+
16
+ def where(options)
17
+ sql = []
18
+ options.each {|key, val| sql << "#{key.to_s} = '#{val}'"}
19
+ add_query("WHERE", sql.join(" AND "))
20
+ end
21
+
22
+ def all(val = "*")
23
+ add_query("SELECT", "#{val}") unless queryset["SELECT"]
24
+ fetch
25
+ end
26
+
27
+ def find(id)
28
+ where(id: id)
29
+ end
30
+
31
+ def find_by(option)
32
+ where(option)
33
+ end
34
+
35
+ def select(val)
36
+ add_query("SELECT", val)
37
+ end
38
+
39
+ def limit(val)
40
+ @queryset["LIMIT"] = val
41
+ self
42
+ end
43
+
44
+ def order(val)
45
+ @queryset["ORDER BY"] = val
46
+ self
47
+ end
48
+
49
+ def add_query(field, condition)
50
+ @queryset ||= {}
51
+ @queryset[field] = queryset[field] ? queryset[field] << condition : [condition]
52
+ self
53
+ end
54
+
55
+ def fetch
56
+ result = process_query(queryset)
57
+ @queryset = {}
58
+ parse_result_to_objects(result)
59
+ end
60
+
61
+ def load
62
+ fetch.first
63
+ end
64
+ end
65
+ end
66
+ end
@@ -12,11 +12,10 @@ module Microframe
12
12
  end
13
13
 
14
14
  def belongs_to(model, options = {})
15
- model = model.to_s
16
15
  define_method(model) do
17
16
  options[:foreign_key] ||= "id"
18
- model_id = send((model + "_id").to_sym)
19
- Module.const_get(model.capitalize).where(options[:foreign_key] => model_id )
17
+ model_id = send("#{model}_id")
18
+ Module.const_get(model.to_s.capitalize).where(options[:foreign_key] => model_id ).fetch.first
20
19
  end
21
20
  end
22
21
 
@@ -3,6 +3,7 @@ module Microframe
3
3
  attr_reader :routes, :placeholders
4
4
  def initialize(routes)
5
5
  @routes = routes
6
+ @placeholders = {};
6
7
  end
7
8
 
8
9
  def self.start(route)
@@ -11,42 +12,44 @@ module Microframe
11
12
 
12
13
  def map(verb, path)
13
14
  value = nil
14
- routes[verb].each do |route, target|
15
- if match_this(route, path)
16
- value = target
17
- break
15
+ if routes[verb]
16
+ routes[verb].each do |route, target|
17
+ if match_this(route, path)
18
+ value = target
19
+ break
20
+ end
18
21
  end
19
22
  end
20
23
  value
21
24
  end
22
25
 
23
- def match_components(a, b)
26
+ def match_components(defined_elt, incoming_elt)
24
27
  found = false
25
- if a == b
28
+ if defined_elt == incoming_elt
26
29
  found = true
27
- elsif a[0] == ":" && a[-1] != ")" && a[-1] != "("
28
- @placeholders[a[1..-1].to_sym] = b
30
+ elsif defined_elt[0] == ":" && defined_elt[-1] != ")" && defined_elt[-1] != "(" && !incoming_elt.nil?
31
+ @placeholders[defined_elt[1..-1].to_sym] = incoming_elt
29
32
  found = true
30
33
  end
31
34
  return found
32
35
  end
33
36
 
34
- def match_begin_of_optional_components(a, optional)
35
- if a[-1] == "("
37
+ def match_begin_of_optional_components(defined_elt, optional)
38
+ if defined_elt[-1] == "("
36
39
  optional += 1
37
- a.sub!("(", "")
40
+ defined_elt.sub!("(", "")
38
41
  changed = true
39
42
  end
40
- return a, optional, changed
43
+ return defined_elt, optional, changed
41
44
  end
42
45
 
43
- def match_end_of_optional_components(a, optional)
44
- if a[-1] == ")"
46
+ def match_end_of_optional_components(defined_elt, optional)
47
+ if defined_elt[-1] == ")"
45
48
  optional -= 1
46
- a.sub!(")", "")
49
+ defined_elt.sub!(")", "")
47
50
  changed = true
48
51
  end
49
- return a, optional, changed
52
+ return defined_elt, optional, changed
50
53
  end
51
54
 
52
55
  def match_optional_components(match, optional, index)
@@ -57,22 +60,22 @@ module Microframe
57
60
  return match, index, match
58
61
  end
59
62
 
60
- def match_this(routes, path)
61
- match = []; @placeholders = {}; index = 0;
62
- route = routes.split("/")
63
- paths = path.split("/")
63
+ def match_this(defined, incoming)
64
+ match = []; index = 0;
65
+ defined_route = defined.split("/")
66
+ incoming_route = incoming.split("/")
64
67
 
65
- return false if route.size < paths.size
68
+ return false if defined_route.size < incoming_route.size
66
69
 
67
- route.each do |a|
70
+ defined_route.each do |route_elt|
68
71
  pending_match = true; matched = false; optional = 0;
69
- b = paths[index]
72
+ incoming_elt = incoming_route[index]
70
73
 
71
74
  while pending_match
72
- matched = match_components(a, b)
75
+ matched = match_components(route_elt, incoming_elt)
73
76
  pending_match = false
74
- a, optional, pending_match = match_begin_of_optional_components(a, optional) unless matched || pending_match
75
- a, optional, pending_match = match_end_of_optional_components(a, optional) unless matched || pending_match
77
+ route_elt, optional, pending_match = match_begin_of_optional_components(route_elt, optional) unless matched || pending_match
78
+ route_elt, optional, pending_match = match_end_of_optional_components(route_elt, optional) unless matched || pending_match
76
79
  matched, index, pending_match = match_optional_components(matched, optional, index) unless matched || pending_match
77
80
  end
78
81
 
@@ -3,19 +3,22 @@ require File.join(__dir__, "mapper")
3
3
 
4
4
  module Microframe
5
5
  class Router
6
- attr_reader :route, :mapper, :object
6
+ attr_reader :routes, :mapper, :object
7
7
  attr_accessor :request
8
8
 
9
9
  def initialize
10
- @route = Hash.new
10
+ @routes = Hash.new
11
11
  end
12
12
 
13
- def handle_request(verb, path)
14
- @mapper ||= Mapper.start(route)
13
+ def handle_request
14
+ verb = request.request_method
15
+ path = request.path_info
16
+ @mapper ||= Mapper.start(routes)
15
17
  handler = @mapper.map(verb, path) #get_handler(verb, path)
18
+
16
19
  return missing_path unless handler
17
20
 
18
- @request.params.merge!(@mapper.placeholders)
21
+ request.params.merge!(@mapper.placeholders)
19
22
 
20
23
  response = setup_controller(handler)
21
24
  unless object.view_rendered
@@ -36,8 +39,7 @@ module Microframe
36
39
 
37
40
  def self.setup_verbs(*verbs)
38
41
  verbs.each do |verb|
39
- verb = verb.to_s
40
- define_method(verb) { |path, handler| set_route(verb.upcase, path, handler) }
42
+ define_method(verb) { |path, handler| set_route(verb.to_s.upcase, path, handler) }
41
43
  end
42
44
  end
43
45
 
@@ -45,7 +47,7 @@ module Microframe
45
47
 
46
48
  def draw(&block)
47
49
  instance_eval(&block)
48
- @route.default = {}
50
+ @routes.default = {}
49
51
  end
50
52
 
51
53
  def resources(name)
@@ -56,18 +58,19 @@ module Microframe
56
58
  get("/#{name}/:id/edit", to: "#{name}#edit")
57
59
  post("/#{name}", to: "#{name}#create")
58
60
  patch("/#{name}/:id", to: "#{name}#update")
59
- delete("/name/:id", to: "#{name}#destroy")
61
+ put("/#{name}/:id", to: "#{name}#update")
62
+ delete("/#{name}/:id", to: "#{name}#destroy")
60
63
  end
61
64
 
62
65
  private
63
66
 
64
67
  def get_handler(verb, path)
65
- route[verb][path]
68
+ routes[verb][path]
66
69
  end
67
70
 
68
71
  def set_route(verb, path, handler = {})
69
- @route[verb] ||= {}
70
- @route[verb][path] = setup_handler(handler)
72
+ @routes[verb] ||= {}
73
+ @routes[verb][path] = setup_handler(handler)
71
74
  end
72
75
 
73
76
  def setup_handler(handler)
@@ -76,7 +79,7 @@ module Microframe
76
79
  end
77
80
 
78
81
  def get_handler_file(controller)
79
- require File.join(".", "app", "controllers", controller + "_controller")
82
+ require File.join(APP_PATH, "app", "controllers", controller + "_controller")
80
83
  end
81
84
 
82
85
  def missing_path
@@ -1,3 +1,3 @@
1
1
  module Microframe
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'microframe/version'
4
+ require "microframe/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "microframe"
@@ -13,10 +13,10 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/andela-ooranagwa/microframe"
14
14
  spec.license = "MIT"
15
15
 
16
- # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
16
+ # Prevent pushing this gem to RubyGems.org by setting "allowed_push_host", or
17
17
  # delete this section to allow pushing this gem to any host.
18
18
  if spec.respond_to?(:metadata)
19
- spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
20
  else
21
21
  raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
22
  end
@@ -26,9 +26,15 @@ Gem::Specification.new do |spec|
26
26
  spec.executables = %w{microframe}
27
27
  spec.require_paths = ["lib"]
28
28
 
29
- spec.add_runtime_dependency "bundler", "~> 1.10"
29
+ spec.add_runtime_dependency "rack", "~> 1.6", ">= 1.6.4"
30
+ spec.add_runtime_dependency "bundler"
30
31
  spec.add_runtime_dependency "thor", "~> 0.19.1"
31
32
  spec.add_development_dependency "pry-nav", "~> 0.2.4"
32
- spec.add_development_dependency "rake", '~> 10.4', '>= 10.4.2'
33
- spec.add_development_dependency "minitest", '~> 5.8', '>= 5.8.3'
33
+ spec.add_development_dependency "rake", "~> 10.4", ">= 10.4.2"
34
+ spec.add_development_dependency "minitest", "~> 5.8", ">= 5.8.3"
35
+ spec.add_development_dependency "capybara", "~> 2.5", ">= 2.5.0"
36
+ spec.add_development_dependency "sqlite3", "~> 1.3", ">= 1.3.11"
37
+ spec.add_development_dependency "tilt", "~> 2.0", ">= 2.0.1"
38
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 0.4", ">= 0.4.8"
39
+ spec.add_development_dependency "simplecov", "~> 0.10", ">= 0.10.0"
34
40
  end
metadata CHANGED
@@ -1,29 +1,49 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: microframe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Osmond
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-16 00:00:00.000000000 Z
11
+ date: 2015-12-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
14
+ name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.10'
19
+ version: '1.6'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.6.4
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: '1.10'
29
+ version: '1.6'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: thor
29
49
  requirement: !ruby/object:Gem::Requirement
@@ -92,6 +112,106 @@ dependencies:
92
112
  - - ">="
93
113
  - !ruby/object:Gem::Version
94
114
  version: 5.8.3
115
+ - !ruby/object:Gem::Dependency
116
+ name: capybara
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - "~>"
120
+ - !ruby/object:Gem::Version
121
+ version: '2.5'
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 2.5.0
125
+ type: :development
126
+ prerelease: false
127
+ version_requirements: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.5'
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 2.5.0
135
+ - !ruby/object:Gem::Dependency
136
+ name: sqlite3
137
+ requirement: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - "~>"
140
+ - !ruby/object:Gem::Version
141
+ version: '1.3'
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: 1.3.11
145
+ type: :development
146
+ prerelease: false
147
+ version_requirements: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
150
+ - !ruby/object:Gem::Version
151
+ version: '1.3'
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: 1.3.11
155
+ - !ruby/object:Gem::Dependency
156
+ name: tilt
157
+ requirement: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - "~>"
160
+ - !ruby/object:Gem::Version
161
+ version: '2.0'
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: 2.0.1
165
+ type: :development
166
+ prerelease: false
167
+ version_requirements: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - "~>"
170
+ - !ruby/object:Gem::Version
171
+ version: '2.0'
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: 2.0.1
175
+ - !ruby/object:Gem::Dependency
176
+ name: codeclimate-test-reporter
177
+ requirement: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - "~>"
180
+ - !ruby/object:Gem::Version
181
+ version: '0.4'
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: 0.4.8
185
+ type: :development
186
+ prerelease: false
187
+ version_requirements: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - "~>"
190
+ - !ruby/object:Gem::Version
191
+ version: '0.4'
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: 0.4.8
195
+ - !ruby/object:Gem::Dependency
196
+ name: simplecov
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - "~>"
200
+ - !ruby/object:Gem::Version
201
+ version: '0.10'
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: 0.10.0
205
+ type: :development
206
+ prerelease: false
207
+ version_requirements: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - "~>"
210
+ - !ruby/object:Gem::Version
211
+ version: '0.10'
212
+ - - ">="
213
+ - !ruby/object:Gem::Version
214
+ version: 0.10.0
95
215
  description:
96
216
  email:
97
217
  - osmond.oranagwa@andela.com
@@ -110,6 +230,7 @@ files:
110
230
  - bin/console
111
231
  - bin/microframe
112
232
  - bin/setup
233
+ - circle.yml
113
234
  - lib/microframe.rb
114
235
  - lib/microframe/application/application.rb
115
236
  - lib/microframe/application/generators/generator.rb
@@ -124,14 +245,19 @@ files:
124
245
  - lib/microframe/application/generators/samples/app_sample/config/routes.rb.tt
125
246
  - lib/microframe/application/generators/samples/app_sample/db/config.rb.tt
126
247
  - lib/microframe/application/generators/samples/app_sample/public/404.html.erb.tt
248
+ - lib/microframe/application/generators/samples/blank.tt
127
249
  - lib/microframe/application/generators/samples/sample_controller.tt
128
250
  - lib/microframe/application/generators/samples/sample_model.tt
129
251
  - lib/microframe/controller/application_controller.rb
252
+ - lib/microframe/controller/form_helper.rb
253
+ - lib/microframe/controller/helpers.rb
254
+ - lib/microframe/controller/view_object.rb
130
255
  - lib/microframe/orm/base.rb
131
256
  - lib/microframe/orm/class_queries.rb
132
257
  - lib/microframe/orm/connection.rb
133
258
  - lib/microframe/orm/instance_queries.rb
134
259
  - lib/microframe/orm/query_utils.rb
260
+ - lib/microframe/orm/queryset.rb
135
261
  - lib/microframe/orm/relationships.rb
136
262
  - lib/microframe/routing/mapper.rb
137
263
  - lib/microframe/routing/router.rb