module GrapeResource::GeneratorHelpers

Attributes

attributes[RW]

Private Instance Methods

attributes_for_params() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 118
def attributes_for_params
  @entities = {}
  editable_attributes.map do |col|
    @entities[col.name] = col.type.underscore.in?(["text", "uuid"]) ? "String" : col.type.camelize
  end
  @entities
end
check_endpoint_or_method(args) click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 6
def check_endpoint_or_method(args)
  @default_endpoint, custom, custom2 = [], [], []
  args.each do |arg|
    case arg.split(":").length
    when 1
      @default_endpoint << arg
    when 2
      custom << arg.split(":")
    else
      if arg.split(":")[1] == "get"
        custom2 << arg.split(":")
      end
    end
  end
  @default_endpoint
  @custom_endpoint, @custom_endpoint2 = custom.to_h, custom2
  collection_or_member
  get_endpoint_and_method_name
end
collection_or_member() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 26
def collection_or_member
  collection, member = [], []
  @custom_endpoint2.each do |c2|
    collection << c2.take(2) if c2.last == "collection"
    member << c2.take(2) if c2.last == "member"
  end
  @collection, @member = collection.to_h, member.to_h
end
editable_attributes() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 112
def editable_attributes
  attributes ||= model_columns_for_attributes.map do |column|
    Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
  end
end
entities_exist?() click to toggle source

Entities

# File lib/generators/grape_resource/generator_helpers.rb, line 101
def entities_exist?
  entity_file = Rails.root.join("app/#{GrapeResource.directory}#{name.underscore.pluralize}/entities/#{name.underscore.singularize}.rb")
  File.exist? entity_file
end
generator_type(class_name) click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 44
def generator_type class_name
  @name_of_class = class_name == "rest" ? name.camelize.pluralize : name.camelize.singularize
  @name_of_class
end
get_endpoint_and_method_name() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 35
def get_endpoint_and_method_name
  @put, @post, @delete = {}, {}, {}
  @custom_endpoint.each do |k, v|
    @put[k] = v if v == "put"
    @post[k] = v if v == "post"
    @delete[k] = v if v == "delete"
  end
end
model_columns_for_attributes() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 106
def model_columns_for_attributes
  name.classify.constantize.columns.reject do |column|
    column.name.to_s =~ /^(id|created_at|updated_at)$/
  end
end
mount_code_exist?() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 55
def mount_code_exist?
  string = "        mount #{GrapeResource.class_name_prefix}::#{name.camelize.pluralize}::Resources::#{@name_of_class}\n"
  File.read(@routes_file).each_line do |line|
    return true if line == string
  end
  return false
end
mounted_routes_exist?() click to toggle source

Main

# File lib/generators/grape_resource/generator_helpers.rb, line 64
def mounted_routes_exist?
  @main_file = Rails.root.join("app/#{GrapeResource.directory}main.rb")
  string = "      mount API::V1::#{name.camelize.pluralize}::Routes\n"
  File.read(@main_file).each_line do |line|
    return true if line == string
  end
  return false
end
routes_exist?() click to toggle source

Routes

# File lib/generators/grape_resource/generator_helpers.rb, line 50
def routes_exist?
  @routes_file = Rails.root.join("app/#{GrapeResource.directory}#{name.underscore.pluralize}/routes.rb")
  File.exist? @routes_file
end
rspec_dummy() click to toggle source
# File lib/generators/grape_resource/generator_helpers.rb, line 79
def rspec_dummy
  @dummy = {}
  editable_attributes.map do |col|
    @dummy[col.name] = case col.type.underscore
    when "string"
      "Example of string"
    when "text"
      "Example of text"
    when "uuid"
      "e388e513-2510-44d6-9fb3-56411e7ed7f2"
    when "integer"
      123
    when "boolean"
      1
    when "datetime"
      "2019-07-09 04:10:55"
    end
  end
  @dummy
end
rspec_exist?() click to toggle source

Specs

# File lib/generators/grape_resource/generator_helpers.rb, line 74
def rspec_exist?
  rspec_file = Rails.root.join("app/#{GrapeResource.directory}#{name.underscore.pluralize}/spec/#{@name_of_class.underscore}_spec.rb")
  File.exist? rspec_file
end