Class: Coarnotify::Core::Notify::NotifyPatternPart

Inherits:
NotifyBase
  • Object
show all
Defined in:
lib/coarnotify/core/notify.rb

Overview

Base class for all pattern parts, such as objects, contexts, actors, etc

If there is a default type specified, and a type is not given at construction, then the default type will be added

Direct Known Subclasses

NotifyActor, NotifyItem, NotifyObject, NotifyService

Instance Attribute Summary

Attributes inherited from NotifyBase

#properties_by_reference, #validate_properties, #validate_stream_on_construct, #validators

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NotifyBase

#doc, #get_property, #id, #id=, #optional_and_validate, #register_property_validation_error, #required, #required_and_validate, #set_property, #to_jsonld, #type, #validate, #validate_property

Constructor Details

#initialize(stream: nil, validate_stream_on_construct: true, validate_properties: true, validators: nil, validation_context: nil, properties_by_reference: true) ⇒ NotifyPatternPart

Constructor for the NotifyPatternPart

If there is a default type specified, and a type is not given at construction, then the default type will be added

Parameters:

  • stream (ActivityStreams2::ActivityStream, Hash) (defaults to: nil)

    The activity stream object, or a hash from which one can be created

  • validate_stream_on_construct (Boolean) (defaults to: true)

    should the incoming stream be validated at construction-time

  • validate_properties (Boolean) (defaults to: true)

    should individual properties be validated as they are set

  • validators (Validate::Validator) (defaults to: nil)

    the validator object for this class and all nested elements

  • validation_context (String, Array) (defaults to: nil)

    the context in which this object is being validated

  • properties_by_reference (Boolean) (defaults to: true)

    should properties be get and set by reference (the default) or by value



561
562
563
564
565
566
567
# File 'lib/coarnotify/core/notify.rb', line 561

def initialize(stream: nil, validate_stream_on_construct: true, validate_properties: true,
               validators: nil, validation_context: nil, properties_by_reference: true)
  super(stream: stream, validate_stream_on_construct: validate_stream_on_construct,
        validate_properties: validate_properties, validators: validators,
        validation_context: validation_context, properties_by_reference: properties_by_reference)
  self.type = self.class.default_type if self.class.default_type && type.nil?
end

Class Method Details

.allowed_typesObject

The list of types that are permissible for this object. If the list is empty, then any type is allowed



546
547
548
# File 'lib/coarnotify/core/notify.rb', line 546

def self.allowed_types
  []
end

.default_typeObject

The default type for this object, if none is provided on construction



541
542
543
# File 'lib/coarnotify/core/notify.rb', line 541

def self.default_type
  nil
end

Instance Method Details

#allowed_typesArray<String>

Get the allowed types for this object

Returns:

  • (Array<String>)

    the allowed types



572
573
574
# File 'lib/coarnotify/core/notify.rb', line 572

def allowed_types
  self.class.allowed_types
end

#type=(types) ⇒ Object

Set the type of the object, and validate that it is one of the allowed types if present

Parameters:

  • types (String, Array<String>)

    the type(s) to set



579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/coarnotify/core/notify.rb', line 579

def type=(types)
  types = [types] unless types.is_a?(Array)

  if !allowed_types.empty?
    types.each do |t|
      unless allowed_types.include?(t)
        raise ArgumentError, "Type value #{t} is not one of the permitted values"
      end
    end
  end

  # keep single values as single values, not arrays
  types = types.length == 1 ? types[0] : types

  set_property(ActivityStreams2::Properties::TYPE, types)
end