Class: Paru::PandocFilter::Attr
- Inherits:
-
Object
- Object
- Paru::PandocFilter::Attr
- Includes:
- Enumerable
- Defined in:
- lib/paru/filter/attr.rb
Overview
Attr represents an attribute object for a node. It contains of an id, a list of class names and a list of key-value pairs.
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get the value for key in this attributes object.
-
#each ⇒ Object
For each key-value pair of this attributes object.
-
#has_class?(name) ⇒ Boolean
(also: #class?)
Does this attributes object have a class?.
-
#has_key?(name) ⇒ Boolean
(also: #key?)
Does this attributes object have this key?.
-
#initialize(attributes = []) ⇒ Attr
constructor
Create a new attributes object.
-
#to_ast ⇒ Array
Convert this attributes object to an AST representation.
Constructor Details
#initialize(attributes = []) ⇒ Attr
Create a new attributes object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/paru/filter/attr.rb', line 42 def initialize(attributes = []) id, classes, data = attributes @id = id || '' @classes = classes || [] @classes = [@classes] unless @classes.is_a? Array @data = data.to_h end |
Instance Attribute Details
#classes ⇒ Array<String>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/paru/filter/attr.rb', line 33 class Attr include Enumerable attr_accessor :id, :classes # Create a new attributes object # # @param attributes [Array = []] the attributes as [id, [class names], # [key-value pairs]] def initialize(attributes = []) id, classes, data = attributes @id = id || '' @classes = classes || [] @classes = [@classes] unless @classes.is_a? Array @data = data.to_h end # For each key-value pair of this attributes object def each @data.each end # Get the value for key in this attributes object # # @param key [String] the key to get the value for. Nil if it does # not exists def [](key) return unless @data.key? key @data[key] end # Does this attributes object have this key? # # @param name [String] key to find # # @return [Boolean] true if this key exist, false otherwise def has_key?(name) @data.key? name end alias key? has_key? # Does this attributes object have a class? # # @param name [String] the class name to search for. # # @return [Boolean] true if this class name exist, false # otherwise. def has_class?(name) @classes.include? name end alias class? has_class? # Convert this attributes object to an AST representation # # @return [Array] Array containing id, class name list, and # key-value pair list def to_ast [ @id, @classes, @data.to_a ] end end |
#id ⇒ String
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/paru/filter/attr.rb', line 33 class Attr include Enumerable attr_accessor :id, :classes # Create a new attributes object # # @param attributes [Array = []] the attributes as [id, [class names], # [key-value pairs]] def initialize(attributes = []) id, classes, data = attributes @id = id || '' @classes = classes || [] @classes = [@classes] unless @classes.is_a? Array @data = data.to_h end # For each key-value pair of this attributes object def each @data.each end # Get the value for key in this attributes object # # @param key [String] the key to get the value for. Nil if it does # not exists def [](key) return unless @data.key? key @data[key] end # Does this attributes object have this key? # # @param name [String] key to find # # @return [Boolean] true if this key exist, false otherwise def has_key?(name) @data.key? name end alias key? has_key? # Does this attributes object have a class? # # @param name [String] the class name to search for. # # @return [Boolean] true if this class name exist, false # otherwise. def has_class?(name) @classes.include? name end alias class? has_class? # Convert this attributes object to an AST representation # # @return [Array] Array containing id, class name list, and # key-value pair list def to_ast [ @id, @classes, @data.to_a ] end end |
Instance Method Details
#[](key) ⇒ Object
Get the value for key in this attributes object
not exists
62 63 64 65 66 |
# File 'lib/paru/filter/attr.rb', line 62 def [](key) return unless @data.key? key @data[key] end |
#each ⇒ Object
For each key-value pair of this attributes object
54 55 56 |
# File 'lib/paru/filter/attr.rb', line 54 def each @data.each end |
#has_class?(name) ⇒ Boolean Also known as: class?
Does this attributes object have a class?
85 86 87 |
# File 'lib/paru/filter/attr.rb', line 85 def has_class?(name) @classes.include? name end |
#has_key?(name) ⇒ Boolean Also known as: key?
Does this attributes object have this key?
73 74 75 |
# File 'lib/paru/filter/attr.rb', line 73 def has_key?(name) @data.key? name end |
#to_ast ⇒ Array
Convert this attributes object to an AST representation
95 96 97 98 99 100 101 |
# File 'lib/paru/filter/attr.rb', line 95 def to_ast [ @id, @classes, @data.to_a ] end |