Class: SchemaDotOrg::SchemaType
- Inherits:
-
ValidatedObject::Base
- Object
- ValidatedObject::Base
- SchemaDotOrg::SchemaType
show all
- Defined in:
- lib/schema_dot_org.rb
Overview
Base class for schema types. Refactors out common code.
Constant Summary
collapse
- ROOT_ATTR =
{ '@context' => 'http://schema.org' }.freeze
- UNQUALIFIED_CLASS_NAME_REGEX =
/([^:]+)$/.freeze
Instance Method Summary
collapse
Instance Method Details
#_to_json_struct ⇒ Object
45
46
47
|
# File 'lib/schema_dot_org.rb', line 45
def _to_json_struct
raise 'For subclasses to implement'
end
|
#to_json(pretty: false, as_root: false) ⇒ Object
29
30
31
32
33
34
35
36
37
|
# File 'lib/schema_dot_org.rb', line 29
def to_json(pretty: false, as_root: false)
structure = as_root ? ROOT_ATTR.merge(to_json_struct) : to_json_struct
if pretty
JSON.pretty_generate(structure)
else
structure.to_json
end
end
|
#to_json_ld(pretty: false) ⇒ Object
25
26
27
|
# File 'lib/schema_dot_org.rb', line 25
def to_json_ld(pretty: false)
"<script type=\"application/ld+json\">\n" + to_json(pretty: pretty, as_root: true) + "\n</script>"
end
|
#to_json_struct ⇒ Object
Use the class name to create the “@type” attribute.
41
42
43
|
# File 'lib/schema_dot_org.rb', line 41
def to_json_struct
{ '@type' => un_namespaced_classname }.merge(_to_json_struct.compact)
end
|
#to_s ⇒ Object
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/schema_dot_org.rb', line 14
def to_s
json_string = to_json_ld(pretty: (!rails_production? && !ENV['SCHEMA_DOT_ORG_MINIFIED_JSON']))
if json_string.respond_to?(:html_safe)
json_string.html_safe
else
json_string
end
end
|
#un_namespaced_classname ⇒ Object
Returns the classname without the module namespace.
50
51
52
53
|
# File 'lib/schema_dot_org.rb', line 50
def un_namespaced_classname
self.class.name =~ UNQUALIFIED_CLASS_NAME_REGEX
Regexp.last_match(1)
end
|