Class: Outil::OCS::Index
- Inherits:
-
Object
- Object
- Outil::OCS::Index
- Defined in:
- lib/outil/ocs/index.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #all ⇒ Object
- #append(name, ast) ⇒ Object
- #hard_reset! ⇒ Object
- #index_path ⇒ Object
- #indexed_names ⇒ Object
-
#initialize(params = {}) ⇒ Index
constructor
A new instance of Index.
- #read(name) ⇒ Object
- #read_exec(name) ⇒ Object
- #update_index(name) ⇒ Object
- #write_serialize(name, ast) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Index
Returns a new instance of Index.
7 8 9 |
# File 'lib/outil/ocs/index.rb', line 7 def initialize params={} @path = params[:path] end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
5 6 7 |
# File 'lib/outil/ocs/index.rb', line 5 def path @path end |
Instance Method Details
#all ⇒ Object
45 46 47 48 49 |
# File 'lib/outil/ocs/index.rb', line 45 def all indexed_names.map do |name| read name end end |
#append(name, ast) ⇒ Object
15 16 17 18 |
# File 'lib/outil/ocs/index.rb', line 15 def append name, ast write_serialize name, ast update_index name end |
#hard_reset! ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/outil/ocs/index.rb', line 57 def hard_reset! Dir["#{@path}/*.ast"].each do |path| File.delete path end File.open(index_path, 'w+') do |file| file.write String.new end end |
#index_path ⇒ Object
11 12 13 |
# File 'lib/outil/ocs/index.rb', line 11 def index_path @index_path ||= "#{@path}/index" end |
#indexed_names ⇒ Object
26 27 28 29 30 |
# File 'lib/outil/ocs/index.rb', line 26 def indexed_names File.open(index_path, 'r') do |f| f.read.split(/\n/) end.map(&:strip) end |
#read(name) ⇒ Object
39 40 41 42 43 |
# File 'lib/outil/ocs/index.rb', line 39 def read(name) File.open("#{@path}/#{name}.ast", 'r') do |file| YAML.load(file.read) end end |
#read_exec(name) ⇒ Object
51 52 53 54 55 |
# File 'lib/outil/ocs/index.rb', line 51 def read_exec(name) instance_eval <<-RUBY_EVAL #{Unparser.unparse(read(name))} RUBY_EVAL end |
#update_index(name) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/outil/ocs/index.rb', line 32 def update_index name return if indexed_names.include?(name.to_s) File.open(index_path, 'a') do |file| file.write name.to_s << "\n" end end |
#write_serialize(name, ast) ⇒ Object
20 21 22 23 24 |
# File 'lib/outil/ocs/index.rb', line 20 def write_serialize name, ast File.open("#{@path}/#{name}.ast", 'w+') do |file| file.write ast.to_yaml end end |