Class: Dongjia::Config

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

Class Method Summary collapse

Class Method Details

.check {|config, save_block| ... } ⇒ Object

获取当前保存的配置 提供的 block 中会包含当前配置,以及一个用于保存的 Proc

Yields:

  • (config, save_block)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dongjia_config.rb', line 11

def self.check

  config_root = File.expand_path('~/.cocoapods-dongjia')
  FileUtils.mkdir_p(config_root) unless File.exists?(config_root)
  config_file = File.join(config_root, 'config.json')

  config = {}
  if File.exists?(config_file)
    config = JSON.parse(File.read(config_file))
  end

  save_block = Proc.new do
    File.open(config_file, 'w') do |f|
      f.write(JSON.pretty_generate(config))
    end
  end

  yield config, save_block if block_given?

end

.is_expired?(key, interval) ⇒ Boolean

将 key 字段记录的时间,与当前时间对比,是否超过了 interval(秒)

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dongjia_config.rb', line 33

def self.is_expired?(key, interval)

  check do |config, save| 

    tm = config[key]
    now = Time.now.to_i
    if tm && (now > tm) && (now - tm < interval)
      return
    end

    update_block = Proc.new do
      config[key] = now
      save.call
    end

    yield config, update_block if block_given?

  end

end