Class: ClWiki::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/cl_wiki/metadata.rb

Class Method Summary collapse

Instance Method Summary collapse

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/)
    valid_metadata?() ? content = content[(idx + 3)..-1] :  = []
  end
  [self.new(), content]
end

.supported_keysObject



19
20
21
# File 'lib/cl_wiki/metadata.rb', line 19

def self.supported_keys
  %w[mtime encrypted owner]
end

.valid_metadata?(lines) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/cl_wiki/metadata.rb', line 14

def self.valid_metadata?(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

Returns:

  • (Boolean)


39
40
41
# File 'lib/cl_wiki/metadata.rb', line 39

def has?(key)
  @hash.key?(key)
end

#to_hObject



47
48
49
# File 'lib/cl_wiki/metadata.rb', line 47

def to_h
  @hash
end

#to_sObject



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