Module: RestfulRouteVersion::VersionMapper
- Defined in:
- lib/restful_route_version/version_mapper.rb
Instance Attribute Summary collapse
-
#cached_namespace_blocks ⇒ Object
Returns the value of attribute cached_namespace_blocks.
Instance Method Summary collapse
- #create_controller_class(full_constant_name, klass_constant) ⇒ Object
- #create_controller_dynamically?(controllers_to_exclude, old_controller_name, new_controller_name) ⇒ Boolean
- #create_derived_controllers(old_namespace, options = {}) ⇒ Object
- #inherit_routes(*entities) ⇒ Object
- #merge_except_options(options) ⇒ Object
- #resource(*resources, &block) ⇒ Object
- #resources(*resources, &block) ⇒ Object
- #skip_resource?(resources, except_options) ⇒ Boolean
- #version_namespace(path, options = {}, &block) ⇒ Object
Instance Attribute Details
#cached_namespace_blocks ⇒ Object
Returns the value of attribute cached_namespace_blocks.
3 4 5 |
# File 'lib/restful_route_version/version_mapper.rb', line 3 def cached_namespace_blocks @cached_namespace_blocks end |
Instance Method Details
#create_controller_class(full_constant_name, klass_constant) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/restful_route_version/version_mapper.rb', line 87 def create_controller_class(full_constant_name, klass_constant) names = full_constant_name.split('::') ActiveSupport::Dependencies.dynamically_defined_constants << full_constant_name names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| if constant.const_defined?(name) constant = constant.const_get(name) else constant = constant.const_set(name, klass_constant) end end end |
#create_controller_dynamically?(controllers_to_exclude, old_controller_name, new_controller_name) ⇒ Boolean
80 81 82 83 84 |
# File 'lib/restful_route_version/version_mapper.rb', line 80 def create_controller_dynamically?(controllers_to_exclude, old_controller_name, new_controller_name) !controllers_to_exclude.include?(old_controller_name) && old_controller_name =~ /Controller$/ && !File.exists?("#{Rails.root}/app/controllers/#{new_controller_name.underscore}.rb") end |
#create_derived_controllers(old_namespace, options = {}) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/restful_route_version/version_mapper.rb', line 61 def create_derived_controllers(old_namespace, = {}) old_namespace.gsub!(/^\/?/,'') current_namespace = [:namespace].gsub(/^\/?/,'') Dir["#{Rails.root}/app/controllers/#{old_namespace}/*.rb"].each do |controller_file_name| require_or_load(controller_file_name) end exclude_constants = [:except].blank? ? [] : [:except] controllers_to_exclude = exclude_constants.map { |x| (old_namespace + "/#{x}Controller").camelize } old_namespace.camelize.constantize.constants.each do |constant_name| full_constant_name = old_namespace.camelize + "::" + constant_name.to_s new_controller_name = "#{current_namespace.camelize}::#{constant_name}" if create_controller_dynamically?(controllers_to_exclude, full_constant_name, new_controller_name) create_controller_class(new_controller_name,Class.new(full_constant_name.constantize)) end end end |
#inherit_routes(*entities) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/restful_route_version/version_mapper.rb', line 37 def inherit_routes(*entities) = entities. = () [:old_namespace] = @scope[:options][:old_namespace] || entities.dup.shift entities.each { |entity| inherited_route_block = @cached_namespace_blocks[entity] inherited_route_block && scope() do inherited_route_block.call() end } create_derived_controllers([:old_namespace], ) end |
#merge_except_options(options) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/restful_route_version/version_mapper.rb', line 52 def () [:except] ||= [] [:namespace] = @scope[:path] if old_exception_option = @scope[:options][:except] [:except] += old_exception_option end end |
#resource(*resources, &block) ⇒ Object
25 26 27 28 29 |
# File 'lib/restful_route_version/version_mapper.rb', line 25 def resource(*resources, &block) = @scope[:options][:except] return if skip_resource?(resources,) super(*resources, &block) end |
#resources(*resources, &block) ⇒ Object
19 20 21 22 23 |
# File 'lib/restful_route_version/version_mapper.rb', line 19 def resources(*resources, &block) = @scope[:options][:except] return if skip_resource?(resources,) super(*resources, &block) end |
#skip_resource?(resources, except_options) ⇒ Boolean
31 32 33 34 35 |
# File 'lib/restful_route_version/version_mapper.rb', line 31 def skip_resource?(resources, ) return false if(resources.length > 1 || .blank?) return true if .include?(resources.first.to_s) false end |
#version_namespace(path, options = {}, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/restful_route_version/version_mapper.rb', line 4 def version_namespace(path, = {}, &block) path = path.to_s = { :path => path, :as => path, :module => path, :shallow_path => path, :shallow_prefix => path }.merge!() @cached_namespace_blocks ||= {} scope() { create_controller_class(@scope[:module].to_s.camelize,Module.new) @cached_namespace_blocks[@scope[:path]] = block if [:cache_route] block.call } end |