Class: Mygithub::Settings

Inherits:
Object
  • Object
show all
Defined in:
lib/mygithub/settings.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = Settings.default_filename) ⇒ Settings

Returns a new instance of Settings.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mygithub/settings.rb', line 25

def initialize(filename = Settings.default_filename)
  @filename = filename
  
  if File.exist?(@filename)
    open(@filename) do |f|
      @data = YAML.load(f.read)
    end
  else
    @data = {
      'username' => '',
      'token'    => ''
    }
    @is_empty = true
  end
end

Class Method Details

.default_databaseObject



21
22
23
# File 'lib/mygithub/settings.rb', line 21

def self.default_database
  File.join default_dir, 'database'
end

.default_dirObject



13
14
15
# File 'lib/mygithub/settings.rb', line 13

def self.default_dir
  File.expand_path('~/.mygithub')
end

.default_filenameObject



17
18
19
# File 'lib/mygithub/settings.rb', line 17

def self.default_filename
  File.join default_dir, 'mygithub.yaml'
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mygithub/settings.rb', line 65

def empty?
  !@is_empty.nil?
end

#saveObject



41
42
43
44
45
46
47
# File 'lib/mygithub/settings.rb', line 41

def save
  FileUtils.mkdir_p File.dirname(@filename)
  
  open(@filename, "w") do |f|
    f.write YAML.dump(@data)
  end
end

#tokenObject



57
58
59
# File 'lib/mygithub/settings.rb', line 57

def token
  @data['token']
end

#token=(token) ⇒ Object



61
62
63
# File 'lib/mygithub/settings.rb', line 61

def token=(token)
  @data['token'] = token
end

#usernameObject



49
50
51
# File 'lib/mygithub/settings.rb', line 49

def username
  @data['username']
end

#username=(username) ⇒ Object



53
54
55
# File 'lib/mygithub/settings.rb', line 53

def username=(username)
  @data['username'] = username
end