mvc_one 0.1.0.pre.rc3 → 0.1.0.pre.rc5

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,120 +8,124 @@
8
8
  # get, post, delete, etc - HTTP methods
9
9
  # '/user', '/users/:id' - regexp, :id - params, available with result
10
10
  # to: 'web#users#create' - route request to Web::UsersController create method
11
- class MvcOne::RegexpRouter
12
- HTTP_METHODS = %w[get post patch put delete options head].freeze
13
-
14
- # concrete route
15
- class Route
16
- ANY_METHOD = 'any'
17
- attr_reader :controller, :action, :pattern, :method
18
-
19
- def initialize(controller:, action:, pattern:, method:)
20
- @controller = controller
21
- @action = action
22
- @pattern = pattern
23
- @method = method
24
- end
11
+ module MvcOne
12
+ class RegexpRouter
13
+ HTTP_METHODS = %w[get post patch put delete options head].freeze
14
+
15
+ # concrete route
16
+ class Route
17
+ ANY_METHOD = 'any'
18
+ attr_reader :controller, :action, :pattern, :method
19
+
20
+ def initialize(controller:, action:, pattern:, method:)
21
+ @controller = controller
22
+ @action = action
23
+ @pattern = pattern
24
+ @method = method
25
+ end
25
26
 
26
- def matched?(path, http_method)
27
- return true if method == ANY_METHOD
28
- return false unless method.to_s == http_method.to_s
27
+ def matched?(path, http_method)
28
+ return true if method == ANY_METHOD
29
+ return false unless method.to_s == http_method.to_s
29
30
 
30
- pattern_split = pattern.split('/')
31
- path_split = path.split('/')
32
- return false if pattern_split.length != path_split.length
31
+ pattern_split = pattern.split('/')
32
+ path_split = path.split('/')
33
+ return false if pattern_split.length != path_split.length
33
34
 
34
- pattern_matched_with?(pattern_split, path_split)
35
- end
35
+ pattern_matched_with?(pattern_split, path_split)
36
+ end
36
37
 
37
- def params(path)
38
- pattern_split = pattern.split('/')
39
- path_split = path.split('/')
38
+ def params(path)
39
+ pattern_split = pattern.split('/')
40
+ path_split = path.split('/')
40
41
 
41
- result = {}
42
+ result = {}
42
43
 
43
- pattern_split.each_with_index do |e, index|
44
- if e.start_with?(':')
45
- key = e.delete(':').to_sym
46
- result[key] = path_split[index]
44
+ pattern_split.each_with_index do |e, index|
45
+ if e.start_with?(':')
46
+ key = e.delete(':').to_sym
47
+ result[key] = path_split[index]
48
+ end
47
49
  end
48
- end
49
50
 
50
- result
51
- end
51
+ result
52
+ end
52
53
 
53
- private
54
+ private
54
55
 
55
- def pattern_matched_with?(pattern_split, path_split)
56
- matched = true
57
- pattern_split.each_with_index do |e, index|
58
- next if e.start_with?(':')
56
+ def pattern_matched_with?(pattern_split, path_split)
57
+ matched = true
58
+ pattern_split.each_with_index do |e, index|
59
+ next if e.start_with?(':')
59
60
 
60
- if e != path_split[index]
61
- matched = false
62
- break
61
+ if e != path_split[index]
62
+ matched = false
63
+ break
64
+ end
63
65
  end
64
- end
65
66
 
66
- matched
67
+ matched
68
+ end
67
69
  end
68
- end
69
70
 
70
- # result of searching right route, has controller, action, params from path
71
- class Result
72
- attr_reader :route, :path
71
+ # result of searching right route, has controller, action, params from path
72
+ class Result
73
+ attr_reader :route, :path
73
74
 
74
- def initialize(route, path)
75
- @route = route
76
- @path = path
77
- end
75
+ def initialize(route, path)
76
+ @route = route
77
+ @path = path
78
+ end
78
79
 
79
- def controller
80
- instance_eval "#{route.controller}Controller", __FILE__, __LINE__ # Return class of matched controller
81
- end
80
+ def controller
81
+ instance_eval "#{route.controller}Controller", __FILE__, __LINE__ # Return class of matched controller
82
+ end
82
83
 
