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,
}.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. ─────────────────────────────────────────────────────────────



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

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

#list_of_unit_node(lou) ⇒ Object



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

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

#payload_for(entity, database: nil) ⇒ Object

Raises:

  • (ArgumentError)


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

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



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

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



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

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



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

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

#value_list_node(vl) ⇒ Object



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

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

#value_term_node(vt) ⇒ Object



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

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

#view_control_node(vc) ⇒ Object



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

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