Class: MetaHeader
- Inherits:
-
Object
- Object
- MetaHeader
- Defined in:
- lib/metaheader.rb,
lib/metaheader/version.rb
Defined Under Namespace
Classes: Parser
Constant Summary collapse
- BOOLEAN =
Object.new.freeze
- OPTIONAL =
Object.new.freeze
- REQUIRED =
Object.new.freeze
- SINGLELINE =
Object.new.freeze
- VALUE =
Object.new.freeze
- VERSION =
MetaHeader’s version
'1.2'.freeze
Instance Attribute Summary collapse
-
#strict ⇒ Boolean
Whether to fail validation if unknown tags are encoutered.
Class Method Summary collapse
-
.from_file(path) ⇒ MetaHeader
Create a new instance from the contents of a file.
Instance Method Summary collapse
-
#[](key, default = nil) ⇒ Object?
Returns the value of a tag by its name, or nil if not found.
-
#[]=(key, value) ⇒ Object
Replaces the value of a tag.
-
#delete(tag) ⇒ Object
Removes a given tag from the list.
-
#empty? ⇒ Boolean
Whether any tags were found in the input.
-
#has?(tag) ⇒ Boolean
Whether a tag was found in the input.
-
#initialize(input) ⇒ MetaHeader
constructor
Parse every tags found in input up to the first newline.
-
#inspect ⇒ String
Makes a human-readable representation of the current instance.
-
#size ⇒ Fixnum
Returns how many tags were found in the input.
-
#to_h ⇒ Hash
Make a hash from the parsed data.
-
#validate(rules) ⇒ Array?
Validates parsed data according to a custom set of rules.
Constructor Details
#initialize(input) ⇒ MetaHeader
Parse every tags found in input up to the first newline.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/metaheader.rb', line 51 def initialize(input) @strict = false @data = {} @last_key = nil @last_prefix = String.new input = input.encode universal_newline: true input.each_line {|line| if line.strip.empty? break else parse line end } Parser.each {|klass| parser = klass.new parser.instance_variable_set :@mh, self parser.parse input } end |
Instance Attribute Details
#strict ⇒ Boolean
Whether to fail validation if unknown tags are encoutered.
40 41 42 |
# File 'lib/metaheader.rb', line 40 def strict @strict end |
Class Method Details
.from_file(path) ⇒ MetaHeader
Create a new instance from the contents of a file.
45 46 47 |
# File 'lib/metaheader.rb', line 45 def self.from_file(path) self.new File.read(path) end |
Instance Method Details
#[](key, default = nil) ⇒ Object?
Returns the value of a tag by its name, or nil if not found.
78 79 80 81 82 83 84 |
# File 'lib/metaheader.rb', line 78 def [](key, default = nil) if tag = @data[key] tag.value else default end end |
#[]=(key, value) ⇒ Object
Replaces the value of a tag.
89 90 91 92 93 94 |
# File 'lib/metaheader.rb', line 89 def []=(key, value) raise ArgumentError, 'value cannot be nil' if value.nil? @data[key] ||= Tag.new key @data[key].value = value end |
#delete(tag) ⇒ Object
Removes a given tag from the list.
117 118 119 |
# File 'lib/metaheader.rb', line 117 def delete(tag) @data.delete tag end |
#empty? ⇒ Boolean
Whether any tags were found in the input.
104 105 106 |
# File 'lib/metaheader.rb', line 104 def empty? @data.empty? end |
#has?(tag) ⇒ Boolean
Whether a tag was found in the input.
111 112 113 |
# File 'lib/metaheader.rb', line 111 def has?(tag) @data.has_key? tag.to_sym end |
#inspect ⇒ String
Makes a human-readable representation of the current instance.
129 130 131 |
# File 'lib/metaheader.rb', line 129 def inspect "#<#{self.class} #{to_h}>" end |
#size ⇒ Fixnum
Returns how many tags were found in the input.
98 99 100 |
# File 'lib/metaheader.rb', line 98 def size @data.size end |
#to_h ⇒ Hash
Make a hash from the parsed data
123 124 125 |
# File 'lib/metaheader.rb', line 123 def to_h Hash[@data.map {|name, tag| [name, tag.value] }] end |
#validate(rules) ⇒ Array?
Validates parsed data according to a custom set of rules.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/metaheader.rb', line 146 def validate(rules) errors = Array.new if @strict @data.each_key {|key| errors << "unknown tag '%s'" % key unless rules.has_key? key } end rules.each_pair {|key, rule| if key_error = validate_key(key, rule) errors << key_error end } errors unless errors.empty? end |