class GrapeResource::RestGenerator

Public Instance Methods

run_me() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 8
def run_me
  unless Object.const_defined?(name.classify)
    say
    say
    say "You need to generate model", :red
    say
    say "Example :"
    say "  rails g model #{name.classify} attr1 attr2", :green
    say
    return
  end

  generator_type "rest"
  create_rest_endpoint
  insert_into_main unless mounted_routes_exist?
  insert_rest_entities unless entities_exist?
  template_rspec unless rspec_exist?
  routes_exist? ? insert_rest_routes : template_rest_routes
end

Private Instance Methods

create_rest_endpoint() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 29
def create_rest_endpoint
  attributes_for_params

  template "rest/rest_endpoint.rb.erb", "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/resources/#{name.underscore.pluralize}.rb"

  inside "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/resources/" do
    gsub_file("#{name.underscore.pluralize}.rb", /.*?remove.*\r?\n/, "")
  end
end
insert_into_main() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 39
def insert_into_main
  insert_into_file "app/#{GrapeResource.directory}/main.rb", "      mount API::V1::#{name.camelize.pluralize}::Routes\n", before: "      #{GrapeResource.entry_point_routes} -- DONT REMOVE THIS LINE"
end
insert_rest_entities() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 43
def insert_rest_entities
  attributes_for_params

  template "entities.rb.erb", "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/entities/#{name.underscore.singularize}.rb"

  inside "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/entities/" do
    gsub_file("#{name.underscore.singularize}.rb", /.*?remove.*\r?\n/, "")
  end
end
insert_rest_routes() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 61
def insert_rest_routes
  insert_into_file "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/routes.rb", "        mount #{GrapeResource.class_name_prefix}::#{name.camelize.pluralize}::Resources::#{name.camelize.pluralize}\n", before: "        #{GrapeResource.entry_point_routes} -- DONT REMOVE THIS LINE\n" unless mount_code_exist?
end
template_rest_routes() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 65
def template_rest_routes
  template "routes.rb.erb", "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/routes.rb"
end
template_rspec() click to toggle source
# File lib/generators/grape_resource/rest_generator.rb, line 53
def template_rspec
  rspec_dummy
  template "rest/specs.rb.erb", "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/spec/#{name.underscore.pluralize}_spec.rb"
  inside "app/#{GrapeResource.directory}/#{name.underscore.pluralize}/spec/" do
    gsub_file("#{name.underscore.pluralize}_spec.rb", /.*?remove.*\r?\n/, "")
  end
end