Class: ClWiki::Metadata
- Inherits:
-
Object
- Object
- ClWiki::Metadata
- Defined in:
- lib/cl_wiki/metadata.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #has?(key) ⇒ Boolean
-
#initialize(lines = []) ⇒ Metadata
constructor
A new instance of Metadata.
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(lines = []) ⇒ Metadata
Returns a new instance of Metadata.
23 24 25 26 27 |
# File 'lib/cl_wiki/metadata.rb', line 23 def initialize(lines = []) @hash = {} @keys = Metadata.supported_keys parse_lines(lines) end |
Class Method Details
.split_file_contents(content) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/cl_wiki/metadata.rb', line 3 def self.split_file_contents(content) idx = content =~ /\n{3}/m = [] if idx = content[0..(idx - 1)].split(/\n/) () ? content = content[(idx + 3)..-1] : = [] end [self.new(), content] end |
.supported_keys ⇒ Object
19 20 21 |
# File 'lib/cl_wiki/metadata.rb', line 19 def self.supported_keys %w[mtime encrypted owner] end |
.valid_metadata?(lines) ⇒ Boolean
14 15 16 17 |
# File 'lib/cl_wiki/metadata.rb', line 14 def self.(lines) lines.map { |ln| ln.scan(/\A(\w+):?/) }.flatten. map { |k| supported_keys.include?(k) }.uniq == [true] end |
Instance Method Details
#[](key) ⇒ Object
29 30 31 |
# File 'lib/cl_wiki/metadata.rb', line 29 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
33 34 35 36 37 |
# File 'lib/cl_wiki/metadata.rb', line 33 def []=(key, value) raise "Unexpected key: #{key}" unless @keys.include?(key) @hash[key] = value end |
#has?(key) ⇒ Boolean
39 40 41 |
# File 'lib/cl_wiki/metadata.rb', line 39 def has?(key) @hash.key?(key) end |
#to_h ⇒ Object
47 48 49 |
# File 'lib/cl_wiki/metadata.rb', line 47 def to_h @hash end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/cl_wiki/metadata.rb', line 43 def to_s @hash.collect { |k, v| "#{k}: #{v}" }.join("\n") + "\n\n\n" end |