Class: Resync::Augmented

Inherits:
Object
  • Object
show all
Includes:
XML::Mapping
Defined in:
lib/resync/shared/augmented.rb

Overview

Base class for elements augmented with ResourceSync-specific child elements.

Direct Known Subclasses

BaseResourceList, Resource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(links: [], metadata: nil) ⇒ Augmented

Creates a new Augmented instance with the specified links and metadata.

Parameters:

  • links (Array<Link>) (defaults to: [])

    related links (i.e. <rs:ln>).

  • metadata (Metadata) (defaults to: nil)

    metadata about this resource.



48
49
50
51
# File 'lib/resync/shared/augmented.rb', line 48

def initialize(links: [], metadata: nil)
  self.links = links
  self. = 
end

Instance Attribute Details

Returns related links.

Returns:

  • (Array<Link>)

    related links.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/resync/shared/augmented.rb', line 12

class Augmented
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Class methods

  # ResourceSync-specific tags needing to be prefixed with +rs+ on output
  RS_TAGS = Set['ln', 'md']
  private_constant :RS_TAGS

  # Adds the +rs+ namespace prefix
  def self.prefix_rewriter(obj, xml, default_obj_to_xml)
    default_obj_to_xml.call(obj, xml)
    xml.each_element do |e|
      e.name = "rs:#{e.name}" if RS_TAGS.include?(e.name)
    end
  end
  private_class_method :prefix_rewriter

  # ------------------------------------------------------------
  # Attributes

  array_node :links, 'ln', class: Link, default_value: [], writer: method(:prefix_rewriter)
  object_node :metadata, 'md', class: , default_value: nil, writer: method(:prefix_rewriter)

  use_mapping :sitemapindex
  array_node :links, 'ln', class: Link, default_value: [], writer: method(:prefix_rewriter), sub_mapping: :_default
  object_node :metadata, 'md', class: , default_value: nil, writer: method(:prefix_rewriter), sub_mapping: :_default

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Augmented+ instance with the specified links and metadata.
  #
  # @param links [Array<Link>] related links (i.e. +<rs:ln>+).
  # @param metadata [Metadata] metadata about this resource.
  def initialize(links: [], metadata: nil)
    self.links = links
    self. = 
  end

  # ------------------------------------------------------------
  # Custom accessors

  # Sets the +links+ list. +nil+ is treated as an empty list.
  def links=(value)
    @links = value || []
  end

  # Finds links with the specified relation.
  # @param rel [String] the relation.
  # @return [Array<Link>] those links having that relation, or an empty array if none exist.
  def links_for(rel:)
    links.select { |l| l.rel == rel }
  end

  # Shortcut to find the first link with the specified relation (in ResourceSync there often
  # should be only one link with a particular relation)
  # @param rel [String] the relation.
  # @return [Link] the first link having that relation, or nil if none exists.
  def link_for(rel:)
    links.find { |l| l.rel == rel }
  end
end

#metadataMetadata

Returns metadata about this object.

Returns:

  • (Metadata)

    metadata about this object.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/resync/shared/augmented.rb', line 12

class Augmented
  include ::XML::Mapping

  # ------------------------------------------------------------
  # Class methods

  # ResourceSync-specific tags needing to be prefixed with +rs+ on output
  RS_TAGS = Set['ln', 'md']
  private_constant :RS_TAGS

  # Adds the +rs+ namespace prefix
  def self.prefix_rewriter(obj, xml, default_obj_to_xml)
    default_obj_to_xml.call(obj, xml)
    xml.each_element do |e|
      e.name = "rs:#{e.name}" if RS_TAGS.include?(e.name)
    end
  end
  private_class_method :prefix_rewriter

  # ------------------------------------------------------------
  # Attributes

  array_node :links, 'ln', class: Link, default_value: [], writer: method(:prefix_rewriter)
  object_node :metadata, 'md', class: , default_value: nil, writer: method(:prefix_rewriter)

  use_mapping :sitemapindex
  array_node :links, 'ln', class: Link, default_value: [], writer: method(:prefix_rewriter), sub_mapping: :_default
  object_node :metadata, 'md', class: , default_value: nil, writer: method(:prefix_rewriter), sub_mapping: :_default

  # ------------------------------------------------------------
  # Initializer

  # Creates a new +Augmented+ instance with the specified links and metadata.
  #
  # @param links [Array<Link>] related links (i.e. +<rs:ln>+).
  # @param metadata [Metadata] metadata about this resource.
  def initialize(links: [], metadata: nil)
    self.links = links
    self. = 
  end

  # ------------------------------------------------------------
  # Custom accessors

  # Sets the +links+ list. +nil+ is treated as an empty list.
  def links=(value)
    @links = value || []
  end

  # Finds links with the specified relation.
  # @param rel [String] the relation.
  # @return [Array<Link>] those links having that relation, or an empty array if none exist.
  def links_for(rel:)
    links.select { |l| l.rel == rel }
  end

  # Shortcut to find the first link with the specified relation (in ResourceSync there often
  # should be only one link with a particular relation)
  # @param rel [String] the relation.
  # @return [Link] the first link having that relation, or nil if none exists.
  def link_for(rel:)
    links.find { |l| l.rel == rel }
  end
end

Instance Method Details

Shortcut to find the first link with the specified relation (in ResourceSync there often should be only one link with a particular relation)

Parameters:

  • rel (String)

    the relation.

Returns:

  • (Link)

    the first link having that relation, or nil if none exists.



72
73
74
# File 'lib/resync/shared/augmented.rb', line 72

def link_for(rel:)
  links.find { |l| l.rel == rel }
end

Finds links with the specified relation.

Parameters:

  • rel (String)

    the relation.

Returns:

  • (Array<Link>)

    those links having that relation, or an empty array if none exist.



64
65
66
# File 'lib/resync/shared/augmented.rb', line 64

def links_for(rel:)
  links.select { |l| l.rel == rel }
end