-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.ts
More file actions
98 lines (87 loc) · 3.23 KB
/
Context.ts
File metadata and controls
98 lines (87 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
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
// ----------------------------------------------------------------
// PURPOSE:
// This class implements the Context class.
//
// CONTEXT SOFTWARE PATTERN:
// The goal of Context Object is not to pass lots of parameters to
// methods implicitly, as a means of by-passing strong typing and
// encapsulation. The goal is to store scoped data in a general,
// but managed way, independent of protocols and presentation technology.
// ----------------------------------------------------------------
import { Language } from './enums/Language'
import { Book } from './models/Book'
import { Product } from './interfaces/Product'
import { Api } from './interfaces/Api'
import { Section } from './interfaces/Section'
import { Log } from './utils/Logging'
import {
BookContextError,
LanguageContextError,
ProductContextError,
SectionContextError
} from './errors/Context'
export abstract class Context {
private static language: Language
private static book: Book
private static section: Section
private static product: Product
// String serializers.
public static toString(): string {
return `<${Context.constructor.name}}>`
}
// ----------------------------------------------------------------
// Product getter and setter.
// ----------------------------------------------------------------
public static getProduct(): Product {
if (!Context.product) {
throw new ProductContextError('No Product context found!')
}
return Context.product
}
public static setProduct(product: Product) {
Log.info('Setting context product', product)
Context.product = product
}
// ----------------------------------------------------------------
// Section getter and setter.
// ----------------------------------------------------------------
public static getSection(): Section {
if (!Context.section) {
throw new SectionContextError('No Section context found!')
}
return Context.section
}
public static getApi(): Api {
return Context.getSection().getApi()
}
public static setSection(section: Section) {
Log.info('Setting context section', section)
Context.section = section
}
// ----------------------------------------------------------------
// Book getter and setter.
// ----------------------------------------------------------------
public static getBook(): Book {
if (!Context.book) {
throw new BookContextError('No Book context found!')
}
return Context.book
}
public static setBook(book: Book) {
Log.info('Setting context book', book)
Context.book = book
}
// ----------------------------------------------------------------
// Language getter and setter.
// ----------------------------------------------------------------
public static getLanguage(): Language {
if (!Context.language) {
throw new LanguageContextError('No Language context found!')
}
return <Language>Context.language
}
public static setLanguage(language: Language) {
Log.info('Setting context language', language)
Context.language = <Language>language
}
}