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
Does this attributes object have a class?.
-
#has_key?(name) ⇒ Boolean
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
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/paru/filter/attr.rb', line 40 def initialize(attributes = []) id, classes, data = attributes @id = id || "" @classes = classes || [] @classes = [@classes] unless @classes.is_a? Array @data = Hash[data] || {} end |
Instance Attribute Details
#classes ⇒ Array<String>
31 32 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 |
# File 'lib/paru/filter/attr.rb', line 31 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 = Hash[data] || {} 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) if @data.key? key @data[key] end 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 # 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 # 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
31 32 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 |
# File 'lib/paru/filter/attr.rb', line 31 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 = Hash[data] || {} 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) if @data.key? key @data[key] end 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 # 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 # 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
60 61 62 63 64 |
# File 'lib/paru/filter/attr.rb', line 60 def [](key) if @data.key? key @data[key] end end |
#each ⇒ Object
For each key-value pair of this attributes object
52 53 54 |
# File 'lib/paru/filter/attr.rb', line 52 def each @data.each end |
#has_class?(name) ⇒ Boolean
Does this attributes object have a class?
81 82 83 |
# File 'lib/paru/filter/attr.rb', line 81 def has_class?(name) @classes.include? name end |
#has_key?(name) ⇒ Boolean
Does this attributes object have this key?
71 72 73 |
# File 'lib/paru/filter/attr.rb', line 71 def has_key?(name) @data.key? name end |
#to_ast ⇒ Array
Convert this attributes object to an AST representation
89 90 91 92 93 94 95 |
# File 'lib/paru/filter/attr.rb', line 89 def to_ast [ @id, @classes, @data.to_a ] end |