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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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: Metadata, 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: Metadata, 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

  # Convenience accessor for the +at_time+ metadata attribute
  # @return [Time] the datetime at which assembling a resource list
  #   began (including resource list indices, resource dumps, etc.)
  # @see Metadata#at_time
  def at_time
    .at_time if 
  end

  # Convenience accessor for the +from_time+ metadata attribute
  # @return [Time] the beginning of the time range represented by
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#from_time
  def from_time
    .from_time if 
  end

  # Convenience accessor for the +until_time+ metadata attribute
  # @return [Time] the end of the time range represented by
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#until_time
  def until_time
    .until_time if 
  end

  # Convenience accessor for the +completed_time+ metadata attribute
  # @return [Time] the datetime at which assembling a resource list
  #   ended (including resource list indices, resource dumps, etc.)
  # @see Metadata#completed_time
  def completed_time
    .completed_time if 
  end

  # Convenience accessor for the +change+ metadata attribute
  # @return [Change] the type of change to a resource reported in
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#change
  def change
    .change if 
  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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
# 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: Metadata, 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: Metadata, 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

  # Convenience accessor for the +at_time+ metadata attribute
  # @return [Time] the datetime at which assembling a resource list
  #   began (including resource list indices, resource dumps, etc.)
  # @see Metadata#at_time
  def at_time
    .at_time if 
  end

  # Convenience accessor for the +from_time+ metadata attribute
  # @return [Time] the beginning of the time range represented by
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#from_time
  def from_time
    .from_time if 
  end

  # Convenience accessor for the +until_time+ metadata attribute
  # @return [Time] the end of the time range represented by
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#until_time
  def until_time
    .until_time if 
  end

  # Convenience accessor for the +completed_time+ metadata attribute
  # @return [Time] the datetime at which assembling a resource list
  #   ended (including resource list indices, resource dumps, etc.)
  # @see Metadata#completed_time
  def completed_time
    .completed_time if 
  end

  # Convenience accessor for the +change+ metadata attribute
  # @return [Change] the type of change to a resource reported in
  #   a change list (including change list indices, change dumps, etc.)
  # @see Metadata#change
  def change
    .change if 
  end
end

Instance Method Details

#at_timeTime

Convenience accessor for the at_time metadata attribute

Returns:

  • (Time)

    the datetime at which assembling a resource list began (including resource list indices, resource dumps, etc.)

See Also:



80
81
82
# File 'lib/resync/shared/augmented.rb', line 80

def at_time
  .at_time if 
end

#changeChange

Convenience accessor for the change metadata attribute

Returns:

  • (Change)

    the type of change to a resource reported in a change list (including change list indices, change dumps, etc.)

See Also:



112
113
114
# File 'lib/resync/shared/augmented.rb', line 112

def change
  .change if 
end

#completed_timeTime

Convenience accessor for the completed_time metadata attribute

Returns:

  • (Time)

    the datetime at which assembling a resource list ended (including resource list indices, resource dumps, etc.)

See Also:



104
105
106
# File 'lib/resync/shared/augmented.rb', line 104

def completed_time
  .completed_time if 
end

#from_timeTime

Convenience accessor for the from_time metadata attribute

Returns:

  • (Time)

    the beginning of the time range represented by a change list (including change list indices, change dumps, etc.)

See Also:



88
89
90
# File 'lib/resync/shared/augmented.rb', line 88

def from_time
  .from_time if 
end

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

#until_timeTime

Convenience accessor for the until_time metadata attribute

Returns:

  • (Time)

    the end of the time range represented by a change list (including change list indices, change dumps, etc.)

See Also:



96
97
98
# File 'lib/resync/shared/augmented.rb', line 96

def until_time
  .until_time if 
end