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.1'.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.
-
.parse(input) ⇒ MetaHeader
Construct a new MetaHeader object or return the object untouched.
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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/metaheader.rb', line 62 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 |
.parse(input) ⇒ MetaHeader
Construct a new MetaHeader object or return the object untouched
52 53 54 55 56 57 58 |
# File 'lib/metaheader.rb', line 52 def self.parse(input) if input.is_a? self input else self.new input end end |
Instance Method Details
#[](key, default = nil) ⇒ Object?
Returns the value of a tag by its name, or nil if not found.
89 90 91 92 93 94 95 |
# File 'lib/metaheader.rb', line 89 def [](key, default = nil) if tag = @data[key] tag.value else default end end |
#[]=(key, value) ⇒ Object
Replaces the value of a tag.
100 101 102 103 104 105 |
# File 'lib/metaheader.rb', line 100 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.
128 129 130 |
# File 'lib/metaheader.rb', line 128 def delete(tag) @data.delete tag end |
#empty? ⇒ Boolean
Whether any tags were found in the input.
115 116 117 |
# File 'lib/metaheader.rb', line 115 def empty? @data.empty? end |
#has?(tag) ⇒ Boolean
Whether a tag was found in the input.
122 123 124 |
# File 'lib/metaheader.rb', line 122 def has?(tag) @data.has_key? tag.to_sym end |
#inspect ⇒ String
Makes a human-readable representation of the current instance.
140 141 142 |
# File 'lib/metaheader.rb', line 140 def inspect "#<#{self.class} #{to_h}>" end |
#size ⇒ Fixnum
Returns how many tags were found in the input.
109 110 111 |
# File 'lib/metaheader.rb', line 109 def size @data.size end |
#to_h ⇒ Hash
Make a hash from the parsed data
134 135 136 |
# File 'lib/metaheader.rb', line 134 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.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/metaheader.rb', line 157 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 |