Class: Specify::BranchParser

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

Overview

BranchParsers parse the information required to set a Specify::Service::ViewLoade#target for upload of .views.xml files from a string that follows the convention Database/CollectionName/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 BranchParser with view_file_path and name.

view_file_path: the directory path of the .vews.xml file (that path must be mapped to a host name in the config).

name: a String with a branch name conforming to the convention Database/CollectionName/level.

config: a database configuration YAML file.

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
# File 'lib/specify/branch_parser.rb', line 50

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)

The name of the collection. Must be an existing Specify::Model::Collection#name.



13
14
15
# File 'lib/specify/branch_parser.rb', line 13

def collection
  @collection
end

#configObject (readonly)

A Specify::Configuration::HostConfig.



16
17
18
# File 'lib/specify/branch_parser.rb', line 16

def config
  @config
end

#databaseObject (readonly)

The name of a Specify database.



19
20
21
# File 'lib/specify/branch_parser.rb', line 19

def database
  @database
end

#hostObject (readonly)

The name of a MySQL/MariaDB host.



22
23
24
# File 'lib/specify/branch_parser.rb', line 22

def host
  @host
end

#userObject (readonly)

The name of a Specify user (an existing Specify::Model::User#name).



25
26
27
# File 'lib/specify/branch_parser.rb', line 25

def user
  @user
end

Class Method Details

.current_branch(config) ⇒ Object

Creates a new instance of BranchParser for the current Git HEAD.

config: a database configuration YAML file.



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

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

Returns the level to a Specify::Service::ViewLoader will upload.



67
68
69
70
71
72
73
74
75
76
# File 'lib/specify/branch_parser.rb', line 67

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 attributes of self as a hash.



59
60
61
62
63
64
# File 'lib/specify/branch_parser.rb', line 59

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