class Dongjia::Config

Public Class Methods

check() { |config, save_block| ... } click to toggle source

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

# 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) { |config, update_block| ... } click to toggle source

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

# 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