83
- def action
84
- route.action
85
- end
84
+ def action
85
+ route.action
86
+ end
86
87
 
87
- def params
88
- route.params(path)
88
+ def params
89
+ route.params(path)
90
+ end
89
91
  end
90
- end
91
92
 
92
- attr_reader :routes
93
+ attr_reader :routes
93
94
 
94
- def initialize(route_path)
95
- @routes = []
96
- load_routes(route_path)
97
- end
95
+ def initialize(route_path)
96
+ @routes = []
97
+ load_routes(route_path)
98
+ end
98
99
 
99
- def resolve(path, method)
100
- route = routes.find { |r| r.matched?(path, method.downcase) }
100
+ def resolve(path, method)
101
+ route = routes.find { |r| r.matched?(path, method.downcase) }
101
102
 
102
- Result.new(route, path) if route
103
- end
103
+ Result.new(route, path) if route
104
+ end
105
+
106
+ private
104
107
 
105
- private
108
+ def load_routes(route_path)
109
+ return unless File.file?(route_path)
106
110
 
107
- def load_routes(route_path)
108
- instance_eval(File.read(route_path), route_path.to_s).call
109
- end
111
+ instance_eval(File.read(route_path), route_path.to_s).call
112
+ end
110
113
 
111
- HTTP_METHODS.each do |method|
112
- define_method method do |pattern, options|
113
- register_route(method, pattern, options)
114
+ HTTP_METHODS.each do |method|
115
+ define_method method do |pattern, options|
116
+ register_route(method, pattern, options)
117
+ end
114
118
  end
115
- end
116
119
 
117
- def any(to:)
118
- register_route('any', '', to: to)
119
- end
120
+ def any(to:)
121
+ register_route('any', '', to: to)
122
+ end
120
123
 
121
- def register_route(method, pattern, options)
122
- path_pieces = options[:to].split('#')
123
- controller = path_pieces.slice(0..-2).map(&:capitalize).join('::')
124
- action = path_pieces.last
125
- @routes << Route.new(controller: controller, action: action, pattern: pattern, method: method)
124
+ def register_route(method, pattern, options)
125
+ path_pieces = options[:to].split('#')
126
+ controller = path_pieces.slice(0..-2).map(&:capitalize).join('::')
127
+ action = path_pieces.last
128
+ @routes << Route.new(controller: controller, action: action, pattern: pattern, method: method)
129
+ end
126
130
  end
127
131
  end
@@ -1,35 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # Base relation object
4
- class MvcOne::ApplicationRelation
5
- def self.wrap(klass)
6
- define_method :wrapped_class do
7
- instance_eval klass.to_s.capitalize, __FILE__, __LINE__ # Return wrapped class
4
+ module MvcOne
5
+ class ApplicationRelation
6
+ def self.wrap(klass)
7
+ define_method :wrapped_class do
8
+ instance_eval klass.to_s.capitalize, __FILE__, __LINE__ # Return wrapped class
9
+ end
8
10
  end
9
- end
10
11
 
11
- attr_reader :target, :attributes
12
+ attr_reader :target, :attributes
12
13
 
13
- def initialize(attributes)
14
- raise 'Please define wrapped class with wrap class method' unless respond_to?(:wrapped_class)
14
+ def initialize(attributes)
15
+ raise 'Please define wrapped class with wrap class method' unless respond_to?(:wrapped_class)
15
16
 
16
- @target = wrapped_class.new(attributes)
17
- @attributes = attributes
18
- end
17
+ @target = wrapped_class.new(attributes)
18
+ @attributes = attributes
19
+ end
19
20
 
20
- def method_missing(method)
21
- target.send(method)
22
- end
21
+ def method_missing(method)
22
+ target.send(method)
23
+ end
23
24
 
24
- def respond_to?(method)
25
- methods.include?(method) || target.respond_to?(method)
26
- end
25
+ def respond_to?(method)
26
+ methods.include?(method) || target.respond_to?(method)
27
+ end
27
28
 
28
- def respond_to_missing?(method)
29
- target.respond_to_missing?(method)
30
- end
29
+ def respond_to_missing?(method)
30
+ target.respond_to_missing?(method)
31
+ end
31
32
 
