Sha256: b5f625202caf5d048b9d6de2827a334fa72b994a950e7f473847a541fcdd14b8
Contents?: true
Size: 1.99 KB
Versions: 1
Compression:
Stored size: 1.99 KB
Contents
require 'psych' require 'pathname' require 'forwardable' module Canals class Session include Enumerable extend Forwardable BASIC_FIELDS = [:name, :pid, :socket] def initialize @session = load_session(session_file) end def_delegator :@session, :[] def each(&block) @session.each(&block) end def each_obj(&block) @session.map{|sess| CanalOptions.new(fill_from_repository(sess))}.each(&block) end def empty? @session.empty? end def add(session, save=true) @session.push(session) save! if save end def del(name, save=true) @session.delete_if{ |s| s[:name] == name } save! if save end def get(session_id) sess = nil case session_id when Numeric sess = @session.find{ |s| s[:pid] == session_id } when String sess = @session.find{ |s| s[:name] == session_id } end fill_from_repository(sess) if sess end def get_obj(session_id) sess = get(session_id) return nil if sess.nil? CanalOptions.new(sess) end def has?(session_id) get(session_id) != nil end def alive?(session_id) sess = get(session_id) File.exist?(sess[:socket]) end def save! FileUtils.mkdir_p(session_file.dirname) File.open(session_file, 'w') do |file| file.write(Psych.dump(@session)) end end private def session_file file = File.join(Dir.home, '.canals/session') Pathname.new(file) end def load_session(_session_file) valid_file = _session_file && _session_file.exist? && !_session_file.size.zero? return [] if !valid_file return Psych.load_file(_session_file) end def basic?(sess) (sess.keys - BASIC_FIELDS).empty? end def fill_from_repository(sess) if (basic?(sess) && Canals.repository.has?(sess[:name])) Canals.repository.get(sess[:name]).to_hash.merge(sess) else sess end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
canals-0.9.0 | lib/canals/session.rb |