Module: SwaggerCodegenRails::Namespace
- Included in:
- Swagger::AddGenerator, Swagger::InitGenerator
- Defined in:
- lib/swagger_codegen_rails/namespace.rb
Instance Method Summary collapse
- #indent(content, multiplier = 2) ⇒ Object
- #module_namespacing(&block) ⇒ Object
- #namespaced? ⇒ Boolean
- #namespaced_route ⇒ Object
-
#split_namespace ⇒ Object
———— hoge/foo/bar => [“Hoge”, “Foo”, “Bar”] ————.
- #wrap_with_namespace(content, namespace) ⇒ Object
Instance Method Details
#indent(content, multiplier = 2) ⇒ Object
30 31 32 33 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 30 def indent(content, multiplier = 2) spaces = " " * multiplier content.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join end |
#module_namespacing(&block) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 4 def module_namespacing(&block) return unless namespaced? content = capture(&block) split_namespace.reverse.each do |name| content = wrap_with_namespace(content, name) end concat(content) end |
#namespaced? ⇒ Boolean
13 14 15 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 13 def namespaced? namespace end |
#namespaced_route ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 35 def namespaced_route depth = 0 lines = [] split_namespace.map(&:underscore).each do |ns| lines << indent("namespace :#{ns} do\n", depth*2) depth += 1 end lines << indent("resources :swagger, only: :index\n", depth*2) until depth.zero? depth -= 1 lines << indent("end\n", depth*2) end lines.join end |
#split_namespace ⇒ Object
hoge/foo/bar
> [“Hoge”, “Foo”, “Bar”]
26 27 28 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 26 def split_namespace namespace.gsub('.','').split("/").reject(&:blank?).map(&:camelize) end |
#wrap_with_namespace(content, namespace) ⇒ Object
17 18 19 20 |
# File 'lib/swagger_codegen_rails/namespace.rb', line 17 def wrap_with_namespace(content, namespace) content = indent(content).chomp "module #{namespace}\n#{content}\nend\n" end |