Class: Opencdd::Exporters::Json

Inherits:
Visitor
  • Object
show all
Defined in:
lib/opencdd/exporters/json.rb

Direct Known Subclasses

Yaml

Constant Summary collapse

PAYLOAD_BUILDERS =

───────────────────────────────────────────────────────────── Public per-entity payload API

payload_for(entity, database:) returns the wire-format Hash for a single entity. Dispatch is via the PAYLOAD_BUILDERS registry — adding a new entity type means adding one entry to the registry and one builder method, not editing a switch.

The optional database: enables cross-entity resolution (e.g. property → value_list). Without it, cross-links are omitted (payload still has all the entity's own fields). ─────────────────────────────────────────────────────────────

{
  Opencdd::Klass             => :class_node,
  Opencdd::Property          => :property_node,
  Opencdd::Unit              => :unit_node,
  Opencdd::ValueList         => :value_list_node,
  Opencdd::ValueTerm         => :value_term_node,
  Opencdd::Relation          => :relation_node,
  Opencdd::ViewControl       => :view_control_node,
  Opencdd::ListUnit          => :list_of_unit_node,
  Opencdd::DetClassification => :det_classification_node,
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Visitor

#seen

Instance Method Summary collapse

Methods inherited from Visitor

#each_sorted, #reset!, #visit_classes, #visit_list_of_units, #visit_properties, #visit_relations, #visit_units, #visit_value_lists, #visit_value_terms, #visit_view_controls

Constructor Details

#initializeJson

Returns a new instance of Json.



10
11
12
13
# File 'lib/opencdd/exporters/json.rb', line 10

def initialize
  super
  @nodes = []
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



8
9
10
# File 'lib/opencdd/exporters/json.rb', line 8

def nodes
  @nodes
end

Instance Method Details

#class_node(klass) ⇒ Object

───────────────────────────────────────────────────────────── Open/closed payload builders

Each per-type _node method is a thin wrapper that adds type: and any type-specific computed fields (e.g. Property's value_list cross-link). The bulk of every entity's payload comes from entity_payload, which iterates the field DSL registry (Opencdd::Entity::FieldRegistry.fields_for) — adding a field to the model is a single field declaration; no edits here. ─────────────────────────────────────────────────────────────



102
103
104
# File 'lib/opencdd/exporters/json.rb', line 102

def class_node(klass)
  entity_payload(klass).merge(type: "class").compact
end

#det_classification_node(det) ⇒ Object



138
139
140
# File 'lib/opencdd/exporters/json.rb', line 138

def det_classification_node(det)
  entity_payload(det).merge(type: "det_classification").compact
end

#list_of_unit_node(lou) ⇒ Object



134
135
136
# File 'lib/opencdd/exporters/json.rb', line 134

def list_of_unit_node(lou)
  entity_payload(lou).merge(type: "list_of_unit").compact
end

#payload_for(entity, database: nil) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
# File 'lib/opencdd/exporters/json.rb', line 84

def payload_for(entity, database: nil)
  @database = database if database
  builder = PAYLOAD_BUILDERS[entity.class]
  raise ArgumentError, "No JSON payload builder for #{entity.class}" unless builder
  public_send(builder, entity)
end

#property_node(prop) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/opencdd/exporters/json.rb', line 106

def property_node(prop)
  entity_payload(prop).merge(
    type: "property",
    data_type: prop.parsed_data_type&.to_s,
    value_list: value_list_irdi_of(prop),
  ).compact
end

#relation_node(rel) ⇒ Object



126
127
128
# File 'lib/opencdd/exporters/json.rb', line 126

def relation_node(rel)
  entity_payload(rel).merge(type: "relation").compact
end

#to_json(database, pretty: true) ⇒ Object



15
16
17
18
19
20
# File 'lib/opencdd/exporters/json.rb', line 15

def to_json(database, pretty: true)
  reset!
  @nodes.clear
  visit_database(database)
  pretty ? JSON.pretty_generate(@nodes) : JSON.generate(@nodes)
end

#unit_node(unit) ⇒ Object



114
115
116
# File 'lib/opencdd/exporters/json.rb', line 114

def unit_node(unit)
  entity_payload(unit).merge(type: "unit").compact
end

#value_list_node(vl) ⇒ Object



118
119
120
# File 'lib/opencdd/exporters/json.rb', line 118

def value_list_node(vl)
  entity_payload(vl).merge(type: "value_list").compact
end

#value_term_node(vt) ⇒ Object



122
123
124
# File 'lib/opencdd/exporters/json.rb', line 122

def value_term_node(vt)
  entity_payload(vt).merge(type: "value_term").compact
end

#view_control_node(vc) ⇒ Object



130
131
132
# File 'lib/opencdd/exporters/json.rb', line 130

def view_control_node(vc)
  entity_payload(vc).merge(type: "view_control").compact
end

#visit_class(klass) ⇒ Object



27
28
29
30
# File 'lib/opencdd/exporters/json.rb', line 27

def visit_class(klass)
  @nodes << class_node(klass)
  super
end

#visit_database(database) ⇒ Object



22
23
24
25
# File 'lib/opencdd/exporters/json.rb', line 22

def visit_database(database)
  @database = database
  super
end

#visit_list_of_unit(lou) ⇒ Object



56
57
58
# File 'lib/opencdd/exporters/json.rb', line 56

def visit_list_of_unit(lou)
  @nodes << list_of_unit_node(lou)
end

#visit_property(prop) ⇒ Object



32
33
34
# File 'lib/opencdd/exporters/json.rb', line 32

def visit_property(prop)
  @nodes << property_node(prop)
end

#visit_relation(rel) ⇒ Object



48
49
50
# File 'lib/opencdd/exporters/json.rb', line 48

def visit_relation(rel)
  @nodes << relation_node(rel)
end

#visit_unit(unit) ⇒ Object



36
37
38
# File 'lib/opencdd/exporters/json.rb', line 36

def visit_unit(unit)
  @nodes << unit_node(unit)
end

#visit_value_list(vl) ⇒ Object



40
41
42
# File 'lib/opencdd/exporters/json.rb', line 40

def visit_value_list(vl)
  @nodes << value_list_node(vl)
end

#visit_value_term(vt) ⇒ Object



44
45
46
# File 'lib/opencdd/exporters/json.rb', line 44

def visit_value_term(vt)
  @nodes << value_term_node(vt)
end

#visit_view_control(vc) ⇒ Object



52
53
54
# File 'lib/opencdd/exporters/json.rb', line 52

def visit_view_control(vc)
  @nodes << view_control_node(vc)
end