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]


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

#classesArray<String>

Returns:

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

#idString

Returns:

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

Parameters:

  • key (String)

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



62
63
64
65
66
# File 'lib/paru/filter/attr.rb', line 62

def [](key)
  return unless @data.key? key

  @data[key]
end

#eachObject

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?

Parameters:

  • name (String)

    the class name to search for.

Returns:

  • (Boolean)

    true if this class name exist, false otherwise.



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?

Parameters:

  • name (String)

    key to find

Returns:

  • (Boolean)

    true if this key exist, false otherwise



73
74
75
# File 'lib/paru/filter/attr.rb', line 73

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



95
96
97
98
99
100
101
# File 'lib/paru/filter/attr.rb', line 95

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