Sha256: 7b3f0775e0f10781a50d6927ab1a21ace35c6470d53435f4a39e0002fc55833f

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

# -*- coding: utf-8 -*-
#
# Copyright 2013 whiteleaf. All rights reserved.
#

require "yaml"
require_relative "narou"

#
# Narou.rbのシステムが記録するデータ単位
#
# .narou ディレクトリにYAMLファイルとして保存される
# scope に :global を指定するとユーザーディレクトリ/.narousetting に保存される
#
module Inventory
  def self.load(name, scope = :local)
    @@cache ||= {}
    return @@cache[name] if @@cache[name]
    {}.tap { |h|
      h.extend(Inventory)
      h.init(name, scope)
      @@cache[name] = h
    }
  end

  def self.clear
    @@cache = {}
  end

  def init(name, scope)
    dir = case scope
          when :local
            Narou.get_local_setting_dir
          when :global
            Narou.get_global_setting_dir
          else
            raise "Unknown scope"
          end
    return nil unless dir
    @mutex = Mutex.new
    @inventory_file_path = File.join(dir, name + ".yaml")
    if File.exist?(@inventory_file_path)
      self.merge!(Helper::CacheLoader.memo(@inventory_file_path) { |yaml|
        YAML.load(yaml)
      })
    end
  end

  def save
    unless @inventory_file_path
      raise "not initialized setting dir yet"
    end
    @mutex.synchronize do
      File.write(@inventory_file_path, YAML.dump(self))
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
narou-2.9.5 lib/inventory.rb
narou-2.9.4 lib/inventory.rb
narou-2.9.3.1 lib/inventory.rb
narou-2.9.3 lib/inventory.rb
narou-2.9.2 lib/inventory.rb
narou-2.9.1 lib/inventory.rb
narou-2.9.0 lib/inventory.rb
narou-2.8.3.1 lib/inventory.rb
narou-2.8.3 lib/inventory.rb
narou-2.8.2 lib/inventory.rb
narou-2.8.1 lib/inventory.rb
narou-2.8.0 lib/inventory.rb
narou-2.7.2 lib/inventory.rb
narou-2.7.1 lib/inventory.rb
narou-2.7.0 lib/inventory.rb