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, 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: {})

: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)


64
65
66
67
68
69
70
71
72
# File 'lib/starter/builder.rb', line 64

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

  save_resource
end

.config_staticObject



47
48
49
50
51
52
53
54
# File 'lib/starter/builder.rb', line 47

def config_static
  [
    { file: %w[script server], pattern: "API-#{resource}" },
    { file: %w[api base.rb], pattern: prefix ? "prefix :#{prefix}" : nil },
    { file: %w[spec requests root_spec.rb], pattern: prefix ? "/#{prefix}" : nil },
    { file: %w[spec requests documentation_spec.rb], pattern: prefix ? "/#{prefix}" : nil }
  ]
end

.endpoint_specsObject

provides the specs for the endpoints of the resource



100
101
102
# File 'lib/starter/builder.rb', line 100

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

.endpointsObject

provides the endpoints for the given resource



95
96
97
# File 'lib/starter/builder.rb', line 95

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 source - A String which provides the template path destination - A String which provides the new project path



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/starter/builder.rb', line 30

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

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

  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)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/starter/builder.rb', line 79

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

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

  remove_mount_point
end