Class: ClWiki::Metadata
- Inherits:
-
Object
- Object
- ClWiki::Metadata
- Defined in:
- lib/cl_wiki/file.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.
146 147 148 149 150 |
# File 'lib/cl_wiki/file.rb', line 146 def initialize(lines = []) @hash = {} @keys = Metadata.supported_keys parse_lines(lines) end |
Class Method Details
.split_file_contents(content) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cl_wiki/file.rb', line 126 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
142 143 144 |
# File 'lib/cl_wiki/file.rb', line 142 def self.supported_keys %w[mtime encrypted owner] end |
.valid_metadata?(lines) ⇒ Boolean
137 138 139 140 |
# File 'lib/cl_wiki/file.rb', line 137 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
152 153 154 |
# File 'lib/cl_wiki/file.rb', line 152 def [](key) @hash[key] end |
#[]=(key, value) ⇒ Object
156 157 158 159 160 |
# File 'lib/cl_wiki/file.rb', line 156 def []=(key, value) raise "Unexpected key: #{key}" unless @keys.include?(key) @hash[key] = value end |
#has?(key) ⇒ Boolean
162 163 164 |
# File 'lib/cl_wiki/file.rb', line 162 def has?(key) @hash.key?(key) end |
#to_h ⇒ Object
170 171 172 |
# File 'lib/cl_wiki/file.rb', line 170 def to_h @hash end |
#to_s ⇒ Object
166 167 168 |
# File 'lib/cl_wiki/file.rb', line 166 def to_s @hash.collect { |k, v| "#{k}: #{v}" }.join("\n") + "\n\n\n" end |