32
- def class
33
- target.class
33
+ def class
34
+ target.class
35
+ end
34
36
  end
35
37
  end
@@ -1,16 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'sequel'
4
+
3
5
  # Main gateway for persisted storage
4
- class MvcOne::ApplicationRepository
5
- class NotFoundRecord < StandardError; end
6
+ module MvcOne
7
+ class ApplicationRepository
8
+ class NotFoundRecord < StandardError; end
6
9
 
7
- attr_reader :table_name
10
+ attr_reader :table_name
8
11
 
9
- def self.db_config
10
- YAML.load_file('config/database.yml')[Application::Config.env]
11
- end
12
+ def self.db_config
13
+ Application::Config.db_config
14
+ end
12
15
 
13
- DB = Sequel.connect(db_config['db']['connection_line'])
14
- DB.loggers << Logger.new($stdout)
15
- DB.sql_log_level = :debug
16
+ DB = Sequel.connect(db_config['db']['connection_line'])
17
+ DB.loggers << Logger.new($stdout)
18
+ DB.sql_log_level = :debug
19
+ end
16
20
  end
@@ -1,141 +1,143 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # All app serializers must be subclasses of ApplicationSerializer
4
- class MvcOne::ApplicationSerializer
5
- class << self
6
- def attributes(*args)
7
- define_method :attributes do
8
- args
4
+ module MvcOne
5
+ class ApplicationSerializer
6
+ class << self
7
+ def attributes(*args)
8
+ define_method :attributes do
9
+ args
10
+ end
9
11
  end
10
- end
11
12
 
12
- def type(type)
13
- define_method :type do
14
- type.to_s
13
+ def type(type)
14
+ define_method :type do
15
+ type.to_s
16
+ end
15
17
  end
16
- end
17
18
 
18
- def id
19
- raise ArgumentError, 'No block given' unless block_given?
19
+ def id
20
+ raise ArgumentError, 'No block given' unless block_given?
20
21
 
21
- define_method :id do
22
- yield data
22
+ define_method :id do
23
+ yield data
24
+ end
23
25
  end
24
- end
25
26
 
26
- def links
27
- raise ArgumentError, 'No block given' unless block_given?
27
+ def links
28
+ raise ArgumentError, 'No block given' unless block_given?
28
29
 
29
- define_method :links do
30
- yield data
30
+ define_method :links do
31
+ yield data
32
+ end
31
33
  end
32
- end
33
34
 
34
- def has_one(relation)
35
- raise ArgumentError, 'No block given' unless block_given?
35
+ def has_one(relation)
36
+ raise ArgumentError, 'No block given' unless block_given?
36
37
 
37
- define_method relation do
38
- yield data
38
+ define_method relation do
39
+ yield data
40
+ end
39
41
  end
40
42
  end
41
- end
42
43
 
43
- attr_reader :data
44
+ attr_reader :data
44
45
 
45
- def initialize(data, include: [])
46
- @data = data
47
- @include = include
48
- end
46
+ def initialize(data, include: [])
47
+ @data = data
48
+ @include = include
49
+ end
49
50
 
50
- def serialize
51
- result = {}
52
- if data.is_a?(Enumerable)
53
- result[:data] = data.map { |e| self.class.new(e, include: @include).serialize_data }
54
- included = data.flat_map { |e| self.class.new(e, include: @include).included_serialization }.compact
55
- result[:included] = included if included.any?
56
- else
57
- result[:data] = data_serialization
58
- result[:included] = included_serialization if included_serialization
51
+ def serialize
52
+ result = {}
53
+ if data.is_a?(Enumerable)
54
+ result[:data] = data.map { |e| self.class.new(e, include: @include).serialize_data }
55
+ included = data.flat_map { |e| self.class.new(e, include: @include).included_serialization }.compact
56
+ result[:included] = included if included.any?
57
+ else
58
+ result[:data] = data_serialization
59
+ result[:included] = included_serialization if included_serialization
60
+ end
61
+ result
59
62
  end
60
- result
61
- end
62
63
 
63
- def serialize_data
64
- data_serialization
65
- end
64
+ def serialize_data
65
+ data_serialization
66
+ end
66
67
 
