friendly_routes 0.1.0 → 0.2.0

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: 70334657055066f9b06ff494e4da152f2b550ff7
4
- data.tar.gz: bd6ce37d205ed5eff3f93f9664f20fefa81a4a86
3
+ metadata.gz: 7de0ba5374e04ea6b36c94db8b69f578625d93e1
4
+ data.tar.gz: 637009a6c665bb90b54aa4256accab74d61aa9fa
5
5
  SHA512:
6
- metadata.gz: 3686f6b415ef16fee8ac88055b9446e37eb218bbabbb61c2fc432a05449299415fc4856bcb0fd7e175c72bcce6ebc93c8e27655c5a4db2eb42247edaa19e4c68
7
- data.tar.gz: 0b4fb915304f26dccdef5063ad73dbfef4970656bb2065372a477e0085ef47a9df53cd59dea55e8fb95609439b09df7788ab2d2d03eb22538eb031f3003b1077
6
+ metadata.gz: c5e2aa090122f1eecdef2e61ddeb63fb9dccb8a7c75b802a39e07f9ff137fbb5f991a8e66fa44092387aa39dbce6a737ac985eb71d77d61f48d99f3146411f84
7
+ data.tar.gz: 919abe796d939ef71af29b6b907f9e16ba79f4c77ec5d309ea42588df2084f4793b0217b2cff4abd34630bc021c53046fc1bf43f8351e2882d2766d37a89bf16
data/README.md CHANGED
@@ -26,10 +26,18 @@ $ gem install friendly_routes
26
26
 
27
27
  ```ruby
28
28
  # config/routes.rb
29
- dummies_route = FriendlyRoutes::Route.new(:get, '/', controller: :dummies, action: :index)
30
- dummies_route.boolean(:free, true: :free, false: :paid)
31
- dummies_route.collection(:category_id, Category, :title)
32
- friendly_url_for dummies_route
29
+ free = FriendlyRoutes::Params::BooleanParams.new(:free, true: :free, false: :paid)
30
+ category = FriendlyRoutes::Params::CollectionParams.new(:category_id, Category, :title)
31
+
32
+ dummies_route = FriendlyRoutes::Route.new('/', controller: :dummies, action: :index)
33
+ dummies_route.params = [free, category]
34
+
35
+ friendly_url_for dummies_route, :get # '/:free/:category'
36
+
37
+ second_dummies_route = FriendlyRoutes::Route.new(:get, '/', controller: :dummies, action: :index)
38
+ dummies_route.params = [free, 'categories', category]
39
+
40
+ friendly_url_for dummies_route, :get # '/:free/categories/:category'
33
41
 
34
42
  # app/controllers/dummies_controller.rb
35
43
  class DummiesController < ApplicationController
@@ -54,22 +62,27 @@ end
54
62
 
55
63
  1. Create new route, pass **method**, **path**, **controller name**, and **action** to it.
56
64
  ```ruby
57
- dummies_route = FriendlyRoutes::Route.new(:get, '/', controller: :dummies, action: :index)
65
+ dummies_route = FriendlyRoutes::Route.new('/', controller: :dummies, action: :index)
58
66
  ```
59
67
  2. Create route params. *Note: params in route will be accepted in creation order*
60
68
 
61
69
  Boolean param, pass **name**, and hash with **true** and **false** keys.
62
70
  ```ruby
63
- dummies_route.boolean(:discount, true: :true_condition, false: :false_condition)
71
+ FriendlyRoutes::Params::BooleanParams.new(:discount, true: :true_condition, false: :false_condition)
64
72
  ```
65
73
 
66
74
  collection param, pass **name**, **collection**, and **attribute** keys.
67
75
  ```ruby
68
- dummies_route.collection(:categories, Category.where(active: true), :title)
76
+ FriendlyRoutes::Params::CollectionParams.new(:categories, Category.where(active: true), :title)
77
+ ```
78
+
79
+ hash param, pass **name**, **collection**, and **attribute** keys.
80
+ ```ruby
81
+ FriendlyRoutes::Params::HashParams.new(:rooms, 'one-roomed' => 1, 'two-roomed' => 2)
69
82
  ```
70
- 3. Initialize route wih `friendly_url_for`
83
+ 4. Initialize route wih `friendly_url_for`
71
84
  ```ruby
