Class: FormatParser::ISOBaseMediaFileFormat::Box

Inherits:
Struct
  • Object
show all
Defined in:
lib/parsers/iso_base_media_file_format/box.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Box

Returns a new instance of Box.



4
5
6
7
8
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 4

def initialize(*args)
  super
  self.fields ||= {}
  self.children ||= []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



3
4
5
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3

def children
  @children
end

#fieldsObject

Returns the value of attribute fields

Returns:

  • (Object)

    the current value of fields



3
4
5
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3

def fields
  @fields
end

#positionObject

Returns the value of attribute position

Returns:

  • (Object)

    the current value of position



3
4
5
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3

def position
  @position
end

#sizeObject

Returns the value of attribute size

Returns:

  • (Object)

    the current value of size



3
4
5
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3

def size
  @size
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



3
4
5
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3

def type
  @type
end

Instance Method Details

#all_children(*types) ⇒ Array<Box>

Return all children with one of the given type(s).

Parameters:

  • types (Array<String>)

    The box type(s) to search for.

Returns:



14
15
16
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 14

def all_children(*types)
  children.select { |child| types.include?(child.type) }
end

#all_descendents(*types) ⇒ Array<Box>

Find and return all descendents of a given type.

Parameters:

  • types (Array<String>)

    The box type(s) to search for.

Returns:



38
39
40
41
42
43
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 38

def all_descendents(*types)
  children.flat_map do |child|
    descendents = child.all_descendents(*types)
    types.include?(child.type) ? [child] + descendents : descendents
  end
end

#all_descendents_by_path(path) ⇒ Array<Box>

Find and return all descendents that exist at the given path.

Parameters:

  • path (Array<String>)

    The path to search at.

Returns:



49
50
51
52
53
54
55
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 49

def all_descendents_by_path(path)
  return [] if path.empty?
  next_type, *remaining_path = path
  matching_children = all_children(next_type)
  return matching_children if remaining_path.empty?
  matching_children.flat_map { |child| child.all_descendents_by_path(remaining_path) }
end

#child?(type) ⇒ Boolean

Returns true if there are one or more children with the given type.

Parameters:

  • type (String)

    The box type to search for.

Returns:

  • (Boolean)


22
23
24
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 22

def child?(type)
  children.any? { |child| child.type == type }
end

#first_child(*types) ⇒ Box?

Return the first child with one of the given types.

Parameters:

  • types (Array<String>)

    The box type(s) to search for.

Returns:



30
31
32
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 30

def first_child(*types)
  children.find { |child| types.include?(child.type) }
end

#first_descendent(*types) ⇒ Box?

Find and return the first descendent (using depth-first search) of a given type.

Parameters:

  • types (Array<String>)

    The box type(s) to search for.

Returns:



61
62
63
64
65
66
67
68
69
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 61

def first_descendent(*types)
  children.each do |child|
    return child if types.include?(child.type)
    if (descendent = child.first_descendent(*types))
      return descendent
    end
  end
  nil
end

#first_descendent_by_path(path) ⇒ Box?

Find and return the first descendent that exists at the given path.

Parameters:

  • path (Array<String>)

    The path to search at.

Returns:



75
76
77
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 75

def first_descendent_by_path(path)
  all_descendents_by_path(path)[0]
end