class Grapethor::Resource

Attributes

api_version[R]
app_path[R]
res_attrs[R]
res_desc[R]
res_name[R]

Public Class Methods

exit_on_failure?() click to toggle source
# File lib/grapethor/generators/resource.rb, line 17
def self.exit_on_failure?
  true
end
source_root() click to toggle source
# File lib/grapethor/generators/resource.rb, line 21
def self.source_root
  File.join(__dir__, '..')
end

Public Instance Methods

create_resource() click to toggle source
# File lib/grapethor/generators/resource.rb, line 63
def create_resource
  report("Creating new resource...") do
    directory "templates/resource", "#{app_path}"
    directory "templates/resource_#{app_test_framework}", "#{app_path}"

    insert_into_file "#{app_path}/api/#{api_version}/base.rb",
                     "\s\s\s\smount API#{api_version}::#{res_name.classify.pluralize}\n",
                     before: "\s\s\s\s# mount API#{api_version}::<ResourceOrEndpointClass>\n"
  end
end
parse_args_and_opts() click to toggle source
# File lib/grapethor/generators/resource.rb, line 47
def parse_args_and_opts
  @res_name    = name.downcase.singularize
  @res_attrs   = options[:attrs].delete_if { |k, v| k == 'id' }.map { |k, v| [k, v.downcase] }.to_h
  @api_version = options[:version].downcase
  @app_path    = options[:path]
end
validate_target_api() click to toggle source
# File lib/grapethor/generators/resource.rb, line 55
def validate_target_api
  unless api_version_exists?
    alert "API version '#{api_version}' does not exist!"
    exit
  end
end

Private Instance Methods

api_version_exists?() click to toggle source
# File lib/grapethor/generators/resource.rb, line 77
def api_version_exists?
  Dir.exist?("#{app_path}/api/#{api_version}")
end
app_orm() click to toggle source
# File lib/grapethor/generators/resource.rb, line 102
def app_orm
  @app_orm ||= config_filename(@app_path)['app_orm']
end
app_prefix() click to toggle source
# File lib/grapethor/generators/resource.rb, line 92
def app_prefix
  @app_prefix ||= config_filename(@app_path)['app_prefix']
end
app_test_framework() click to toggle source
# File lib/grapethor/generators/resource.rb, line 97
def app_test_framework
  @app_test_framework ||= config_filename(@app_path)['app_test_framework']
end
param_to_type(param) click to toggle source
# File lib/grapethor/generators/resource.rb, line 107
def param_to_type(param)
  ATTRS_MAP.dig(app_orm.to_sym, param.to_sym, :type) || 'Unknown'
end
res_migration() click to toggle source
# File lib/grapethor/generators/resource.rb, line 82
def res_migration
  "#{Time.now.strftime("%Y%m%d%H%M%S")}_create_#{res_name.pluralize}"
end
res_name_plural() click to toggle source
# File lib/grapethor/generators/resource.rb, line 87
def res_name_plural
  res_name.pluralize
end
sample_value(param, path=false) click to toggle source
# File lib/grapethor/generators/resource.rb, line 112
def sample_value(param, path=false)
  val = ATTRS_MAP.dig(app_orm.to_sym, param.to_sym, :sample)
  if path && val.respond_to?(:tr!)
    val.tr!("'", "")
  end
  val || 'unknown'
end