Class: Octonore::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/octonore/template.rb

Overview

A gitignore template. Templates have two attributes: a name and a source.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ String

Create a new template.

Example:

c_template = Octonore::Template.new('C')
java_template = Octonore::Template.new('java')

Parameters:

  • name (String)

    name of template to create


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/octonore/template.rb', line 28

def initialize(name)
  # Get two arrays of all of the names of templates available. Downcase
  # one.
  list = self.class.list
  down_list = list.map(&:downcase)
  # Create a hash from the lowercase names to the normal names so we can
  # make the user-specified name case-insensitive.
  list_hash = Hash[down_list.zip(list)]

  # Downcase the the user's name selection and get the matching normal
  # name in the list_hash
  self.name = list_hash[name.downcase]
  # Retrieve the information from Github for their template
  reload
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


7
8
9
# File 'lib/octonore/template.rb', line 7

def name
  @name
end

#sourceObject

Returns the value of attribute source


7
8
9
# File 'lib/octonore/template.rb', line 7

def source
  @source
end

Class Method Details

.listArray

List available templates

Example:

>> Octonore::Template.list

Returns:

  • (Array)

    The available gitignore templates.


15
16
17
18
# File 'lib/octonore/template.rb', line 15

def self.list
  raw_list = Octonore::HTTPHelper.instance.get("/templates")
  parsed_list = raw_list.parsed_response
end

Instance Method Details

#reloadString

Reload the Gitignore source from Github.

Example:

>> outdated_template.source = nil
=> nil
>> outdated_template.reload
=> "# Object files\n*.o\n\n# Libraries\n*.lib..."
>> outdated_template.source
=> "# Object files\n*.o\n\n# Libraries\n*.lib..."

Returns:

  • (String)

    Contents of template.


55
56
57
58
59
60
61
62
63
64
# File 'lib/octonore/template.rb', line 55

def reload
  data = get_template_hash @name
  
  if valid_template_hash? data
    @source = data["source"]
  else
    raise GitignoreTemplateNotFoundError,
      "Template '#{@name}' does not exist!"
  end
end