Class: Starter::Builder

Inherits:
Object
  • Object
show all
Extended by:
BaseFile, Names, Templates::Endpoints, Templates::Files
Defined in:
lib/starter/builder.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Names

api_base_file_name, api_file_name, api_spec_name, base_file_name, base_spec_name, entity_file_name, klass_name, lib_file_name, lib_klass_name, lib_spec_name, mount_point

Methods included from BaseFile

add_mount_point, add_to_base, base_file, base_prefix, base_version, remove_from_base, remove_mount_point

Methods included from Templates::Files

api_file, api_spec, base_namespace_file, entity_file, lib_file, lib_spec

Methods included from Templates::Endpoints

crud, get_all, get_all_spec, post, post_spec, singular_one

Class Attribute Details

.destinationObject (readonly)

Returns the value of attribute destination.



20
21
22
# File 'lib/starter/builder.rb', line 20

def destination
  @destination
end

.entityObject (readonly)

Returns the value of attribute entity.



20
21
22
# File 'lib/starter/builder.rb', line 20

def entity
  @entity
end

.forceObject (readonly)

Returns the value of attribute force.



20
21
22
# File 'lib/starter/builder.rb', line 20

def force
  @force
end

.ormObject (readonly)

Returns the value of attribute orm.



20
21
22
# File 'lib/starter/builder.rb', line 20

def orm
  @orm
end

.prefixObject (readonly)

Returns the value of attribute prefix.



20
21
22
# File 'lib/starter/builder.rb', line 20

def prefix
  @prefix
end

.resourceObject (readonly)

Returns the value of attribute resource.



20
21
22
# File 'lib/starter/builder.rb', line 20

def resource
  @resource
end

.setObject (readonly)

Returns the value of attribute set.



20
21
22
# File 'lib/starter/builder.rb', line 20

def set
  @set
end

Class Method Details

.add!(resource, options = {}) ⇒ Object

would be called from add command

resource - A String as name options - A Hash to provide some optional arguments (default: {})

:resource - the name of the resource to create
:set – Whitespace separated list of http verbs
       (default: nil, possible: post get put patch delete)
:force - A Boolean, if given existent files should be overwriten (default: false)
:entity - A Boolean, if given an entity file would be created (default: false)
:orm - A Boolean, if given the created lib/model file will be inherited orm specific (default: false)


68
69
70
71
72
73
74
75
76
77
# File 'lib/starter/builder.rb', line 68

def add!(resource, options = {})
  @resource = resource
  @set = options[:set]
  @force = options[:force]
  @entity = options[:entity]
  @orm = options[:orm]

  Orms.add_migration(klass_name, resource.downcase) if @orm
  save_resource
end

.endpoint_specsObject

provides the specs for the endpoints of the resource



103
104
105
# File 'lib/starter/builder.rb', line 103

def endpoint_specs
  content(endpoint_set.map { |x| "#{x}_spec" }).join("\n")
end

.endpointsObject

provides the endpoints for the given resource



98
99
100
# File 'lib/starter/builder.rb', line 98

def endpoints
  content(endpoint_set).join("\n\n")
end

.new!(name, source, destination, options = {}) ⇒ Object

public methods

would be called from new command

name - A String as project name,

it will also be the namespace in the lib folder

source - A String which provides the template path destination - A String which provides the new project path options - A Hash to provide some optional arguments (default: {})

:prefix - Sets the Prefix for the API
:orm - Sets and creates ORM specific files


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/starter/builder.rb', line 35

def new!(name, source, destination, options = {})
  @resource = name
  @destination = destination
  @prefix = options[:p] # can be nil

  FileUtils.copy_entry source, destination

  config_static.each do |config|
    replace_static(File.join(config[:file]), config[:pattern])
  end

  add_namespace_with_version

  Orms.build(name, destination, options[:orm]) if options[:orm]

  Starter::Config.save(
    dest: destination,
    content: { prefix: prefix, orm: options[:orm].to_s }
  )

  self
end

.remove!(resource, options = {}) ⇒ Object

would be called on from rm command

resource - A String, which indicates the resource to remove options - A Hash to provide some optional arguments (default: {})

:entity - A Boolean, if given an entity file would also be removed (default: nil -> false)


84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/starter/builder.rb', line 84

def remove!(resource, options = {})
  @resource = resource
  @entity = options[:entity]

  file_list.map { |x| send("#{x}_name") }.each do |file_to_remove|
    FileUtils.rm file_to_remove
  rescue StandardError => e
    $stdout.puts e.to_s
  end

  remove_mount_point
end