Class: ClWiki::Metadata

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = []) ⇒ Metadata

Returns a new instance of Metadata.



150
151
152
153
154
# File 'lib/cl_wiki/file.rb', line 150

def initialize(lines = [])
  @hash = {}
  @keys = Metadata.supported_keys
  parse_lines(lines)
end

Class Method Details

.split_file_contents(content) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/cl_wiki/file.rb', line 130

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



146
147
148
# File 'lib/cl_wiki/file.rb', line 146

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

.valid_metadata?(lines) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
144
# File 'lib/cl_wiki/file.rb', line 141

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



156
157
158
# File 'lib/cl_wiki/file.rb', line 156

def [](key)
  @hash[key]
end

#[]=(key, value) ⇒ Object



160
161
162
163
164
# File 'lib/cl_wiki/file.rb', line 160

def []=(key, value)
  raise "Unexpected key: #{key}" unless @keys.include?(key)

  @hash[key] = value
end

#has?(key) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/cl_wiki/file.rb', line 166

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

#to_hObject



174
175
176
# File 'lib/cl_wiki/file.rb', line 174

def to_h
  @hash
end

#to_sObject



170
171
172
# File 'lib/cl_wiki/file.rb', line 170

def to_s
  @hash.collect { |k, v| "#{k}: #{v}" }.join("\n") + "\n\n\n"
end