Class: S7::World

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/s7/world.rb

Overview

アプリケーション全体を表現する。

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GetText

#N_, #_, bindtextdomain, included

Constructor Details

#initializeWorld

Returns a new instance of World.



39
40
41
42
43
44
45
46
# File 'lib/s7/world.rb', line 39

def initialize
  @base_dir = File.join(Utils::home_dir, ".s7")
  @logger = Logger.new(STDERR)
  @entry_collection = EntryCollection.new
  @undo_stack = UndoStack.new
  @configuration = Configuration.new(self)
  @changed = false
end

Instance Attribute Details

#base_dirObject

設定や機密情報などを格納するディレクトリ。



19
20
21
# File 'lib/s7/world.rb', line 19

def base_dir
  @base_dir
end

#changedObject

変更があるかどうか。



37
38
39
# File 'lib/s7/world.rb', line 37

def changed
  @changed
end

#configurationObject (readonly)

Configuration オブジェクト。



34
35
36
# File 'lib/s7/world.rb', line 34

def configuration
  @configuration
end

#entry_collectionObject (readonly)

機密情報の集合。



28
29
30
# File 'lib/s7/world.rb', line 28

def entry_collection
  @entry_collection
end

#loggerObject

Logger のインスタンス。



22
23
24
# File 'lib/s7/world.rb', line 22

def logger
  @logger
end

#master_keyObject

マスターキー。



25
26
27
# File 'lib/s7/world.rb', line 25

def master_key
  @master_key
end

#undo_stackObject (readonly)

UndoStack オブジェクト。



31
32
33
# File 'lib/s7/world.rb', line 31

def undo_stack
  @undo_stack
end

Instance Method Details

#configuration_pathObject

設定を格納するファイルのパスを取得する。



59
60
61
# File 'lib/s7/world.rb', line 59

def configuration_path
  return File.join(base_dir, "configuration")
end

#dumpObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/s7/world.rb', line 95

def dump
  hash = {}
  hash["entries"] = entry_collection.entries.collect { |entry|
    {
      "attributes" => entry.attributes.collect { |attr|
        {
          "type" => attr.type,
          "name" => attr.name,
          "value" => attr.value,
          "secret" => attr.secret?,
          "editabled" => attr.editable?,
          "protected" => attr.protected?,
        }
      },
      "tags" => entry.tags,
    }
  }
  return hash.to_yaml
end

#load(data) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/s7/world.rb', line 115

def load(data)
  hash = YAML.load(data)
  hash["entries"].each do |entry_hash|
    entry = Entry.new
    attributes = entry_hash["attributes"].collect { |attr_hash|
      type = attr_hash["type"]
      attr_names = ["name", "value", "secret", "editabled", "protected"]
      Attribute.create_instance(type, 
                                attr_hash.select { |key, value|
                                  attr_names.include?(key)
                                })
    }
    entry.add_attributes(attributes)
    entry.tags.push(*entry_hash["tags"])
    entry_collection.add_entries(entry)
  end
end

#lockObject

2 重起動を防止するため、lock ファイルを作成する。 すでに s7 を起動していた場合、ApplicationError 例外を発生させる。



72
73
74
75
76
77
78
79
80
81
# File 'lib/s7/world.rb', line 72

def lock
  if File.exist?(lock_path)
    pid = File.read(lock_path).to_i
    raise ApplicationError, _("running other s7: pid=<%d>") % pid
  end
  File.open(lock_path, "w") do |f|
    f.flock(File::LOCK_EX)
    f.write(Process.pid)
  end
end

#lock_pathObject

2 重起動を防止するためにロックするためのファイルのパスを取得する。



49
50
51
# File 'lib/s7/world.rb', line 49

def lock_path
  return File.join(base_dir, "lock")
end

#save(force = false) ⇒ Object

情報をファイルに書き出す。 すでに secrets ファイルが存在してれば、.bak というサフィックスを 追加してバックアップを取る。



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/s7/world.rb', line 136

def save(force = false)
  if force || @changed
    if File.exist?(secrets_path)
      FileUtils.cp(secrets_path, secrets_path + ".bak")
    end
    S7File.write(secrets_path,
                 master_key,
                 configuration.cipher_type,
                 dump)
    configuration.save(configuration_path)
    @changed = false
  end
end

#secrets_pathObject

機密情報を格納するファイルのパスを取得する。



54
55
56
# File 'lib/s7/world.rb', line 54

def secrets_path
  return File.join(base_dir, "secrets")
end

#start_loggingObject

ログの記録を開始する。



64
65
66
67
68
# File 'lib/s7/world.rb', line 64

def start_logging
  log_path = File.join(base_dir, "s7.log")
  self.logger = Logger.new(log_path)
  logger.level = Logger::DEBUG
end

#unlockObject

lock ファイルを削除する。



84
85
86
87
88
89
90
91
92
93
# File 'lib/s7/world.rb', line 84

def unlock
  if File.exist?(lock_path)
    pid = File.read(lock_path).to_i
    if pid == Process.pid
      FileUtils.rm(lock_path)
    else
      raise ApplicationError, _("running other s7: pid=<%d>") % pid
    end
  end
end