eredor 0.2.0 → 0.3.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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eredor/version.rb +1 -1
  3. data/lib/eredor.rb +116 -37
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '07877c607d2d4cd1c6ad3bf504d48f8cc19c70bd6cfacfe344dd17ac2d479796'
4
- data.tar.gz: cee85827f625435e7e93a205c3ba50403ca68cdb8794fb49bb5f5bf72051ff63
3
+ metadata.gz: a4d71c085a6c5161a99c59ff88dc83cf024abd1d71ddc259ed6416546f98d0f9
4
+ data.tar.gz: 99cbf359ce2aaad64850be06c52d259f19646cdcb9484a899ced64dce6c56c6c
5
5
  SHA512:
6
- metadata.gz: 64e1c90f1d916710ba2af39917f1dcb59fe7ea381cc733a538e24ced5dad8c3e8aa4e432686aea5663dd05bbb8b589fc47849ced65f22c5909442c5bced64032
7
- data.tar.gz: 4a68d9b223c815b08c367bf932dbf2cde65212ba6bd39a91abbfc616c50d892708e57445acab89122f581eeb91cd55d32fe4072ba86b101a030255d029109dd0
6
+ metadata.gz: 5906e61700d7303b309a3d6a04bd45cb4af787fb7ba6c0527acc42dc6bbdf3070f1f6e5537efd69b4ab2e62d38b40a68e160a2461288dfaad23e93840930af80
7
+ data.tar.gz: d68fdc74f35c0c396e0eb9dfac7513670e57dee1508f21d78951dca4ba9909bb3ebb86b882e25d187a1d73ecae2cda847f436cd29957c08e8dbc308a9b9516e4
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Eredor
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/eredor.rb CHANGED
@@ -1,6 +1,9 @@
1
- # frozen_string_literal: true
1
+ # frozen_string_literal: false
2
2
 
3
+ require "pg"
4
+ require "dotenv/load"
3
5
  require_relative "eredor/version"
6
+ require "singleton"
4
7
 
5
8
  module Eredor
6
9
  class Error < StandardError; end
@@ -26,18 +29,17 @@ module Eredor
26
29
  def handle
27
30
  method = @request.request_method.to_sym
28
31
  path = @request.path
29
- body = %w[POST PUT PATCH].include?(method.to_s) ? @request.POST : nil
30
- query = @request.GET
32
+ params = @request.params
31
33
 
32
34
  @routes[method].each do |route|
33
35
  if route[:pattern]
34
36
  match = route[:pattern].match(path)
35
37
  next unless match
36
38
 
37
- params = extract_params(match, route[:keys])
38
- return call_handler(route, params, body, query)
39
+ params.merge!(extract_params(match))
40
+ return call_handler(route, params)
39
41
  elsif route[:path] == path
40
- return call_handler(route, {}, body, query)
42
+ return call_handler(route, params)
41
43
  end
42
44
  end
43
45
 
@@ -49,34 +51,23 @@ module Eredor
49
51
  def register_route(method, path, &block)
50
52
  raise(StandardError, "A block must be given") unless block_given?
51
53
 
52
- keys = []
53
- regex_path = path.gsub(/:(\w+)/) do |match|
54
- keys << match[1..-1].to_sym
55
- "(?<#{keys.last}>[^/]+)"
56
- end
57
-
58
- route = { path: path, handler: block }
59
- route[:pattern] = /\A#{regex_path}\z/ if keys.any?
60
- route[:keys] = keys if keys.any?
54
+ regex_path = path.gsub(/:(\w+)/) { |m| "(?<#{m[1..-1]}>[^/]+)" }
55
+ pattern = /\A#{regex_path}\z/
61
56
 
62
- @routes[method] << route
57
+ @routes[method] << {
58
+ path: path,
59
+ pattern: path.include?(":") ? pattern : nil,
60
+ handler: block
61
+ }
63
62
  end
64
63
 
65
- def extract_params(match, keys)
66
- keys.each_with_object({}) do |key, h|
67
- h[key] = match[key.to_s]
68
- end
64
+ def extract_params(match)
65
+ match.named_captures.transform_keys(&:to_sym)
69
66
  end
70
67
 
71
- def call_handler(route, params, body, query)
72
- env = {
73
- params: params,
74
- body: body,
75
- query: query
76
- }
77
- result = route[:handler].call(env)
68
+ def call_handler(route, params)
69
+ result = route[:handler].call(params)
78
70
 
