Class: Specify::BranchParser

Inherits:
Object
  • Object
show all
Defined in:
lib/specify/branch_parser.rb

Overview

A class that parses target information (hostname, database, collection, level) from a string that follows the convention Database/Collection/level this can be the name of a git branch of a repository residing in a folder denoting the hostname.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_file_path, name, config = nil) ⇒ BranchParser

Returns a new instance of BranchParser

Raises:

  • (ArgumentError)

11
12
13
14
15
16
17
# File 'lib/specify/branch_parser.rb', line 11

def initialize(view_file_path, name, config = nil)
  @config = Configuration::HostConfig.new(config)
  @database, collection, @level, @user = *name.split('/')
  raise ArgumentError, BRANCH_ERROR + name unless collection && level
  @host = @config.resolve_host view_file_path
  @collection = normalize_name collection
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection


9
10
11
# File 'lib/specify/branch_parser.rb', line 9

def collection
  @collection
end

#configObject (readonly)

Returns the value of attribute config


9
10
11
# File 'lib/specify/branch_parser.rb', line 9

def config
  @config
end

#databaseObject (readonly)

Returns the value of attribute database


9
10
11
# File 'lib/specify/branch_parser.rb', line 9

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host


9
10
11
# File 'lib/specify/branch_parser.rb', line 9

def host
  @host
end

#userObject (readonly)

Returns the value of attribute user


9
10
11
# File 'lib/specify/branch_parser.rb', line 9

def user
  @user
end

Class Method Details

.current_branch(config) ⇒ Object

Creates a new instance of BranchParser from the current HEAD.


20
21
22
23
24
25
26
27
28
29
# File 'lib/specify/branch_parser.rb', line 20

def self.current_branch(config)
  stdout_str, stderr_str, status = Open3.capture3(GIT_CURRENT_BRANCH)
  unless status.exitstatus.zero?
    STDERR.puts "There was an error running #{GIT_CURRENT_BRANCH}"
    STDERR.puts stderr_str
    exit 1
  end
  branch = stdout_str.chomp
  new(Dir.pwd, branch, config)
end

Instance Method Details

#levelObject


39
40
41
42
43
44
45
46
47
48
# File 'lib/specify/branch_parser.rb', line 39

def level
  case @level
  when 'collection', 'discipline'
    @level.to_sym
  when 'user'
    { user: @user }
  else
    { user_type: @level.downcase.to_sym }
  end
end

#to_hObject

Returns the BranchParser object's attributes as a hash.


32
33
34
35
36
37
# File 'lib/specify/branch_parser.rb', line 32

def to_h
  { host: host,
    database: database,
    collection: collection,
    level: level }
end