Class: Xolo::Core::BaseClasses::ServerObject

Inherits:
Object
  • Object
show all
Extended by:
JSONWrappers
Includes:
JSONWrappers
Defined in:
lib/xolo/core/base_classes/server_object.rb

Overview

The base class for "xolo objects stored on the xolo server", i.e. Titles and Versions/Patches - whether they are being used on the server, or in xadm.

This class holds stuff common to all no matter where or how they are used.

See also Title and Version

Direct Known Subclasses

Title, Version

Instance Method Summary collapse

Methods included from JSONWrappers

extended, included, parse_json

Constructor Details

#initialize(data_hash) ⇒ ServerObject

Constructor



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/xolo/core/base_classes/server_object.rb', line 46

def initialize(data_hash)
  # log_debug "Instantiating a #{self.class}..."

  self.class::ATTRIBUTES.each do |attr, deets|
    val = data_hash[attr]

    # log_debug "Initializing, setting ATTR '#{attr}' => '#{val}' (#{val.class})"

    # anything not nil, esp empty arrays, needs to be set
    next if val.nil?

    # convert timestamps to Time objects if needed,
    # All the other values shouldn't need converting
    # when taking in JSON or xadm opts.
    val = Time.parse(val.to_s) if deets[:type] == :time && !val.is_a?(Time)

    # call the setter
    send "#{attr}=", val
  end
end

Instance Method Details

#to_hString

Convert to a Hash for sending between xadm and the Xolo Server, or installing on clients.

Only the values defined in ATTRIBUTES are sent, because all other other attributes are meant only for the local context, i.e. on the server, via xadm, or via 'xolo'.

Returns:

  • (String)

    The attributes of this title as JSON



80
81
82
83
84
85
86
87
88
89
# File 'lib/xolo/core/base_classes/server_object.rb', line 80

def to_h
  hash = {}
  self.class::ATTRIBUTES.each do |attr, deets|
    hash[attr] = send attr

    # ensure multi values are arrays, even if they are empty
    hash[attr] = [hash[attr]].compact if deets[:multi] && !hash[attr].is_a?(Array)
  end
  hash
end

#to_json(*_args) ⇒ String

Convert to a JSON object for sending between xadm and the Xolo Server or storage on the server.

Always make it 'pretty', i.e. human readable, since it often gets stored in files that humans will look at

Returns:

  • (String)

    The attributes of this title as JSON



99
100
101
# File 'lib/xolo/core/base_classes/server_object.rb', line 99

def to_json(*_args)
  JSON.pretty_generate to_h
end