79
- # Permitir retorno flexível
80
71
  case result
81
72
  when Array || Rack::Response then result # já está no formato [status, headers, body]
82
73
  when String then [200, { "content-type" => "text/html" }, [result]]
@@ -91,11 +82,11 @@ module Eredor
91
82
  end
92
83
 
93
84
  class BaseController
94
- attr_reader :params, :request
85
+ attr_reader :params
95
86
 
96
- def initialize(request)
97
- @params = request.params
98
- @request = request
87
+ def initialize(params, data_mapper)
88
+ @params = params
89
+ instance_variable_set("@#{class_name}_mapper", data_mapper)
99
90
  end
100
91
 
101
92
  def render(file)
@@ -107,13 +98,101 @@ module Eredor
107
98
  private
108
99
 
109
100
  def get_view(file)
110
- File.read("../views/#{controller_name}/#{file}.html.erb")
101
+ File.read("./app/views/#{class_name}/#{file}.html.erb")
102
+ end
103
+
104
+ def class_name
105
+ controller = self.class.name.to_s
106
+ controller.gsub(/Controller/, "").downcase
107
+ end
108
+ end
109
+
110
+ class PostgresDatabase
111
+ # include Singleton
112
+ def self.connect
113
+ connection = PG.connect(ENV["DATABASE_URL"])
114
+ connection.type_map_for_results = PG::BasicTypeMapForResults.new(connection)
115
+ connection
116
+ end
117
+ end
118
+
119
+ class DataAccessObject
120
+ def initialize(table, connection)
121
+ @connection = connection
122
+ @table_name = table
123
+
124
+ raise ArgumentError, "Table name can't be nil" if @table_name.nil?
125
+ end
126
+
127
+ def all
128
+ @connection.exec("SELECT * FROM #{@table_name}")
129
+ rescue PG::Error => e
130
+ "Query error: #{e}"
131
+ end
132
+
133
+ def where(field)
134
+ @connection.exec_params("SELECT * FROM #{@table_name} WHERE #{field.keys.first} = $1", [field.values.first])
135
+ rescue PG::Error => e
136
+ "Query error: #{e}"
137
+ end
138
+
139
+ def save(data)
140
+ placeholder = (1..data.size).map { |i| "$#{i}" }.join ", "
141
+ @connection.exec_params("INSERT INTO #{@table_name} (#{data.keys.join(", ")})
142
+ VALUES (#{placeholder})", data.values)
143
+ rescue PG::Error => e
144
+ "Query error: #{e}"
145
+ end
146
+
147
+ def update(data)
148
+ placeholder = (1..data.size).map { |i| "$#{i}" }.join ", "
149
+ @connection.exec_params("UPDATE #{@table_name} SET #{data.keys.join ", "} = #{placeholder}", data.values)
150
+ rescue PG::Error => e
151
+ "Query error: #{e}"
152
+ end
153
+
154
+ def destroy(field)
155
+ @connection.exec_params("DELETE FROM #{@table_name} WHERE #{field.keys.first} = $1", [field.values.first])
156
+ rescue PG::Error => e
157
+ "Query error: #{e}"
158
+ end
159
+ end
160
+
161
+ class Repository
162
+ def initialize(data_access_object)
163
+ @dao = data_access_object
164
+ end
165
+
166
+ def all
167
+ @dao.all.map do |row|
168
+ {
169
+ id: row["id"],
170
+ title: row["title"],
171
+ description: row["description"],
172
+ created_at: row["created_at"],
173
+ updated_at: row["updated_at"]
174
+ }
175
+ end
176
+ end
177
+
178
+ def get_by(field)
179
+ @dao.where(field).each do |row|
180
+ row.each_pair do |key, value|
181
+ @post[key.to_sym] = value
182
+ end
183
+ end
184
+ end
185
+
186
+ def save(data)
187
+ @dao.save(data)
188
+ end
189
+
190
+ def update(data)
191
+ @dao.update(data)
111
192
  end
112
193
 
113
- def controller_name
114
- controller = self.class.name
115
- controller.gsub!(/Controller/, "")
116
- controller.downcase!
194
+ def destroy(field)
195
+ @dao.destroy(field)
117
196
  end
118
197
  end
119
198
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eredor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guilherme Ribeiro
@@ -46,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  requirements: []
49
- rubygems_version: 3.7.2
49
+ rubygems_version: 3.6.9
50
50
  specification_version: 4
51
51
  summary: Minimal classes for small web applications
52
52
  test_files: []