67
- def data_serialization
68
- raise NoMethodError if data.is_a?(Enumerable)
68
+ def data_serialization
69
+ raise NoMethodError if data.is_a?(Enumerable)
69
70
 
70
- result = {}
71
- add_type(result)
72
- add_id(result)
73
- add_attributes(result)
74
- add_links(result)
75
- add_relatioship(result)
76
- result
77
- end
71
+ result = {}
72
+ add_type(result)
73
+ add_id(result)
74
+ add_attributes(result)
75
+ add_links(result)
76
+ add_relatioship(result)
77
+ result
78
+ end
78
79
 
79
- def included_serialization
80
- return nil if @include.empty?
80
+ def included_serialization
81
+ return nil if @include.empty?
81
82
 
82
- @include.map do |include|
83
- raise(NoMethodError, "Undefined #{include} include") unless respond_to?(include)
83
+ @include.map do |include|
84
+ raise(NoMethodError, "Undefined #{include} include") unless respond_to?(include)
84
85
 
85
- send(include)
86
+ send(include)
87
+ end
86
88
  end
87
- end
88
89
 
89
- private
90
+ private
90
91
 
91
- def add_relatioship(result)
92
- result[:relationships] = {} unless @include.empty?
93
- @include.each do |include|
94
- raise(NoMethodError, "Undefined #{include} include") unless respond_to?(include)
92
+ def add_relatioship(result)
93
+ result[:relationships] = {} unless @include.empty?
94
+ @include.each do |include|
95
+ raise(NoMethodError, "Undefined #{include} include") unless respond_to?(include)
95
96
 
96
- included_entity = send(include)
97
- included_relationship = {}
98
- included_relationship[:data] = included_entity.slice(:id, :type)
99
- result[:relationships][include] = included_relationship
97
+ included_entity = send(include)
98
+ included_relationship = {}
99
+ included_relationship[:data] = included_entity.slice(:id, :type)
100
+ result[:relationships][include] = included_relationship
101
+ end
100
102
  end
101
- end
102
103
 
103
- def add_links(result)
104
- result[:links] = links if respond_to?(:links)
105
- end
104
+ def add_links(result)
105
+ result[:links] = links if respond_to?(:links)
106
+ end
106
107
 
107
- def add_type(result)
108
- result[:type] = respond_to?(:type) ? type : default_type
109
- end
108
+ def add_type(result)
109
+ result[:type] = respond_to?(:type) ? type : default_type
110
+ end
110
111
 
111
- def add_id(result)
112
- result[:id] = respond_to?(:id) ? id : default_id
113
- end
112
+ def add_id(result)
113
+ result[:id] = respond_to?(:id) ? id : default_id
114
+ end
114
115
 
115
- def add_attributes(result)
116
- return unless respond_to?(:attributes)
116
+ def add_attributes(result)
117
+ return unless respond_to?(:attributes)
117
118
 
118
- result_attributes = attributes.each_with_object({}) do |attr, acc|
119
- acc[attr] = get_attribute(attr)
119
+ result_attributes = attributes.each_with_object({}) do |attr, acc|
120
+ acc[attr] = get_attribute(attr)
121
+ end
122
+ result[:attributes] = result_attributes
120
123
  end
121
- result[:attributes] = result_attributes
122
- end
123
124
 
124
- def default_type
125
- data.class.to_s.downcase
126
- end
125
+ def default_type
126
+ data.class.to_s.downcase
127
+ end
127
128
 
128
- def default_id
129
- data.id.to_s
130
- end
129
+ def default_id
130
+ data.id.to_s
131
+ end
131
132
 
132
- def get_attribute(attr)
133
- if respond_to?(attr)
134
- send(attr)
135
- elsif data.respond_to?(attr)
136
- data.send(attr)
137
- else
138
- raise NoMethodError, "Undefined serialize key #{attr}"
133
+ def get_attribute(attr)
134
+ if respond_to?(attr)
135
+ send(attr)
136
+ elsif data.respond_to?(attr)
137
+ data.send(attr)
138
+ else
139
+ raise NoMethodError, "Undefined serialize key #{attr}"
140
+ end
139
141
  end
