Class: Vars::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/vars/options.rb

Defined Under Namespace

Modules: Defaults, EnvKeys

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Options

Returns a new instance of Options.



17
18
19
# File 'lib/vars/options.rb', line 17

def initialize(opts = {})
  @opts = opts.transform_keys(&:to_sym)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/vars/options.rb', line 3

def opts
  @opts
end

Instance Method Details

#branchObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/vars/options.rb', line 46

def branch
  case
  when opts.key?(:branch)
    opts.fetch(:branch)
  when ENV.key?(EnvKeys::BRANCH)
    opts[:branch] = ENV.fetch(EnvKeys::BRANCH)
  when in_repository?
    opts[:branch] = capture("git symbolic-ref --short HEAD")
  else
    Defaults::BRANCH
  end
end

#hash(reload = false) ⇒ Object



21
22
23
24
# File 'lib/vars/options.rb', line 21

def hash(reload = false)
  @hash = nil if reload
  @hash ||= load_source
end

#in_repository?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/vars/options.rb', line 63

def in_repository?
  opts[:in_repository] = success?("git rev-parse --git-dir") unless opts.key?(:in_repository)
  opts.fetch(:in_repository)
end

#load_sourceObject



26
27
28
29
# File 'lib/vars/options.rb', line 26

def load_source
  src = YAML.safe_load(ERB.new(raw_source, nil, "-").result(Class.new.__binding__), [], [], true)
  src.fetch("default", {}).merge(src.fetch(name.to_s))
end

#nameObject



31
32
33
# File 'lib/vars/options.rb', line 31

def name
  opts.fetch(:name, ENV.fetch(EnvKeys::ENV_NAME, Defaults::ENV_NAME))
end

#repo_pathObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/vars/options.rb', line 35

def repo_path
  case
  when opts.key?(:repo_path)
    opts.fetch(:repo_path)
  when in_repository?
    opts[:repo_path] = capture("git rev-parse --show-toplevel")
  else
    nil
  end
end

#source_typeObject



59
60
61
# File 'lib/vars/options.rb', line 59

def source_type
  opts.fetch(:source_type, Defaults::SOURCE_TYPE)
end