Class: Lionel::Configuration

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Singleton
Defined in:
lib/lionel/configuration.rb

Constant Summary collapse

FILE_NAME =
'.lionelrc'
CONFIG_ACCESSORS =
[
  :trello_key, :trello_token, :trello_board_id,
  :google_token, :google_refresh_token,
  :google_client_id, :google_client_secret,
  :google_doc_id
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



31
32
33
34
# File 'lib/lionel/configuration.rb', line 31

def initialize
  @path = File.join(File.expand_path("~"), FILE_NAME)
  @data = OpenStruct.new(load_data)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/lionel/configuration.rb', line 8

def data
  @data
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/lionel/configuration.rb', line 8

def path
  @path
end

Class Method Details

.config_accessor(*args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/lionel/configuration.rb', line 18

def self.config_accessor(*args)
  def_delegators :data, *args

  args.each do |accessor|
    define_method("#{accessor}=") do |value|
      data.send("#{accessor}=", value)
      write
    end
  end
end

Instance Method Details

#default_dataObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lionel/configuration.rb', line 50

def default_data
  # {
  #   'trello_key' => ENV['TRELLO_KEY'],
  #   'trello_token' => ENV['TRELLO_TOKEN'],
  #   'trello_board_id' => ENV['TRELLO_BOARD_ID'],
  #   'google_token' => ENV['GOOGLE_TOKEN'],
  #   'google_refresh_token' => ENV['GOOGLE_REFRESH_TOKEN'],
  #   'google_doc_id' => ENV['GOOGLE_DOC_ID']
  #   'google_client_id' => ENV['GOOGLE_CLIENT_ID']
  #   'google_client_secret' => ENV['GOOGLE_CLIENT_SECRET']
  # }
  {}.tap do |data|
    CONFIG_ACCESSORS.each do |name|
      data[name] = ENV[name.to_s.upcase]
    end
  end
end

#load_dataObject



43
44
45
46
47
48
# File 'lib/lionel/configuration.rb', line 43

def load_data
  load_file
rescue Errno::ENOENT
  Lionel.logger.info "Couldn't load file, falling back to ENV"
  default_data
end

#load_fileObject



68
69
70
71
# File 'lib/lionel/configuration.rb', line 68

def load_file
  require 'yaml'
  YAML.load_file(@path)
end

#save(attrs = {}) ⇒ Object



36
37
38
39
40
41
# File 'lib/lionel/configuration.rb', line 36

def save(attrs = {})
  attrs.each do |accessor, value|
    data.send("#{accessor}=", value)
  end
  write
end

#writeObject



73
74
75
76
77
78
# File 'lib/lionel/configuration.rb', line 73

def write
  require 'yaml'
  File.open(@path, File::RDWR|File::TRUNC|File::CREAT, 0600) do |rcfile|
    rcfile.write @data.marshal_dump.to_yaml
  end
end