Class: OpenActive::BaseModel

Inherits:
Object
  • Object
show all
Includes:
Concerns::JsonLdSerializable, Concerns::TypeChecker
Defined in:
lib/openactive/base_model.rb

Direct Known Subclasses

JsonLdModel, Rpde::RpdeBody, Rpde::RpdeItem

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::TypeChecker

#check_types, included

Methods included from Concerns::JsonLdSerializable

included, #to_h, #values

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



8
9
10
11
12
# File 'lib/openactive/base_model.rb', line 8

def initialize(attributes = {})
  assign_attributes(attributes) if attributes

  super()
end

Class Method Details

.define_property(property, types: nil, as: nil, default: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/openactive/base_model.rb', line 20

def self.define_property(property, types: nil, as: nil, default: nil)
  attr_accessor property
  property property, as: as || property.to_s if as != false
  validate_property property, types: types if types

  if default
    define_method property do
      instance_variable_get("@#{property}") || default
    end
  end
  property
end

.deserialize(value) ⇒ mixed

Returns a value from a given JSON-LD deserialized array.

Parameters:

  • value (mixed)

    If an array is provided, we recursively deserialize it

Returns:

  • (mixed)


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
# File 'lib/openactive/base_model.rb', line 71

def self.deserialize(value)
  if value.is_a?(Hash)
    # If an associative array with a type, return its deserialization form,
    # so that it gets converted from array to object
    # (associative arrays are still arrays in PHP)
    if value.key?("@type") || value.key?("type")

      type = value["@type"] || value["type"]

      # only schema is in a subdir, everything else is flat
      type = type.split(":")[1] if type.include?(':') && !type.start_with?("schema:")

      klass = ::OpenActive::Models.const_get(type)

      inst = klass.deserialize_class(value)

      return inst
    end
  elsif value.is_a?(Array)
    # NOTE: OpenActive is more strict than schema.org in this regard, so no single element array handling here.
    # If providing a non-associative array
    # Loop through it and serialize each item if needed
    value = value.map do |item|
      deserialize(item)
    end
  end
  value
end

.deserialize_class(data) ⇒ object

Returns an object from a given JSON-LD representation.

Parameters:

  • data (string, Array)

    If a string is provided, we attempt JSON-decoding first

Returns:

  • (object)


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/openactive/base_model.rb', line 54

def self.deserialize_class(data)
  inst = new

  # If data provided is not an array, return an empty class
  return inst unless data.is_a?(Array) || data.is_a?(Hash)

  data.each do |key, value|
    inst.set_property(key, value)
  end

  inst
end

.serialize(obj, **kwargs) ⇒ string

Returns the JSON-LD representation of the given instance.

TODO: make this more Ruby-esque (to_h, to_hash, to_json)

Parameters:

Returns:

  • (string)

    JSON-LD string representation of the given instance.



109
110
111
# File 'lib/openactive/base_model.rb', line 109

def self.serialize(obj, **kwargs)
  ::OpenActive::Helpers::JsonLd.prepare_data_for_serialization(obj, **kwargs)
end

Instance Method Details

#assign_attributes(attributes) ⇒ Object



14
15
16
17
18
# File 'lib/openactive/base_model.rb', line 14

def assign_attributes(attributes)
  attributes.each do |key, value|
    set_property(key, value)
  end
end

#deserialize(*data) ⇒ Object



100
101
102
# File 'lib/openactive/base_model.rb', line 100

def deserialize(*data)
  self.class.deserialize(*data)
end

#serialize(**kwargs) ⇒ Object



113
114
115
# File 'lib/openactive/base_model.rb', line 113

def serialize(**kwargs)
  self.class.serialize(self, **kwargs)
end

#set_property(key, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/openactive/base_model.rb', line 33

def set_property(key, value)
  attr_name = key.to_s.underscore

  attr_name = attr_name[1..] if attr_name.start_with?('@')

  val = value

  val = deserialize(value) if value.is_a?(Array) || value.is_a?(Hash)

  return if ["context", "type"].include?(attr_name)

  # Calling the setter will type-enforce it
  send("#{attr_name}=", val)
end

#to_json(schema: false, pretty: false) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/openactive/base_model.rb', line 117

def to_json(schema: false, pretty: false)
  serialized = serialize(schema: schema)

  return JSON.pretty_generate(serialized) if pretty

  serialized.to_json
end