Class: Mchat::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mchat/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_runObject

Returns the value of attribute store_messages_reader_run.



7
8
9
# File 'lib/mchat/store.rb', line 7

def store_messages_reader_run
  @store_messages_reader_run
end

Instance Method Details

#create_storeObject



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_storeObject



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_quitObject



86
87
88
89
90
91
92
# File 'lib/mchat/store.rb', line 86

def hook_quit
  @store.transaction do
    messages = @store[@field_name]
    @store[@field_history_name] += messages
    @store[@field_name] = []
  end
end

#message_loop_readerObject



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 message_loop_reader
  get_store
  thx = Thread.new {
    while @store_messages_reader_run do
      @store.transaction do
        messages = @store[@field_name]
        messages.each do |m|
        end
        # puts m
        if block_given?
          yield(messages)
        end
        @store[@field_history_name] += messages
        @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 message_writer(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 messages_history(count)
  last_messages = @store[@field_history_name].last(count)
  return last_messages
end

#store_exist?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/mchat/store.rb', line 20

def store_exist?
  File.exist? @store_path
end