Module: FIRM::Serializable::SerializeClassMethods

Includes:
JSON::SerializeClassMethods, XML::SerializeClassMethods
Defined in:
lib/firm/serializable.rb,
lib/firm/serializer/xml.rb,
lib/firm/serializer/json.rb

Overview

extend serialization class methods

Instance Method Summary collapse

Methods included from JSON::SerializeClassMethods

#json_create

Methods included from XML::SerializeClassMethods

#from_xml

Instance Method Details

#define_deserialize_finalizer(meth = nil) {|obj| ... } ⇒ void Also known as: deserialize_finalizer

This method returns an undefined value.

Defines a finalizer method/proc/block to be called after all properties have been deserialized and restored. Procs or blocks will be called with the deserialized object as the single argument. Unbound methods will be bound to the deserialized object before calling. Explicitly specifying nil will undefine the finalizer.

Parameters:

  • meth (Symbol, String, Proc, UnboundMethod, nil) (defaults to: nil)

    name of instance method, proc or method to call for finalizing

Yield Parameters:

  • obj (Object)

    deserialized object to finalize



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/firm/serializable.rb', line 474

def define_deserialize_finalizer(meth=nil, &block)
  if block and meth.nil?
    # the given block should expect and use the given object instance
    set_deserialize_finalizer(block)
  elsif meth and block.nil?
    h_meth = case meth
             when ::Symbol, ::String
               Serializable::MethodResolver.new(self, meth)
             when ::Proc
               # check arity == 1
               if meth.arity != 1
                 Kernel.raise ArgumentError,
                              "Deserialize finalizer Proc should expect a single argument",
                              caller
               end
               meth
             when ::UnboundMethod
               # check arity == 0
               if meth.arity>0
                 Kernel.raise ArgumentError,
                              "Deserialize finalizer method should not expect any argument",
                              caller
               end
               ->(obj) { meth.bind(obj).call }
             else
               Kernel.raise ArgumentError,
                            "Specify deserialize finalizer with a method, name, proc OR block",
                            caller
             end
    set_deserialize_finalizer(h_meth)
  elsif meth.nil? and block.nil?
    set_deserialize_finalizer(nil)
  else
    Kernel.raise ArgumentError,
                 "Specify deserialize finalizer with a method, name, proc OR block",
                 caller
  end
  nil
end

#deserialize(source, format: Serializable.default_format) ⇒ Object

Deserializes object from source data

Parameters:

  • source (IO, String)

    source data (String or IO(-like object))

  • format (Symbol, String) (defaults to: Serializable.default_format)

    data format of source

Returns:

  • (Object)

    deserialized object



519
520
521
# File 'lib/firm/serializable.rb', line 519

def deserialize(source, format: Serializable.default_format)
  Serializable.deserialize(source, format: format)
end

#excluded_property(*props) ⇒ void Also known as: excluded_properties, excludes

This method returns an undefined value.

Excludes a serializable property for instances of this class. (mostly/only useful to exclude properties from base classes which do not require serialization for derived class)

Parameters:

  • props (Symbol, String)

    one or more ids of serializable properties



460
461
462
# File 'lib/firm/serializable.rb', line 460

def excluded_property(*props)
  excluded_serializer_properties.merge props.flatten.collect { |prop| prop }
end

#property(*props, force: false, optional: false) ⇒ void #property(hash, force: false, optional: false) ⇒ void #property(*props, force: false, handler: nil, optional: false) {|id, obj, val| ... } ⇒ void Also known as: properties, contains

Adds (a) serializable property(-ies) for instances of his class (and derived classes)

