Class: Pubid::Rendering::RenderingContext

Inherits:
Object
  • Object
show all
Defined in:
lib/pubid/rendering/context.rb

Overview

Context object for flavor-specific rendering rules

This encapsulates the rendering rules that vary between flavors, allowing components to render themselves correctly based on context.

Usage

context = RenderingContext.new(
  stage_separator: "/",
  stage_separator_with_copublisher: " ",
  type_separator: "/",
  type_separator_with_prefix: " ",
  default_type_abbr: "IS"
)
identifier.to_s(context: context)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage_separator: "/", stage_separator_with_copublisher: " ", type_separator: "/", type_separator_with_prefix: " ", default_type_abbr: "IS", lang: :en, lang_single: false, with_language_code: :none, stage_format_long: false, with_date: true) ⇒ RenderingContext

Initialize a new rendering context

Parameters:

  • stage_separator (String) (defaults to: "/")

    separator before stage without copublisher

  • stage_separator_with_copublisher (String) (defaults to: " ")

    separator before stage with copublisher

  • type_separator (String) (defaults to: "/")

    separator before type (default)

  • type_separator_with_prefix (String) (defaults to: " ")

    separator before type when prefix exists

  • default_type_abbr (String) (defaults to: "IS")

    default type abbreviation to skip rendering

  • lang (Symbol) (defaults to: :en)

    language for rendering (:en or :fr)

  • lang_single (Boolean) (defaults to: false)

    use single char language format



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pubid/rendering/context.rb', line 36

def initialize(stage_separator: "/",
               stage_separator_with_copublisher: " ",
               type_separator: "/",
               type_separator_with_prefix: " ",
               default_type_abbr: "IS",
               lang: :en,
               lang_single: false,
               with_language_code: :none,
               stage_format_long: false,
               with_date: true)
  @stage_separator = stage_separator
  @stage_separator_with_copublisher = stage_separator_with_copublisher
  @type_separator = type_separator
  @type_separator_with_prefix = type_separator_with_prefix
  @default_type_abbr = default_type_abbr
  @lang = lang
  @lang_single = lang_single
  @with_language_code = with_language_code
  @stage_format_long = stage_format_long
  @with_date = with_date
end

Instance Attribute Details

#default_type_abbrObject (readonly)

Returns the value of attribute default_type_abbr.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def default_type_abbr
  @default_type_abbr
end

#langObject (readonly)

Returns the value of attribute lang.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def lang
  @lang
end

#lang_singleObject (readonly)

Returns the value of attribute lang_single.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def lang_single
  @lang_single
end

#stage_format_longObject (readonly)

Returns the value of attribute stage_format_long.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def stage_format_long
  @stage_format_long
end

#stage_separatorObject (readonly)

Returns the value of attribute stage_separator.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def stage_separator
  @stage_separator
end

#stage_separator_with_copublisherObject (readonly)

Returns the value of attribute stage_separator_with_copublisher.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def stage_separator_with_copublisher
  @stage_separator_with_copublisher
end

#type_separatorObject (readonly)

Returns the value of attribute type_separator.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def type_separator
  @type_separator
end

#type_separator_with_prefixObject (readonly)

Returns the value of attribute type_separator_with_prefix.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def type_separator_with_prefix
  @type_separator_with_prefix
end

#with_dateObject (readonly)

Returns the value of attribute with_date.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def with_date
  @with_date
end

#with_language_codeObject (readonly)

Returns the value of attribute with_language_code.



22
23
24
# File 'lib/pubid/rendering/context.rb', line 22

def with_language_code
  @with_language_code
end

Class Method Details

.from_format(format) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pubid/rendering/context.rb', line 143

def self.from_format(format)
  case format
  when :ref_num_short then ref_num_short
  when :ref_num_long then ref_num_long
  when :ref_dated then ref_dated
  when :ref_dated_long then ref_dated
  when :ref_undated then ref_undated
  when :ref_undated_long then ref_undated_long
  else ref_dated
  end
end

.iecRenderingContext

IEC rendering context singleton

Returns:



96
97
98
99
100
101
102
103
104
# File 'lib/pubid/rendering/context.rb', line 96

def self.iec
  @iec ||= new(
    stage_separator: "/",
    stage_separator_with_copublisher: " ",
    type_separator: "/",
    type_separator_with_prefix: " ",
    default_type_abbr: "IS",
  )
end

.isoRenderingContext

ISO rendering context singleton

Returns:



84
85
86
87
88
89
90
91
92
# File 'lib/pubid/rendering/context.rb', line 84

def self.iso
  @iso ||= new(
    stage_separator: "/",
    stage_separator_with_copublisher: " ",
    type_separator: "/",
    type_separator_with_prefix: " ",
    default_type_abbr: "IS",
  )
end

.nistRenderingContext

NIST rendering context singleton

Returns:



108
109
110
111
112
113
114
115
116
# File 'lib/pubid/rendering/context.rb', line 108

def self.nist
  @nist ||= new(
    stage_separator: "",
    stage_separator_with_copublisher: " ",
    type_separator: " ",
    type_separator_with_prefix: " ",
    default_type_abbr: "",
  )
end

.ref_datedObject



128
129
130
131
# File 'lib/pubid/rendering/context.rb', line 128

def self.ref_dated
  new(with_language_code: :none, stage_format_long: false,
      with_date: true)
end

.ref_num_longObject



124
125
126
# File 'lib/pubid/rendering/context.rb', line 124

def self.ref_num_long
  new(with_language_code: :iso, stage_format_long: true, with_date: true)
end

.ref_num_shortObject

Format factory methods — replace RenderingStyle hierarchy



119
120
121
122
# File 'lib/pubid/rendering/context.rb', line 119

def self.ref_num_short
  new(with_language_code: :single, stage_format_long: false,
      with_date: true)
end

.ref_undatedObject



133
134
135
136
# File 'lib/pubid/rendering/context.rb', line 133

def self.ref_undated
  new(with_language_code: :none, stage_format_long: false,
      with_date: false)
end

.ref_undated_longObject



138
139
140
141
# File 'lib/pubid/rendering/context.rb', line 138

def self.ref_undated_long
  new(with_language_code: :none, stage_format_long: true,
      with_date: false)
end

Instance Method Details

#should_render_type?(type_abbr) ⇒ Boolean

Check if type should be rendered (not default)

Parameters:

  • type_abbr (String)

    type abbreviation to check

Returns:

  • (Boolean)

    true if type should be rendered



78
79
80
# File 'lib/pubid/rendering/context.rb', line 78

def should_render_type?(type_abbr)
  type_abbr && type_abbr != @default_type_abbr
end

#stage_separator_for(has_copublisher:) ⇒ String

Get the appropriate stage separator based on whether there’s a copublisher

Parameters:

  • has_copublisher (Boolean)

    whether identifier has copublisher

Returns:

  • (String)

    the separator to use



62
63
64
# File 'lib/pubid/rendering/context.rb', line 62

def stage_separator_for(has_copublisher:)
  has_copublisher ? @stage_separator_with_copublisher : @stage_separator
end

#type_separator_for(has_prefix:) ⇒ String

Get the appropriate type separator based on whether there’s a prefix

Parameters:

  • has_prefix (Boolean)

    whether there’s a prefix (stage or copublisher)

Returns:

  • (String)

    the separator to use



70
71
72
# File 'lib/pubid/rendering/context.rb', line 70

def type_separator_for(has_prefix:)
  has_prefix ? @type_separator_with_prefix : @type_separator
end