Class: MetaHeader

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ MetaHeader

Parse every tags found in input up to the first newline.

Parameters:

  • input (String)


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

#strictBoolean

Whether to fail validation if unknown tags are encoutered.

Returns:

  • (Boolean)

See Also:



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.

Parameters:

  • path (String)

    path to the file to be read

Returns:



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.

Parameters:

  • key (Symbol)

    tag name

  • default (Object) (defaults to: nil)

    value to return if key doesn’t exist

Returns:

  • (Object, nil)


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.

Parameters:

  • value

    the new value

Returns:

  • value

Raises:

  • (ArgumentError)


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.

Parameters:

  • tag (Symbol)

    the tag to remove



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.

Returns:

  • (Boolean)


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.

Parameters:

  • tag (Symbol)

    the tag to lookup

Returns:

  • (Boolean)


111
112
113
# File 'lib/metaheader.rb', line 111

def has?(tag)
  @data.has_key? tag.to_sym
end

#inspectString

Makes a human-readable representation of the current instance.

Returns:

  • (String)


129
130
131
# File 'lib/metaheader.rb', line 129

def inspect
  "#<#{self.class} #{to_h}>"
end

#sizeFixnum

Returns how many tags were found in the input.

Returns:

  • (Fixnum)


98
99
100
# File 'lib/metaheader.rb', line 98

def size
  @data.size
end

#to_hHash

Make a hash from the parsed data

Returns:

  • (Hash)


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.

Examples:

mh = MetaHeader.new "@hello world\n@chunky bacon"
mh.validate \
  hello: [MetaHeader::REQUIRED, MetaHeader::SINGLELINE, /\d/],
  chunky: proc {|value| 'not bacon' unless value == 'bacon' }

Parameters:

  • rules (Hash)

    tag_name => rule or array_of_rules

Returns:

  • (Array, nil)

    error list or nil

See Also:



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