Class: Grapethor::Resource

Inherits:
Thor::Group
  • Object
show all
Includes:
Utils, Thor::Actions
Defined in:
lib/grapethor/generators/resource.rb

Constant Summary

Constants included from Utils

Utils::ATTRS_MAP, Utils::CONFIG_FILENAME, Utils::TEST_FRAMEWORK_DIRNAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_versionObject (readonly)

Returns the value of attribute api_version.



9
10
11
# File 'lib/grapethor/generators/resource.rb', line 9

def api_version
  @api_version
end

#app_pathObject (readonly)

Returns the value of attribute app_path.



9
10
11
# File 'lib/grapethor/generators/resource.rb', line 9

def app_path
  @app_path
end

#res_attrsObject (readonly)

Returns the value of attribute res_attrs.



9
10
11
# File 'lib/grapethor/generators/resource.rb', line 9

def res_attrs
  @res_attrs
end

#res_descObject (readonly)

Returns the value of attribute res_desc.



9
10
11
# File 'lib/grapethor/generators/resource.rb', line 9

def res_desc
  @res_desc
end

#res_nameObject (readonly)

Returns the value of attribute res_name.



9
10
11
# File 'lib/grapethor/generators/resource.rb', line 9

def res_name
  @res_name
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/grapethor/generators/resource.rb', line 17

def self.exit_on_failure?
  true
end

.source_rootObject



21
22
23
# File 'lib/grapethor/generators/resource.rb', line 21

def self.source_root
  File.join(__dir__, '..')
end

Instance Method Details

#create_resourceObject



63
64
65
66
67
68
69
70
71
72
# 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_optsObject



47
48
49
50
51
52
# 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_apiObject



55
56
57
58
59
60
# 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