Class: Resync::Augmented
- Inherits:
-
Object
- Object
- Resync::Augmented
- Includes:
- XML::Mapping
- Defined in:
- lib/resync/shared/augmented.rb
Overview
Base class for elements augmented with ResourceSync-specific child elements.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#links ⇒ Array<Link>
Related links.
-
#metadata ⇒ Metadata
Metadata about this object.
Instance Method Summary collapse
-
#initialize(links: [], metadata: nil) ⇒ Augmented
constructor
Creates a new
Augmentedinstance with the specified links and metadata. -
#link_for(rel:) ⇒ Link
Shortcut to find the first link with the specified relation (in ResourceSync there often should be only one link with a particular relation).
-
#links_for(rel:) ⇒ Array<Link>
Finds links with the specified relation.
Constructor Details
#initialize(links: [], metadata: nil) ⇒ Augmented
Creates a new Augmented instance with the specified links and metadata.
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
#links ⇒ Array<Link>
Returns 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: 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 end |
#metadata ⇒ Metadata
Returns 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: 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 end |
Instance Method Details
#link_for(rel:) ⇒ Link
Shortcut to find the first link with the specified relation (in ResourceSync there often should be only one link with a particular relation)
72 73 74 |
# File 'lib/resync/shared/augmented.rb', line 72 def link_for(rel:) links.find { |l| l.rel == rel } end |
#links_for(rel:) ⇒ Array<Link>
Finds links with the specified relation.
64 65 66 |
# File 'lib/resync/shared/augmented.rb', line 64 def links_for(rel:) links.select { |l| l.rel == rel } end |