Skip to content

Commit

Permalink
Cleanup per #51
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed May 11, 2015
1 parent e45ced3 commit f2b99ae
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ if !ENV['TRAVIS']
gem 'yard', require: false
end

gem 'lotus-utils', '~> 0.4', require: false, github: 'lotus/utils', branch: 'master'
gem 'lotus-utils', '~> 0.4', require: false, github: 'lotus/utils', branch: '0.4.x'
gem 'simplecov', require: false
gem 'coveralls', require: false
42 changes: 0 additions & 42 deletions lib/lotus/routing/nested.rb

This file was deleted.

25 changes: 13 additions & 12 deletions lib/lotus/routing/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Resource

# @api private
# @since x.x.x
SLASH = '/'.freeze
NESTED_ROUTES_SEPARATOR = '/'.freeze

# Set of default routes
#
Expand Down Expand Up @@ -49,15 +49,15 @@ class Resource

# @api private
# @since x.x.x
attr_reader :parent_resource
attr_reader :parent

# @api private
# @since 0.1.0
def initialize(router, name, options = {}, parent_resource = nil, &blk)
@router = router
@name = name
@parent_resource = parent_resource
@options = Options.new(self.class.actions, options.merge(name: @name))
def initialize(router, name, options = {}, parent = nil, &blk)
@router = router
@name = name
@parent = parent
@options = Options.new(self.class.actions, options.merge(name: @name))
generate(&blk)
end

Expand All @@ -79,20 +79,21 @@ def resource(name, options = {}, &blk)
_resource(Resource, name, options, &blk)
end

# Return slash, no wildcard param
# Return separator
#
# @api private
# @since x.x.x
def wildcard_param(route_param = nil)
SLASH
NESTED_ROUTES_SEPARATOR
end

private

# @api private
# @since x.x.x
def _resource(klass, name, options, &blk)
merged_options = options.merge(separator: @options[:separator], namespace: @options[:namespace])
nested_name = "#{@name}#{Resource::Action::NESTED_ROUTES_SEPARATOR}#{name}"
klass.new(@router, nested_name, merged_options, self, &blk)
options = options.merge(separator: @options[:separator], namespace: @options[:namespace])
klass.new(@router, [@name, name].join(NESTED_ROUTES_SEPARATOR), options, self, &blk)
end

def generate(&blk)
Expand Down
16 changes: 9 additions & 7 deletions lib/lotus/routing/resource/action.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'lotus/utils/string'
require 'lotus/utils/path_prefix'
require 'lotus/utils/class_attribute'
require 'lotus/routing/nested'
require 'lotus/routing/resource/nested'

module Lotus
module Routing
Expand Down Expand Up @@ -48,6 +48,7 @@ class Action
# @param router [Lotus::Router]
# @param action [Lotus::Routing::Resource::Action]
# @param options [Hash]
# @param resource [Lotus::Routing::Resource, Lotus::Routing::Resources]
#
# @api private
#
Expand All @@ -60,6 +61,7 @@ def self.generate(router, action, options = {}, resource = nil)
#
# @param router [Lotus::Router]
# @param options [Hash]
# @param resource [Lotus::Routing::Resource, Lotus::Routing::Resources]
# @param blk [Proc]
#
# @api private
Expand All @@ -86,6 +88,8 @@ def generate(&blk)

# Resource name
#
# @return [String]
#
# @api private
# @since 0.1.0
#
Expand All @@ -98,7 +102,7 @@ def generate(&blk)
#
# # 'identity' is the name passed in the @options
def resource_name
@options[:name]
@resource_name ||= @options[:name].to_s
end

# Namespace
Expand Down Expand Up @@ -254,19 +258,17 @@ def controller_name
# @api private
# @since x.x.x
def _singularized_as
resource_name.to_s.split(NESTED_ROUTES_SEPARATOR).map do |name|
resource_name.split(NESTED_ROUTES_SEPARATOR).map do |name|
Lotus::Utils::String.new(name).singularize
end.join(self.class.named_route_separator)
end
end

# Create nested rest path
#
# @api private
# @since x.x.x
def _nested_rest_path
nested = Nested.new(resource_name, @resource)
nested.calculate_nested_path
nested.nested_path
Nested.new(resource_name, @resource).to_path
end
end

Expand Down
39 changes: 39 additions & 0 deletions lib/lotus/routing/resource/nested.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Lotus
module Routing
class Resource
# Helper class to calculate nested path
#
# @api private
# @since x.x.x
class Nested
# @api private
# @since x.x.x
SEPARATOR = '/'.freeze

# @api private
# @since x.x.x
def initialize(resource_name, resource)
@resource_name = resource_name.to_s.split(SEPARATOR)
@resource = resource
@path = []
_calculate(@resource_name.dup, @resource)
end

# @api private
# @since x.x.x
def to_path
@path.reverse!.pop
@resource_name.zip(@path).flatten.join
end

private

def _calculate(param_wildcard, resource = nil)
return if resource.nil?
@path << resource.wildcard_param(param_wildcard.pop)
_calculate(param_wildcard, resource.parent)
end
end
end
end
end
4 changes: 0 additions & 4 deletions lib/lotus/routing/resource/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ class Options
# @since 0.1.0
attr_reader :actions

# @api private
# @since x.x.x
attr_reader :options

# Initialize the options for:
# * Lotus::Router#resource
# * Lotus::Router#resources
Expand Down
3 changes: 1 addition & 2 deletions lib/lotus/routing/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class Resources < Resource
# @api private
# @since x.x.x
def wildcard_param(route_param = nil)
sigularized_param = Lotus::Utils::String.new(route_param).singularize
"#{SLASH}:#{sigularized_param}_id#{SLASH}"
"/:#{ Lotus::Utils::String.new(route_param).singularize }_id/"
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lotus/routing/resources/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module PluralizedAction
# @api private
# @since x.x.x
def as
Lotus::Utils::String.new(super).pluralize
Lotus::Utils::String.new(super).pluralize
end
end

Expand All @@ -50,7 +50,7 @@ def as
# @see Lotus::Router#resources
class CollectionAction < Resource::CollectionAction
def as(action_name)
Lotus::Utils::String.new(super(action_name)).pluralize
Lotus::Utils::String.new(super(action_name)).pluralize
end
end

Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit f2b99ae

Please sign in to comment.