Class: DevContext::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/dev_context/config.rb

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.dxcf").freeze
DEFAULTS =
{
  "version" => 1,
  "contexts" => {},
  "active_stack" => [],
  "repos" => {},
  "clone_roots" => []
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: ENV.fetch("DX_CONFIG_PATH", DEFAULT_PATH)) ⇒ Config

Returns a new instance of Config.



19
20
21
22
# File 'lib/dev_context/config.rb', line 19

def initialize(path: ENV.fetch("DX_CONFIG_PATH", DEFAULT_PATH))
  @path = path
  @data = load_data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/dev_context/config.rb', line 17

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/dev_context/config.rb', line 17

def path
  @path
end

Instance Method Details

#activate_context!(context_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/dev_context/config.rb', line 69

def activate_context!(context_name)
  name = normalize_name(context_name)
  context = contexts.fetch(name)

  active_stack.delete(name)
  active_stack.unshift(name)
  context["last_used_at"] = Time.now.utc.iso8601
  save!
  context
end

#active_contextsObject



87
88
89
# File 'lib/dev_context/config.rb', line 87

def active_contexts
  active_stack.filter_map { |name| contexts[name] }
end

#active_stackObject



44
45
46
# File 'lib/dev_context/config.rb', line 44

def active_stack
  data.fetch("active_stack")
end

#add_context(name:, repo_path:, branch:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dev_context/config.rb', line 48

def add_context(name:, repo_path:, branch:)
  timestamp = Time.now.utc.iso8601
  context_name = normalize_name(name)

  contexts[context_name] = {
    "name" => context_name,
    "repo_path" => File.expand_path(repo_path),
    "branch" => branch,
    "created_at" => contexts[context_name]&.fetch("created_at", timestamp) || timestamp,
    "last_used_at" => timestamp
  }
  touch_repo!(repo_path)
  save!
  context_name
end

#contextsObject



36
37
38
# File 'lib/dev_context/config.rb', line 36

def contexts
  data.fetch("contexts")
end

#deactivate_context!(context_name) ⇒ Object



80
81
82
83
84
85
# File 'lib/dev_context/config.rb', line 80

def deactivate_context!(context_name)
  name = normalize_name(context_name)
  removed = active_stack.delete(name)
  save! if removed
  !!removed
end

#get_context(name_or_path) ⇒ Object



64
65
66
67
# File 'lib/dev_context/config.rb', line 64

def get_context(name_or_path)
  key = normalize_name(name_or_path)
  contexts[key] || contexts.values.find { |ctx| ctx["repo_path"] == File.expand_path(name_or_path) }
end

#init!Object



28
29
30
31
32
33
34
# File 'lib/dev_context/config.rb', line 28

def init!
  return false if initialized?

  @data = DEFAULTS.dup
  save!
  true
end

#initialized?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/dev_context/config.rb', line 24

def initialized?
  File.exist?(path)
end

#normalize_name(name) ⇒ Object



91
92
93
# File 'lib/dev_context/config.rb', line 91

def normalize_name(name)
  name.to_s.strip.downcase
end

#reposObject



40
41
42
# File 'lib/dev_context/config.rb', line 40

def repos
  data.fetch("repos")
end