Overloads:

  • #property(*props, force: false, optional: false) ⇒ void

    This method returns an undefined value.

    Specifies one or more serialized properties. The serialization framework will determine the availability of setter and getter methods automatically by looking for methods "#{prop_id}=(v)", "#set{prop_id}(v)" or "#{prop_id}(v)" for setters and "#{prop_id}()" or "#get{prop_id}" for getters.

    Parameters:

    • props (Symbol, String)

      one or more ids of serializable properties

    • force (Boolean) (defaults to: false)

      overrides any #disable_serialize for the properties specified

    • optional (Object) (defaults to: false)

      indicates optionality; if false the property will not be optional; true means optional if the serialized value == nil; any value other than ‘false’ or ‘true’ means optional if the serialize value equals that value; alternatively a Proc, Lambda (gets the object and the property id passed) or UnboundMethod (gets the property id passed) can be specified which is called at serialization time to determine the default (optional) value

  • #property(hash, force: false, optional: false) ⇒ void
    Note:

    Use *val to specify the optional value argument for setter requests instead of val=nil to be able to support setting explicit nil values.

    This method returns an undefined value.

    Specifies one or more serialized properties with associated setter/getter method ids/procs/lambda-s. Procs with setter support MUST accept 1 or 2 arguments (1 for getter, 2 for setter) where the first argument will always be the property owner’s object instance and the second (in case of a setter proc) the value to restore.

    Examples:

    property(
      prop_a: ->(obj, *val) {
                obj.my_prop_a_setter(val.first) unless val.empty?
                obj.my_prop_a_getter
              },
      prop_b: Proc.new { |obj, *val|
                obj.my_prop_b_setter(val.first) unless val.empty?
                obj.my_prop_b_getter
              },
      prop_c: :serialization_method)

    Parameters:

    • hash (Hash)

      a hash of pairs of property ids and getter/setter procs

    • force (Boolean) (defaults to: false)

      overrides any #disable_serialize for the properties specified

    • optional (Object) (defaults to: false)

      indicates optionality; if false the property will not be optional; true means optional if the serialized value == nil; any value other than ‘false’ or ‘true’ means optional if the serialize value equals that value; alternatively a Proc, Lambda (gets the object and the property id passed) or UnboundMethod (gets the property id passed) can be specified which is called at serialization time to determine the default (optional) value

  • #property(*props, force: false, handler: nil, optional: false) {|id, obj, val| ... } ⇒ void
    Note:

    Use *val to specify the optional value argument for setter requests instead of val=nil to be able to support setting explicit nil values.

    This method returns an undefined value.

    Specifies one or more serialized properties with a getter/setter handler proc/method/block. The getter/setter proc or block should accept either 2 (property id and object for getter) or 3 arguments (property id, object and value for setter) and is assumed to handle getter/setter requests for all specified properties. The getter/setter method should accept either 1 (property id for getter) or 2 arguments (property id and value for setter) and is assumed to handle getter/setter requests for all specified properties.

    Examples:

    property(:property_a, :property_b, :property_c) do |id, obj, *val|
      case id
        when :property_a
          ...
        when :property_b
          ...
        when :property_c
          ...
      end
    end

    Parameters:

    • props (Symbol, String)

      one or more ids of serializable properties

    • force (Boolean) (defaults to: false)

      overrides any #disable_serialize for the properties specified

    • handler (Symbol, String, Proc) (defaults to: nil)

      serialization handler method name or Proc

    • optional (Object) (defaults to: false)

      indicates optionality; if false the property will not be optional; true means optional if the serialized value == nil; any value other than ‘false’ or ‘true’ means optional if the serialize value equals that value; alternatively a Proc, Lambda (gets the object and the property id passed) or UnboundMethod (gets the property id passed) can be specified which is called at serialization time to determine the default (optional) value

    Yield Parameters:

    • id (Symbol, String)

      property id

    • obj (Object)

      object instance

    • val (Object)

      optional property value to set in case of setter request



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/firm/serializable.rb', line 428

def property(*props, **kwargs, &block)
  forced = !!kwargs.delete(:force)
  optional = kwargs.has_key?(:optional) ? kwargs.delete(:optional) : false
  if block || kwargs[:handler]
    props.each do |prop|
      serializer_properties << Property.new(self, prop, force: forced, handler: kwargs[:handler], optional: optional, &block)
    end
  else
    props.flatten.each do |prop|
      if ::Hash === prop
        prop.each_pair do |pn, pp|
          serializer_properties << Property.new(self, pn, pp, force: forced, optional: optional)
        end
      else
        serializer_properties << Property.new(self, prop, force: forced, optional: optional)
      end
    end
    unless kwargs.empty?
      kwargs.each_pair do |pn, pp|
        serializer_properties << Property.new(self, pn, pp, force: forced, optional: optional)
      end
    end
  end
end