Class: FSDB::Format
- Inherits:
-
Object
- Object
- FSDB::Format
- Defined in:
- lib/fsdb/formats.rb
Overview
Formats are handled as instances rather than as subclasses so that they can be dynamically generated, serialized, etc. When defining a dump proc for a new format, be sure to perform the write with a single syswrite call to protect data consistency in case of exceptions.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Instance Method Summary collapse
- #===(path) ⇒ Object
-
#binary? ⇒ Boolean
Used only on Windows.
- #dump(object, f, *opts) ⇒ Object
-
#initialize(*args) ⇒ Format
constructor
A new instance of Format.
- #load(f) ⇒ Object
-
#when(*patterns) ⇒ Object
A convenient way to use a format for a different pattern.
Constructor Details
#initialize(*args) ⇒ Format
Returns a new instance of Format.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fsdb/formats.rb', line 9 def initialize(*args) @options = []; @patterns = []; @binary = false while arg = args.shift case arg when Symbol @options << arg eval "@#{arg} = true" ## yech! use instance_variable_set in 1.8 when Hash @name ||= arg[:name] @load ||= arg[:load] @dump ||= arg[:dump] else @patterns << arg end end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/fsdb/formats.rb', line 8 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/fsdb/formats.rb', line 8 def @options end |
#patterns ⇒ Object
Returns the value of attribute patterns.
8 9 10 |
# File 'lib/fsdb/formats.rb', line 8 def patterns @patterns end |
Instance Method Details
#===(path) ⇒ Object
37 38 39 |
# File 'lib/fsdb/formats.rb', line 37 def ===(path) @patterns.any? {|pat| pat === path} end |
#binary? ⇒ Boolean
Used only on Windows.
42 43 44 |
# File 'lib/fsdb/formats.rb', line 42 def binary? @binary end |
#dump(object, f, *opts) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/fsdb/formats.rb', line 57 def dump(object, f, *opts) if @dump f.rewind; f.truncate(0) @dump[object, f] f.flush end rescue StandardError => e raise e, "Format #{name} can't dump object at path #{f.path}: #{e.inspect}" + "\n\nObject is:\n#{object.inspect[0..1000]}\n\n" end |
#load(f) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fsdb/formats.rb', line 46 def load(f) @load[f] if @load rescue Errno::EISDIR Formats::DIR_LOAD[f] ## any reason not to do this? rescue Errno::ENOTDIR raise Database::NotDirError rescue StandardError => e raise e, "Format #{name} can't load object at path #{f.path}: #{e.inspect}" end |
#when(*patterns) ⇒ Object
A convenient way to use a format for a different pattern. Duplicates the format and sets the patterns to the specified patterns.
31 32 33 34 35 |
# File 'lib/fsdb/formats.rb', line 31 def when(*patterns) fmt = dup fmt.patterns = patterns fmt end |