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.3.1'.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, IO)

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/metaheader.rb', line 62

def initialize(input)
  @strict = false
  @data = {}

  @last_tag = nil
  @empty_lines = 0

  unless input.is_a? IO
    input = StringIO.new input.encode universal_newline: true
  end

  input.each_line {|line| break unless parse line }

  Parser.each {|klass|
    input.rewind

    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)
  File.open(path) {|file| self.new file }
end

.parse(input) ⇒ MetaHeader

Construct a new MetaHeader object or return the object untouched

Parameters:

Returns:


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.

Parameters:

  • key (Symbol)

    tag name

  • default (Object) (defaults to: nil)

    value to return if key doesn't exist

Returns:

  • (Object, nil)

88
89
90
91
92
93
94
# File 'lib/metaheader.rb', line 88

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)

99
100
101
102
103
104
# File 'lib/metaheader.rb', line 99

def []=(key, value)
  raise ArgumentError, 'value cannot be nil' if value.nil?

  @data[key] ||= Tag.new key
  @data[key].value = value
end

#alias(*args) ⇒ Object

Rename one or more tags.

Examples:

mh.alias :old, :new
mh.alias :old1, :old2, :new
mh.alias [:old1, :old2], :new
mh.alias old1: :new1, old2: :new2

Parameters:

  • old (Symbol, Hash)
  • new (Symbol)

Raises:

  • (ArgumentError)

182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/metaheader.rb', line 182

def alias(*args)
  raise ArgumentError, 'wrong number of arguments' unless args.size.between? 1, 2

  tags, new = args

  if args.size == 1
    tags.each {|k, v| self.alias k, v }
  else
    Array(tags).each {|old|
      @data[new] = delete old if has? old
    }
  end
end

#delete(tag) ⇒ Object

Removes a given tag from the list.

Parameters:

  • tag (Symbol)

    the tag to remove


127
128
129
# File 'lib/metaheader.rb', line 127

def delete(tag)
  @data.delete tag
end

#empty?Boolean

Whether any tags were found in the input.

Returns:

  • (Boolean)

114
115
116
# File 'lib/metaheader.rb', line 114

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)

121
122
123
# File 'lib/metaheader.rb', line 121

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

#inspectString

Makes a human-readable representation of the current instance.

Returns:

  • (String)

139
140
141
# File 'lib/metaheader.rb', line 139

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

#sizeInteger

Returns how many tags were found in the input.

Returns:

  • (Integer)

108
109
110
# File 'lib/metaheader.rb', line 108

def size
  @data.size
end

#to_hHash

Make a hash from the parsed data

Returns:

  • (Hash)

133
134
135
# File 'lib/metaheader.rb', line 133

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\[email protected] 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:


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/metaheader.rb', line 156

def validate(rules)
  errors = Array.new

  if @strict
    @data.each {|key, tag|
      errors << "unknown tag '%s'" % tag.name 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