Class: Shop::ShopConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/shop/shopconfig.rb

Constant Summary collapse

CONFIG_FILE =
"#{ENV['HOME']}/.shop"

Instance Method Summary collapse

Constructor Details

#initializeShopConfig

Returns a new instance of ShopConfig.



10
11
12
13
14
# File 'lib/shop/shopconfig.rb', line 10

def initialize
  @config = {}
  bootstrap
  get
end

Instance Method Details

#bootstrapObject



36
37
38
39
40
# File 'lib/shop/shopconfig.rb', line 36

def bootstrap
  return if File.exists?(CONFIG_FILE)
  path = template.template_path('shop')
  FileUtils.cp(path, CONFIG_FILE)
end

#editObject

Public: opens the config file in an editor for you to edit. Uses the $EDITOR environment variable, or %EDITOR% on Windows for editing. This method is designed to handle multiple platforms. If $EDITOR is nil, try to open using the open_command.

Stolen from github.com/holman/boom and adapted

Returns a String with a helpful message.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/shop/shopconfig.rb', line 55

def edit
  platform = Platform.new
  unless $EDITOR.nil?
    unless platform.windows?
      system("`echo $EDITOR` #{CONFIG_FILE} &")
    else
      system("start %EDITOR% #{CONFIG_FILE}")
    end
  else
    system("#{platform.open_command} #{CONFIG_FILE}")
  end

  "Make your edits, and do be sure to save."
end

#get(namespace = false, key = false, defaultValue = '') ⇒ Object

Returns the whole config or a specific value

namespace - the namespace where the key is searched key - the key neede defaultValue - default value to return if the value is nil



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/shop/shopconfig.rb', line 21

def get(namespace = false, key = false, defaultValue = '')
  if namespace && key
    value = @config[namespace][key]
    if value
      return value
    else
      return defaultValue
    end
  end

  return @config if !@config.empty?

  get_config
end

#get_configObject



42
43
44
45
# File 'lib/shop/shopconfig.rb', line 42

def get_config
  @config = YAML.load_file(CONFIG_FILE)
  return @config
end

#templateObject



6
7
8
# File 'lib/shop/shopconfig.rb', line 6

def template
  Template.new
end