Class: Ytterb::Util::Settings

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

Instance Method Summary collapse

Constructor Details

#initializeSettings

Returns a new instance of Settings.



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

def initialize
  @cache_dir = ".cache"
  @options = {
    :settings_file => File.join("settings",DataPersistHelper.get_extension()),
    :settings_data_dir => [@cache_dir, "local_settings"],
    :cached_symbol_data_dir => [@cache_dir, "cached_symbol_data"],
    :raw_symbol_list_data_dir => [@cache_dir, "raw_symbol_data"]
  }.freeze
  @local_path =  File.expand_path(File.dirname(__FILE__)).
    split(File::SEPARATOR).
    reverse.drop(1).reverse.
    join(File::SEPARATOR)

  # create the cache dirs if they don's exist
  @options.each do |option, value|
    options_s = option.to_s
    next unless options_s.end_with?("data_dir")
    FileUtils.mkdir_p(File.join(@local_path, @options[option]))
  end
  load
end

Instance Method Details

#[](key) ⇒ Object



59
60
61
# File 'lib/ytterb/util/settings.rb', line 59

def [](key)
  @local_settings[key]
end

#[]=(key, value) ⇒ Object



63
64
65
# File 'lib/ytterb/util/settings.rb', line 63

def []=(key, value)
  @local_settings[key] = value
end

#create_full_path_and_file(full_path) ⇒ Object



36
37
38
39
40
41
# File 'lib/ytterb/util/settings.rb', line 36

def create_full_path_and_file(full_path) 
  FileUtils.mkdir_p(File.dirname(full_path))
  unless File.file?(full_path)
    FileUtils.touch(full_path)
  end
end

#full_path_settings_fileObject



43
44
45
# File 'lib/ytterb/util/settings.rb', line 43

def full_path_settings_file
  File.join(@local_path,@options[:settings_data_dir],@options[:settings_file])
end

#get_raw_symbol_list_data_dirObject



55
56
57
# File 'lib/ytterb/util/settings.rb', line 55

def get_raw_symbol_list_data_dir
  File.join(@local_path,@options[:raw_symbol_list_data_dir])
end

#get_symbol_store_file(symbol, build_path_and_file = true) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/ytterb/util/settings.rb', line 47

def get_symbol_store_file(symbol, build_path_and_file = true)
  split_for_storage = symbol.split('')
  split_for_storage[-1] += DataPersistHelper.get_extension()
  full_path = File.join(@local_path,@options[:cached_symbol_data_dir],split_for_storage)
  create_full_path_and_file(full_path)  if build_path_and_file == true
  full_path
end

#loadObject



67
68
69
70
71
72
# File 'lib/ytterb/util/settings.rb', line 67

def load
  @local_settings = DataPersistHelper.load(full_path_settings_file())
rescue
ensure
  set_defaults_if_uninitialized
end

#saveObject



74
75
76
77
# File 'lib/ytterb/util/settings.rb', line 74

def save
  create_full_path_and_file(full_path_settings_file())
  DataPersistHelper.save(full_path_settings_file(), @local_settings)
end

#set_defaults_if_uninitializedObject



28
29
30
31
32
33
34
# File 'lib/ytterb/util/settings.rb', line 28

def set_defaults_if_uninitialized
  # defaults to be used if not defined
  @local_settings ||= {}
  @local_settings[:sync_from] ||= (Date.today-365).to_s
  @local_settings[:sync_increment] ||= 120
  @local_settings[:api_calls_per_hour] ||= 1900
end