140
142
  end
141
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MvcOne
4
- VERSION = "0.1.0-rc3"
4
+ VERSION = '0.1.0-rc5'
5
5
  end
data/lib/mvc_one.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "mvc_one/version"
3
+ require_relative 'mvc_one/version'
4
+ require_relative 'mvc_one/application'
4
5
 
6
+ # Global namespace
5
7
  module MvcOne
6
- class Error < StandardError; end
7
- # Your code goes here...
8
8
  end
data/mvc_one.gemspec CHANGED
@@ -1,45 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/mvc_one/version"
3
+ require_relative 'lib/mvc_one/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "mvc_one"
6
+ spec.name = 'mvc_one'
7
7
  spec.version = MvcOne::VERSION
8
- spec.authors = ["Evgenii Sendziuk"]
9
- spec.email = ["evgeniisendziuk@taxdome.com"]
8
+ spec.authors = ['Evgenii Sendziuk']
9
+ spec.email = ['evgeniisendziuk@taxdome.com']
10
10
 
11
- spec.summary = "Simple mvc framework, not for production purposes"
12
- spec.description = "Simple mvc framework, not for production purposes"
13
- spec.license = "MIT"
14
- spec.required_ruby_version = ">= 2.6.0"
11
+ spec.summary = 'Simple mvc framework, not for production purposes'
12
+ spec.description = 'Simple mvc framework, not for production purposes'
13
+ spec.license = 'MIT'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
15
 
16
-
17
- spec.metadata["source_code_uri"] = "https://github.com/senzpo/mvc_one.git"
18
- spec.metadata["changelog_uri"] = "https://github.com/senzpo/mvc_one.git"
16
+ spec.metadata['source_code_uri'] = 'https://github.com/senzpo/mvc_one.git'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/senzpo/mvc_one.git'
19
18
 
20
19
  # Specify which files should be added to the gem when it is released.
21
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
21
  spec.files = Dir.chdir(__dir__) do
23
22
  `git ls-files -z`.split("\x0").reject do |f|
24
- (File.expand_path(f) == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
23
+ (File.expand_path(f) == __FILE__) ||
24
+ f.match(%r{\A(?:(?:bin|test|spec|features|config)/|\.(?:git|circleci)|appveyor)})
25
25
  end
26
26
  end
27
- spec.bindir = "exe"
27
+ spec.bindir = 'exe'
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
- spec.require_paths = ["lib"]
29
+ spec.require_paths = ['lib']
30
30
 
31
31
  # Uncomment to register a new dependency of your gem
32
- spec.add_dependency "thor"
33
- spec.add_dependency "slim"
34
- spec.add_dependency "bcrypt"
35
- spec.add_dependency "rack"
36
- spec.add_dependency "rack-session"
37
- spec.add_dependency "sequel"
38
- spec.add_dependency "sqlite3"
39
- spec.add_dependency "dry-struct"
40
- spec.add_dependency "dry-transaction"
41
- spec.add_dependency "dry-validation"
32
+ spec.add_dependency 'bcrypt', '~> 3.1'
33
+ spec.add_dependency 'dry-struct', '~> 1.5'
34
+ spec.add_dependency 'dry-transaction', '~> 0.14'
35
+ spec.add_dependency 'dry-validation', '~> 1.9'
36
+ spec.add_dependency 'rack', '~> 3'
37
+ spec.add_dependency 'rack-session', '~> 2'
38
+ spec.add_dependency 'sequel', '~> 5.60'
39
+ spec.add_dependency 'slim', '~> 5'
40
+ spec.add_dependency 'sqlite3', '~> 1.5'
41
+ spec.add_dependency 'thor', '~> 1.2'
42
42
 
43
43
  # For more information and examples about making a new gem, check out our
44
44
  # guide at: https://bundler.io/guides/creating_gem.html
45
+ spec.metadata['rubygems_mfa_required'] = 'true'
45
46
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  lambda do
4
+ any to: 'welcome#index'
5
+
4
6
  # Example
5
7
  # get '/api/v1/users', to: 'api#v1#users#index'
6
8
  # get '/api/v1/users/:id', to: 'api#v1#users#show'