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.



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

.supported_keysObject



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

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

.valid_metadata?(lines) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
# File 'lib/cl_wiki/file.rb', line 137

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



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

Returns:

  • (Boolean)


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

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

#to_hObject



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

def to_h
  @hash
end

#to_sObject



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