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.



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

def initialize(opts = {})
  @opts = opts.transform_keys(&:to_sym)
  # Use only when source_type is git.
  @need_fetch = true
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



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

def branch
  return opts.fetch(:branch) if opts.key?(:branch)

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

#hash(reload = false) ⇒ Object



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

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

#in_repository?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# File 'lib/vars/options.rb', line 68

def in_repository?
  opts[:in_repository] = success?("git rev-parse --git-dir") unless opts.key?(:in_repository)
  # Update remote repositories.
  if opts[:in_repository] && @need_fetch
    execute("git fetch #{remote_name}")
    @need_fetch = false
  end

  opts.fetch(:in_repository)
end

#load_sourceObject



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

def load_source
  src = YAML.safe_load(ERB.new(raw_source, nil, "-").result(Class.new.__binding__), [], [], true)
  return {} if src.nil?

  src.fetch("default", {}).merge(src.fetch(name.to_s))
end

#nameObject



37
38
39
# File 'lib/vars/options.rb', line 37

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

#remote_nameObject



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

def remote_name
  ENV.fetch(EnvKeys::REMOTE_NAME, Defaults::REMOTE_NAME)
end

#repo_pathObject



41
42
43
44
45
46
# File 'lib/vars/options.rb', line 41

def repo_path
  return opts.fetch(:repo_path) if opts.key?(:repo_path)
  return nil unless in_repository?

  opts[:repo_path] = capture("git rev-parse --show-toplevel")
end

#source_typeObject



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

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