Class: EasyCreds::Templates::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_creds/templates/registry.rb

Constant Summary collapse

BUNDLED_DIR =
File.expand_path('files', __dir__).freeze

Instance Method Summary collapse

Constructor Details

#initialize(global_dir: nil) ⇒ Registry

Returns a new instance of Registry.



8
9
10
# File 'lib/easy_creds/templates/registry.rb', line 8

def initialize(global_dir: nil)
  @global_dir = global_dir
end

Instance Method Details

#delete(name) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
# File 'lib/easy_creds/templates/registry.rb', line 31

def delete(name)
  raise ArgumentError, "Cannot delete bundled template '#{name}'" unless user_defined?(name)

  File.delete(user_path(name))
end

#listObject



12
13
14
# File 'lib/easy_creds/templates/registry.rb', line 12

def list
  (bundled_names + user_names).uniq
end

#load(name) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/easy_creds/templates/registry.rb', line 16

def load(name)
  path = user_path(name) || bundled_path(name)
  raise ArgumentError, "Template '#{name}' not found" unless path

  YAML.safe_load_file(path) || {}
end

#path_for(name) ⇒ Object



27
28
29
# File 'lib/easy_creds/templates/registry.rb', line 27

def path_for(name)
  user_path(name) || bundled_path(name)
end

#user_defined?(name) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/easy_creds/templates/registry.rb', line 23

def user_defined?(name)
  user_path(name) ? true : false
end