Class: Newsmlg2::CatalogStore

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/newsmlg2/catalog_store.rb

Overview

The set of catalogs known to one NewsML-G2 document: inline elements plus catalogs resolved from hrefs (IPTC standard catalogs resolve to the files bundled with this gem). Owns scheme alias and URI lookups used for qcode expansion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCatalogStore

Returns a new instance of CatalogStore.



11
12
13
# File 'lib/newsmlg2/catalog_store.rb', line 11

def initialize
  @catalogs = []
end

Instance Attribute Details

#catalogsObject (readonly)

Returns the value of attribute catalogs.



15
16
17
# File 'lib/newsmlg2/catalog_store.rb', line 15

def catalogs
  @catalogs
end

Instance Method Details

#[](index) ⇒ Object



37
38
39
# File 'lib/newsmlg2/catalog_store.rb', line 37

def [](index)
  catalogs[index]
end

#add_catalog(catalog) ⇒ Object Also known as: <<



17
18
19
20
# File 'lib/newsmlg2/catalog_store.rb', line 17

def add_catalog(catalog)
  catalogs << catalog
  self
end

#add_catalog_ref(href) ⇒ Object



23
24
25
26
# File 'lib/newsmlg2/catalog_store.rb', line 23

def add_catalog_ref(href)
  catalog = Newsmlg2::CatalogCache.load(href)
  add_catalog(catalog) if catalog
end

#eachObject



28
29
30
# File 'lib/newsmlg2/catalog_store.rb', line 28

def each(&)
  catalogs.each(&)
end

#get_scheme_for_alias(alias_name) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/newsmlg2/catalog_store.rb', line 41

def get_scheme_for_alias(alias_name)
  schemes.each do |scheme|
    return scheme if scheme.alias_attr == alias_name
  end
  raise AliasNotFoundInCatalogs,
        'no catalog of this document declares the scheme alias ' \
        "'#{alias_name}'"
end

#get_scheme_for_uri(uri) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/newsmlg2/catalog_store.rb', line 50

def get_scheme_for_uri(uri)
  schemes.each do |scheme|
    return scheme if scheme.uri == uri
  end
  raise UriNotFoundInCatalogs,
        "no catalog of this document declares the scheme URI '#{uri}'"
end

#lengthObject Also known as: size



32
33
34
# File 'lib/newsmlg2/catalog_store.rb', line 32

def length
  catalogs.length
end

#schemesObject



58
59
60
# File 'lib/newsmlg2/catalog_store.rb', line 58

def schemes
  catalogs.flat_map(&:schemes)
end

#to_sObject



62
63
64
# File 'lib/newsmlg2/catalog_store.rb', line 62

def to_s
  "#<Newsmlg2::CatalogStore #{length} catalog(s)>"
end