Sha256: 6a29340ae7d829a28a635363298e3573d1066f494df32b193072291dceeaa850

Contents?: true

Size: 1.08 KB

Versions: 4

Compression:

Stored size: 1.08 KB

Contents

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

require "yaml"
require_relative "narou"

#
# Narou.rbのシステムが記録するデータ単位
#
# .narou ディレクトリにYAMLファイルとして保存される
#
module Inventory
  def self.load(name, scope)
    @@cache ||= {}
    return @@cache[name] if @@cache[name]
    {}.tap { |h|
      h.extend(Inventory)
      h.init(name, scope)
      @@cache[name] = h
    }
  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
    @inventory_file_path = File.join(dir, name + ".yaml")
    if File.exists?(@inventory_file_path)
      self.merge!(YAML.load_file(@inventory_file_path))
    end
  end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
narou-1.6.4 lib/inventory.rb
narou-1.6.3 lib/inventory.rb
narou-1.6.1 lib/inventory.rb
narou-1.6.0 lib/inventory.rb