├── Rakefile ├── lib └── ruboty │ ├── local_yaml.rb │ ├── local_yaml │ └── version.rb │ └── brains │ └── local_yaml.rb ├── Gemfile ├── .gitignore ├── README.md └── ruboty-local_yaml.gemspec /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | -------------------------------------------------------------------------------- /lib/ruboty/local_yaml.rb: -------------------------------------------------------------------------------- 1 | require 'ruboty/local_yaml/version' 2 | require 'ruboty/brains/local_yaml' 3 | 4 | -------------------------------------------------------------------------------- /lib/ruboty/local_yaml/version.rb: -------------------------------------------------------------------------------- 1 | module Ruboty 2 | module LocalYaml 3 | VERSION = '0.1.0' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in ruboty-local_yaml.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ruboty::LocalYaml 2 | 3 | Store [Ruboty](https://github.com/r7kamura/ruboty/)'s memory in local yaml file. 4 | 5 | ## Usage 6 | ``` 7 | # Gemfile 8 | gem 'ruboty-local_yaml' 9 | ``` 10 | 11 | ## ENV 12 | ``` 13 | LOCAL_YAML_PATH # Yaml path (default: ruboty.yml) 14 | LOCAL_YAML_INTERVAL # Interval time to write (default: 5) 15 | ``` 16 | 17 | -------------------------------------------------------------------------------- /ruboty-local_yaml.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'ruboty/local_yaml/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'ruboty-local_yaml' 8 | spec.version = Ruboty::LocalYaml::VERSION 9 | spec.authors = ['ru_shalm'] 10 | spec.email = ['ru_shalm@hazimu.com'] 11 | 12 | spec.summary = %q{Store Ruboty's memory in local yaml file.} 13 | spec.homepage = 'https://github.com/rutan/ruboty-local_yaml' 14 | spec.license = 'MIT' 15 | 16 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 17 | spec.bindir = 'exe' 18 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 19 | spec.require_paths = ['lib'] 20 | 21 | spec.add_dependency 'ruboty' 22 | spec.add_development_dependency 'bundler', '~> 1.10' 23 | spec.add_development_dependency 'rake', '~> 10.0' 24 | end 25 | -------------------------------------------------------------------------------- /lib/ruboty/brains/local_yaml.rb: -------------------------------------------------------------------------------- 1 | require 'yaml' 2 | 3 | module Ruboty 4 | module Brains 5 | class LocalYAML < Base 6 | env :LOCAL_YAML_PATH, 'Yaml path', optional: true 7 | env :LOCAL_YAML_SAVE_INTERVAL, 'Interval time to write', optional: true 8 | 9 | def initialize 10 | super 11 | @thread = Thread.new { sync } 12 | @thread.abort_on_exception = true 13 | end 14 | 15 | def data 16 | @data ||= pull || {} 17 | end 18 | 19 | private 20 | 21 | def push 22 | yaml = data.to_yaml 23 | return if yaml == @old_yaml 24 | File.open(file_path, 'w') { |f| f.write yaml } 25 | @old_yaml = yaml 26 | end 27 | 28 | def pull 29 | return nil unless File.exist?(file_path) 30 | YAML.load File.read(file_path) 31 | end 32 | 33 | def sync 34 | loop do 35 | wait 36 | push 37 | end 38 | end 39 | 40 | def wait 41 | sleep(interval) 42 | end 43 | 44 | def file_path 45 | (ENV['LOCAL_YAML_PATH'] || './ruboty.yml') 46 | end 47 | 48 | def interval 49 | (ENV['LOCAL_YAML_SAVE_INTERVAL'] || 5).to_i 50 | end 51 | end 52 | end 53 | end 54 | --------------------------------------------------------------------------------