Class: FormatParser::ISOBaseMediaFileFormat::Box
- Inherits:
-
Struct
- Object
- Struct
- FormatParser::ISOBaseMediaFileFormat::Box
- Defined in:
- lib/parsers/iso_base_media_file_format/box.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#position ⇒ Object
Returns the value of attribute position.
-
#size ⇒ Object
Returns the value of attribute size.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
-
#all_children(*types) ⇒ Array<Box>
Return all children with one of the given type(s).
-
#all_descendents(*types) ⇒ Array<Box>
Find and return all descendents of a given type.
-
#all_descendents_by_path(path) ⇒ Array<Box>
Find and return all descendents that exist at the given path.
-
#child?(type) ⇒ Boolean
Returns true if there are one or more children with the given type.
-
#first_child(*types) ⇒ Box?
Return the first child with one of the given types.
-
#first_descendent(*types) ⇒ Box?
Find and return the first descendent (using depth-first search) of a given type.
-
#first_descendent_by_path(path) ⇒ Box?
Find and return the first descendent that exists at the given path.
-
#initialize(*args) ⇒ Box
constructor
A new instance of Box.
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
#children ⇒ Object
Returns the value of attribute children
3 4 5 |
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3 def children @children end |
#fields ⇒ Object
Returns the value of attribute fields
3 4 5 |
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3 def fields @fields end |
#position ⇒ Object
Returns the value of attribute position
3 4 5 |
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3 def position @position end |
#size ⇒ Object
Returns the value of attribute size
3 4 5 |
# File 'lib/parsers/iso_base_media_file_format/box.rb', line 3 def size @size end |
#type ⇒ Object
Returns the value of attribute 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).
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.
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.
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.
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.
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.
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.
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 |