Class: YKK
- Inherits:
-
Object
- Object
- YKK
- Defined in:
- lib/ykk.rb
Instance Attribute Summary collapse
-
#dir ⇒ Object
Returns the value of attribute dir.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #file_of(key) ⇒ Object
-
#initialize(dir = nil) ⇒ YKK
constructor
A new instance of YKK.
- #inspect ⇒ Object
- #key?(key) ⇒ Boolean
- #key_gen(value) ⇒ Object
Constructor Details
#initialize(dir = nil) ⇒ YKK
Returns a new instance of YKK.
22 23 24 |
# File 'lib/ykk.rb', line 22 def initialize(dir = nil) self.dir = dir end |
Instance Attribute Details
#dir ⇒ Object
Returns the value of attribute dir.
20 21 22 |
# File 'lib/ykk.rb', line 20 def dir @dir end |
Class Method Details
.inspect ⇒ Object
16 17 18 |
# File 'lib/ykk.rb', line 16 def self.inspect @instance.inspect end |
.method_missing(method, *args, &block) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/ykk.rb', line 8 def self.method_missing(method, *args, &block) if @instance.respond_to?(method) @instance.__send__(method, *args, &block) else super end end |
Instance Method Details
#<<(value) ⇒ Object
26 27 28 29 30 |
# File 'lib/ykk.rb', line 26 def <<(value) key = key_gen(value) self[key] = value key end |
#[](key) ⇒ Object
32 33 34 35 36 |
# File 'lib/ykk.rb', line 32 def [](key) path = file_of(key) return nil unless File.exists?(path) YAML.load(File.read(path)) end |
#[]=(key, value) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ykk.rb', line 42 def []=(key, value) path = file_of(key) dirname = File.dirname(path) FileUtils.mkdir_p(dirname) unless File.exists?(dirname) File.open(path, 'wb') { |f| f << value.to_yaml } end |
#delete(key) ⇒ Object
49 50 51 52 53 |
# File 'lib/ykk.rb', line 49 def delete(key) path = file_of(key) File.delete(path) if File.exists?(path) nil end |
#file_of(key) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/ykk.rb', line 55 def file_of(key) key = key.to_s raise ArgumentError, 'invalid key' unless key =~ /^[\w\/]+$/ raise "dir is not specified" unless dir File.join(dir, key) end |
#inspect ⇒ Object
66 67 68 69 70 71 |
# File 'lib/ykk.rb', line 66 def inspect pairs = Dir.glob(dir + '/*').map {|f| "#{File.basename(f).inspect}: #{YAML.load_file(f).inspect}" } "YKK(#{pairs.join ', '})" end |
#key?(key) ⇒ Boolean
38 39 40 |
# File 'lib/ykk.rb', line 38 def key?(key) !!self[key] end |
#key_gen(value) ⇒ Object
62 63 64 |
# File 'lib/ykk.rb', line 62 def key_gen(value) Digest::SHA1.hexdigest(value.to_yaml) end |