Class: Mchat::Store
- Inherits:
-
Object
- Object
- Mchat::Store
- Defined in:
- lib/mchat/store.rb
Instance Attribute Summary collapse
-
#store_messages_reader_run ⇒ Object
Returns the value of attribute store_messages_reader_run.
Instance Method Summary collapse
- #create_store ⇒ Object
- #get_store ⇒ Object
- #hook_quit ⇒ Object
-
#initialize(opt = {}) ⇒ Store
constructor
A new instance of Store.
- #message_loop_reader ⇒ Object
- #message_writer(content) ⇒ Object
- #messages_history(count) ⇒ Object
- #store_exist? ⇒ Boolean
Constructor Details
#initialize(opt = {}) ⇒ Store
Returns a new instance of Store.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mchat/store.rb', line 8 def initialize(opt={}) # TODO use path to read dir @store_dir = Pathname.new(Dir.home).join('.mchat') @store_path = opt[:store_path] || Pathname.new(Dir.home).join('.mchat').join('mchatdb') @store_sync_time = opt[:store_sync_time] || 1 @store_async_flag = opt[:store_async_flag] || false @field_name = opt[:field_name] @field_history_name = "#{@field_name.to_s}_history".to_sym @store_messages_reader_run = true @store = nil end |
Instance Attribute Details
#store_messages_reader_run ⇒ Object
Returns the value of attribute store_messages_reader_run.
7 8 9 |
# File 'lib/mchat/store.rb', line 7 def @store_messages_reader_run end |
Instance Method Details
#create_store ⇒ Object
23 24 25 26 27 |
# File 'lib/mchat/store.rb', line 23 def create_store require 'fileutils' FileUtils.mkdir_p(@store_dir) unless File.exist?(@store_dir) PStore.new(@store_path) end |
#get_store ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mchat/store.rb', line 29 def get_store if !store_exist? create_store end @store = ::PStore.new(@store_path) @store.transaction do @store[@field_history_name] ||= Array.new @store[@field_name] ||= Array.new end @store end |
#hook_quit ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/mchat/store.rb', line 86 def hook_quit @store.transaction do = @store[@field_name] @store[@field_history_name] += @store[@field_name] = [] end end |
#message_loop_reader ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mchat/store.rb', line 60 def get_store thx = Thread.new { while @store_messages_reader_run do @store.transaction do = @store[@field_name] .each do |m| end # puts m if block_given? yield() end @store[@field_history_name] += @store[@field_name] = [] end sleep @store_sync_time end } if(!@store_async_flag) thx.join end return thx end |
#message_writer(content) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mchat/store.rb', line 49 def (content) get_store @store.transaction do if content.is_a? Array @store[@field_name] += content else @store[@field_name] << content end end end |
#messages_history(count) ⇒ Object
44 45 46 47 |
# File 'lib/mchat/store.rb', line 44 def (count) = @store[@field_history_name].last(count) return end |
#store_exist? ⇒ Boolean
20 21 22 |
# File 'lib/mchat/store.rb', line 20 def store_exist? File.exist? @store_path end |