Class: Paru::PandocFilter::Attr

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(attributes = []) ⇒ Attr

Create a new attributes object

Parameters:

  • attributes (Array = []) (defaults to: [])

    the attributes as [id, [class names],

    key-value pairs]


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

#classesArray<String>

Returns:

  • (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

#idString

Returns:

  • (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

Parameters:

  • key (String)

    the key to get the value for. Nil if it does



60
61
62
63
64
# File 'lib/paru/filter/attr.rb', line 60

def [](key) 
    if @data.key? key
        @data[key]
    end 
end

#eachObject

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?

Parameters:

  • name (String)

    the class name to search for.

Returns:

  • (Boolean)

    true if this class name exist, false otherwise.



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?

Parameters:

  • name (String)

    key to find

Returns:

  • (Boolean)

    true if this key exist, false otherwise



71
72
73
# File 'lib/paru/filter/attr.rb', line 71

def has_key?(name)
    @data.key? name
end

#to_astArray

Convert this attributes object to an AST representation

Returns:

  • (Array)

    Array containing id, class name list, and key-value pair list



89
90
91
92
93
94
95
# File 'lib/paru/filter/attr.rb', line 89

def to_ast
    [
        @id,
        @classes,
        @data.to_a
    ]
end