Class: DevContext::Config
- Inherits:
-
Object
- Object
- DevContext::Config
- Defined in:
- lib/dev_context/config.rb
Constant Summary collapse
- DEFAULT_PATH =
File.("~/.dxcf").freeze
- DEFAULTS =
{ "version" => 1, "contexts" => {}, "active_stack" => [], "repos" => {}, "clone_roots" => [] }.freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #activate_context!(context_name) ⇒ Object
- #active_contexts ⇒ Object
- #active_stack ⇒ Object
- #add_context(name:, repo_path:, branch:) ⇒ Object
- #contexts ⇒ Object
- #deactivate_context!(context_name) ⇒ Object
- #get_context(name_or_path) ⇒ Object
- #init! ⇒ Object
-
#initialize(path: ENV.fetch("DX_CONFIG_PATH", DEFAULT_PATH)) ⇒ Config
constructor
A new instance of Config.
- #initialized? ⇒ Boolean
- #normalize_name(name) ⇒ Object
- #repos ⇒ Object
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
17 18 19 |
# File 'lib/dev_context/config.rb', line 17 def data @data end |
#path ⇒ Object (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_contexts ⇒ Object
87 88 89 |
# File 'lib/dev_context/config.rb', line 87 def active_contexts active_stack.filter_map { |name| contexts[name] } end |
#active_stack ⇒ Object
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:) = Time.now.utc.iso8601 context_name = normalize_name(name) contexts[context_name] = { "name" => context_name, "repo_path" => File.(repo_path), "branch" => branch, "created_at" => contexts[context_name]&.fetch("created_at", ) || , "last_used_at" => } touch_repo!(repo_path) save! context_name end |
#contexts ⇒ Object
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.(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
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 |
#repos ⇒ Object
40 41 42 |
# File 'lib/dev_context/config.rb', line 40 def repos data.fetch("repos") end |