72
- friendly_url_for dummies_route
85
+ friendly_url_for dummies_route, :get
73
86
  ```
74
87
 
75
88
  ### Controllers
@@ -2,15 +2,15 @@
2
2
 
3
3
  module FriendlyRoutes
4
4
  module Dispatcher
5
- def friendly_url_for(route)
5
+ def friendly_url_for(route, method, as: '')
6
6
  public_send(
7
- route.method,
7
+ method,
8
8
  route.path,
9
9
  controller: route.controller,
10
10
  action: route.action,
11
11
  friendly_route: route,
12
12
  constraints: route.constraints,
13
- as: route.as
13
+ as: as
14
14
  )
15
15
  end
16
16
  end
@@ -5,9 +5,14 @@ module FriendlyRoutes
5
5
  class Base
6
6
  attr_accessor :type, :name
7
7
 
8
- def initialize(type, name)
8
+ def initialize(type, name, optional)
9
9
  @type = type
10
10
  @name = name
11
+ @optional = optional
12
+ end
13
+
14
+ def optional?
15
+ @optional == true
11
16
  end
12
17
 
13
18
  def constraints
@@ -2,12 +2,12 @@
2
2
 
3
3
  module FriendlyRoutes
4
4
  module Params
5
- class Boolean < Base
5
+ class BooleanParams < Base
6
6
  attr_accessor :true, :false
7
7
 
8
- def initialize(name, options)
8
+ def initialize(name, options, optional: true)
9
9
  check_params(options)
10
- super(:boolean, name)
10
+ super(:boolean, name, optional)
11
11
  @true = options[:true]
12
12
  @false = options[:false]
13
13
  end
@@ -2,18 +2,18 @@
2
2
 
3
3
  module FriendlyRoutes
4
4
  module Params
5
- class Collection < Base
5
+ class CollectionParams < Base
6
6
  attr_accessor :collection, :key_attr
7
7
 
8
- def initialize(name, collection, key_attr)
9
- super(:collection, name)
8
+ def initialize(name, collection, key_attr, optional: true)
9
+ super(:collection, name, optional)
10
10
  @collection = collection
11
11
  @key_attr = key_attr
12
12
  check_params
13
13
  end
14
14
 
15
15
  def constraints
16
- Regexp.new @collection.all.map(&@key_attr).join('|')
16
+ Regexp.new @collection.all.map(&@key_attr).compact.map(&:downcase).join('|')
17
17
  end
18
18
 
19
19
  def parse(value)
@@ -23,8 +23,8 @@ module FriendlyRoutes
23
23
  private
24
24
 
25
25
  def check_params
26
- unless @collection.try(:has_attribute?, @key_attr)
27
- raise ArgumentError, "Collection not passed, or doesn't have passed attribute"
26
+ if @collection.nil? || @key_attr.nil?
27
+ raise ArgumentError, 'Collection or key attribute not passed'
28
28
  end
29
29
  end
30
30
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ module Params
5
+ class HashParams < Base
6
+ attr_accessor :hash
7
+
8
+ def initialize(name, hash, optional: true)
9
+ super(:collection, name, optional)
10
+ @hash = hash
11
+ check_params
12
+ end
13
+
14
+ def constraints
15
+ Regexp.new @hash.keys.join('|')
16
+ end
17
+
18
+ def parse(value)
19
+ @hash[value]
20
+ end
21
+
22
+ private
23
+
24
+ def check_params
25
+ raise ArgumentError, 'Invalid hash passed' unless @hash.is_a?(Hash)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,8 +4,7 @@ module FriendlyRoutes
4
4
  class Route
5
5
  attr_accessor :method, :controller, :action, :params, :prefix
6
6
 
7
- def initialize(method, path, controller: nil, action: nil, prefix: 'friendly_routes')
8
- @method = method
7
+ def initialize(path, controller: nil, action: nil, prefix: 'friendly_routes')
9
8
  @original_path = path
10
9
  @controller = controller
11
10
  @action = action
@@ -13,39 +12,20 @@ module FriendlyRoutes
13
12
  @prefix = prefix
14
13
  end
15
14
 
16
- def boolean(name, params)
17
- @params.push(Params::Boolean.new(name, params))
18
- end
19
-
20
- def collection(name, collection, key_attr)
21
- @params.push(Params::Collection.new(name, collection, key_attr))
22
- end
23
-
24
15
  def path
25
- @original_path + mapped_params
26
- end
27
-
28
- def prefixed_param_name(param)
29
- "#{@prefix}_#{param.name}"
30
- end
31
-
32
- def as
33
- "#{@prefix}_#{@controller}_#{@action}"
16
+ @original_path + FriendlyRoutes::PrefixedParams.new(@params, @prefix).call
34
17
  end
35
18
 
36
19
  def constraints
37
- @params.map do |param|
38
- [prefixed_param_name(param).to_sym, param.constraints]
39
- end.to_h
20
+ FriendlyRoutes::Constraints.new(dynamic_params, @prefix).call
40
21
  end
41
22
 
42
- private
23
+ def dynamic_params
24
+ @params.select { |param| param.is_a?(FriendlyRoutes::Params::Base) }
25
+ end
43
26
 
44
- def mapped_params
45
- mapped = @params.map do |param|
46
- ':' + prefixed_param_name(param)
47
- end
48
- mapped.join('/')
27
+ def inspect
28
+ dynamic_params.map(&:name).join(', ')
49
29
  end
50
30
  end
51
31
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ class Constraints
5
+ def initialize(params, prefix)
6
+ @params = params
7
+ @prefix = prefix
8
+ end
9
+
10
+ def call
11
+ @params.map do |param|
12
+ [FriendlyRoutes::PrefixedParam.new(param.name, @prefix).call.to_sym, param.constraints]
13
+ end.to_h
14
+ end
15
+ end
16
+ end
@@ -8,7 +8,8 @@ module FriendlyRoutes
8
8
  end
9
9
 
10
10
  def call
11
- @route.params.each do |param|
11
+ return unless @route
12
+ @route.dynamic_params.each do |param|
12
13
  parse(param)
13
14
  end
14
15
  end
@@ -16,9 +17,11 @@ module FriendlyRoutes
16
17
  private
17
18
 
18
19
  def parse(param)
19
- value = @params[@route.prefixed_param_name(param)]
20
+ prefixed_name = FriendlyRoutes::PrefixedParam.new(param.name, @route.prefix).call
21
+ value = @params[prefixed_name]
20
22
  return unless value
21
23
  @params[param.name] = param.parse(value)
24
+ @params.delete(prefixed_name)
22
25
  end
23
26
  end
24
27
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ class PrefixedParam
5
+ def initialize(param, prefix)
6
+ @param = param
7
+ @prefix = prefix
8
+ end
9
+
10
+ def call
11
+ "#{@prefix}_#{@param}"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FriendlyRoutes
4
+ class PrefixedParams
5
+ def initialize(params, prefix)
6
+ @params = params
7
+ @prefix = prefix
8
+ end
9
+
10
+ def call
11
+ mapped.join('/')
12
+ end
13
+
14
+ private
15
+
16
+ def mapped
17
+ @params.map do |param|
18
+ if param.is_a?(FriendlyRoutes::Params::Base)
19
+ name = PrefixedParam.new(param.name, @prefix).call
20
+ param.optional? ? "(:#{name})" : ":#{name}"
21
+ else
22
+ param
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FriendlyRoutes
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -2,11 +2,15 @@
2
2
 
3
3
  require 'friendly_routes/dispatcher'
4
4
  require 'friendly_routes/helper'
5
- require 'friendly_routes/parser'
6
5
  require 'friendly_routes/route'
7
6
  require 'friendly_routes/params/base'
8
- require 'friendly_routes/params/boolean'
9
- require 'friendly_routes/params/collection'
7
+ require 'friendly_routes/params/boolean_params'
8
+ require 'friendly_routes/params/collection_params'
9
+ require 'friendly_routes/params/hash_params'
10
+ require 'friendly_routes/services/constraints'
11
+ require 'friendly_routes/services/parser'
12
+ require 'friendly_routes/services/prefixed_param'
13
+ require 'friendly_routes/services/prefixed_params'
10
14
 
11
15
  module FriendlyRoutes
12
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_routes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Gritsay
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-31 00:00:00.000000000 Z
11
+ date: 2016-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -143,10 +143,14 @@ files:
143
143
  - lib/friendly_routes/dispatcher.rb
144
144
  - lib/friendly_routes/helper.rb
145
145
  - lib/friendly_routes/params/base.rb
146
- - lib/friendly_routes/params/boolean.rb
147
- - lib/friendly_routes/params/collection.rb
148
- - lib/friendly_routes/parser.rb
146
+ - lib/friendly_routes/params/boolean_params.rb
147
+ - lib/friendly_routes/params/collection_params.rb
148
+ - lib/friendly_routes/params/hash_params.rb
149
149
  - lib/friendly_routes/route.rb
150
+ - lib/friendly_routes/services/constraints.rb
151
+ - lib/friendly_routes/services/parser.rb
152
+ - lib/friendly_routes/services/prefixed_param.rb
153
+ - lib/friendly_routes/services/prefixed_params.rb
150
154
  - lib/friendly_routes/version.rb
151
155
  homepage: https://github.com/RoM4iK/friendly_routes
152
156
  licenses: