Class: Librails::Settings

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/librails/settings.rb

Class Method Summary collapse

Class Method Details

.get_array(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/librails/settings.rb', line 40

def self.get_array(key)
  value = get_value(key)
  return nil unless value
  values = nil
  if value =~ /^---/
    # Rails Settings Cachedは配列をYAML形式で保存している
    # "---\n- bootstrap\n- chatgpt\n- git\n- github\n- javascript\n
    value = value.sub(/^---\n/, '')
    values = value.split(/\n/).collect {|v| v.sub(/^- /, '').strip}
  else
    # 新方式は単なるtsvとする
    values = value.split(/\t/)
  end
  values
end

.get_int(key) ⇒ Object



22
23
24
25
26
# File 'lib/librails/settings.rb', line 22

def self.get_int(key)
  value = get_value(key)
  return nil unless value
  value.to_i
end

.get_record(key) ⇒ Object



70
71
72
# File 'lib/librails/settings.rb', line 70

def self.get_record(key)
  Settings.find_by(var: key)
end

.get_str(key) ⇒ Object



18
19
20
# File 'lib/librails/settings.rb', line 18

def self.get_str(key)
  get_value(key)
end

.get_time(key) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/librails/settings.rb', line 28

def self.get_time(key)
  value = get_value(key)
  return nil unless value
  result = nil
  begin
    result = Time.zone.parse(value)
  rescue => e
    puts e.message
  end
  result
end

.get_value(key) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/librails/settings.rb', line 60

def self.get_value(key)
  settings = get_record(key)
  value = nil
  if settings
    # Rails::Settings::Cachedが先頭に"-- "を付けているのでそれを除去する。互換性のための処理
    value = normalize_value(settings.value)
  end
  value
end

.normalize_value(value) ⇒ Object



56
57
58
# File 'lib/librails/settings.rb', line 56

def self.normalize_value(value)
  value.sub(/^--- /, '').strip
end

.set_value(key, value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/librails/settings.rb', line 74

def self.set_value(key, value)
  settings = get_record(key)
  unless settings
    settings = Settings.new
    settings.var = key
  end
  str = nil
  if value.kind_of?(Array)
    str = value.join("\t") # タブ区切りで保存
  else
    str = value.to_s
  end
  settings.value = str
  settings.save!
end