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.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cnote/config.rb', line 7

def initialize(path)
  path = File.expand_path path

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

    @note_path = get_note_path

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

    puts "Okay, we're ready to go!"
  else
    conf = YAML.load(File.read(path))

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

Instance Attribute Details

#note_pathObject (readonly)

Returns the value of attribute note_path.



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

def note_path
  @note_path
end

Instance Method Details

#cursorObject



60
61
62
# File 'lib/cnote/config.rb', line 60

def cursor
  @cursor || ">"
end

#editorObject



56
57
58
# File 'lib/cnote/config.rb', line 56

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

#get_note_pathObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cnote/config.rb', line 29

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

#to_hashObject



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

def to_hash
  {
    "note_path" => @note_path
  }
end