Class: Scjson::Types::LogProps

Inherits:
Object
  • Object
show all
Defined in:
lib/scjson/types.rb

Overview

diagnostic output statement.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ LogProps

Instantiate a new LogProps object.

Parameters:

  • kwargs (Hash)

    Optional keyword overrides.



1172
1173
1174
1175
1176
1177
# File 'lib/scjson/types.rb', line 1172

def initialize(**kwargs)
  @other_element = kwargs.fetch(:other_element, [])
  @label = kwargs.fetch(:label, nil)
  @expr = kwargs.fetch(:expr, nil)
  @other_attributes = kwargs.fetch(:other_attributes, {})
end

Instance Attribute Details

#exprObject

Returns the value of attribute expr.



1169
1170
1171
# File 'lib/scjson/types.rb', line 1169

def expr
  @expr
end

#labelObject

Returns the value of attribute label.



1169
1170
1171
# File 'lib/scjson/types.rb', line 1169

def label
  @label
end

#other_attributesObject

Returns the value of attribute other_attributes.



1169
1170
1171
# File 'lib/scjson/types.rb', line 1169

def other_attributes
  @other_attributes
end

#other_elementObject

Returns the value of attribute other_element.



1169
1170
1171
# File 'lib/scjson/types.rb', line 1169

def other_element
  @other_element
end

Class Method Details

.from_hash(data) ⇒ LogProps

Build an instance from a Hash representation.

Parameters:

  • data (Hash)

    Canonical hash representation.

Returns:

Raises:

  • (ArgumentError)


1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# File 'lib/scjson/types.rb', line 1182

def self.from_hash(data)
  raise ArgumentError, 'Expected Hash' unless data.is_a?(Hash)

  normalized = data.transform_keys(&:to_s)
  kwargs = {}
  kwargs[:other_element] = Array(normalized.fetch('other_element', []))
  kwargs[:label] = normalized.fetch('label', nil)
  kwargs[:expr] = normalized.fetch('expr', nil)
  kwargs[:other_attributes] = normalized.fetch('other_attributes', {})
  new(**kwargs)
end

.from_json(json) ⇒ LogProps

Deserialize an instance from a JSON payload.

Parameters:

  • json (String)

    JSON document to decode.

Returns:



1197
1198
1199
1200
# File 'lib/scjson/types.rb', line 1197

def self.from_json(json)
  parsed = JSON.parse(json)
  from_hash(parsed)
end

Instance Method Details

#to_hashHash

Convert the object to a Hash suitable for JSON serialization.

Returns:

  • (Hash)


1204
1205
1206
1207
1208
1209
1210
1211
# File 'lib/scjson/types.rb', line 1204

def to_hash
  {
    'other_element' => @other_element,
    'label' => @label,
    'expr' => @expr,
    'other_attributes' => @other_attributes
  }
end

#to_json(*opts) ⇒ String

Serialize the object to JSON.

Parameters:

  • opts (Array)

    JSON generation options.

Returns:

  • (String)


1216
1217
1218
# File 'lib/scjson/types.rb', line 1216

def to_json(*opts)
  JSON.generate(to_hash, *opts)
end