Class: Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Config

Returns a new instance of Config.



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cnote/config.rb', line 9

def initialize(path)
  @path = File.expand_path(path)

  if !File.exists?(@path)
    puts "Welcome, new user!"

    @note_path = get_note_path
    save
    puts "Okay, we're ready to go!"
  else
    load
  end
end

Instance Attribute Details

#editorObject



50
51
52
# File 'lib/cnote/config.rb', line 50

def editor
  @editor || ENV["EDITOR"]
end

#note_pathObject (readonly)

Returns the value of attribute note_path.



6
7
8
# File 'lib/cnote/config.rb', line 6

def note_path
  @note_path
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/cnote/config.rb', line 6

def path
  @path
end

#promptObject



54
55
56
# File 'lib/cnote/config.rb', line 54

def prompt
  @prompt || ">"
end

Instance Method Details

#get(key) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/cnote/config.rb', line 68

def get(key)
  case key.downcase
  when 'editor'
    editor
  when 'prompt'
    prompt
  end
end

#get_note_pathObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cnote/config.rb', line 23

def get_note_path
  path = nil

  while !path or !File.exists? path
    print "Enter a path for your note folder: "

    path = File.expand_path gets.chomp
    
    if File.exists? path
      if !File.directory? path
        puts "Hey, that's not a folder!"
      end
    else
      puts "That folder doesn't exist yet. Do you want to create it?"
      case gets.strip.downcase
      when "y", "yes", "yeah", "sure", "ok", "okay", "alright", "yep", "yup"
        FileUtils.mkdir_p path
        puts "Done!"
      else
        puts "Okay."
      end
    end
  end

  return path
end

#loadObject



83
84
85
86
87
88
89
# File 'lib/cnote/config.rb', line 83

def load
  conf = YAML.load(File.read(@path))

  @note_path = conf["note_path"]
  @editor = conf["editor"]
  @prompt = conf["prompt"]
end


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

def print
  ap to_hash
end

#saveObject



77
78
79
80
81
# File 'lib/cnote/config.rb', line 77

def save
  File.open(@path, "w") do |file|
    file.write(YAML.dump(to_hash))
  end
end

#set(key, val) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/cnote/config.rb', line 58

def set(key, val)
  case key.downcase
  when 'editor'
    @editor = val
  when 'prompt'
    @prompt = val
  end
  save
end

#to_hashObject



95
96
97
98
99
100
101
# File 'lib/cnote/config.rb', line 95

def to_hash
  hash = Hash.new
  hash["note_path"] = @note_path if @note_path
  hash["editor"] = @editor if @editor
  hash["prompt"] = @prompt if @prompt
  return hash
end