Module: FIRM::Serializable::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
-
#define_deserialize_finalizer(meth = nil) {|obj| ... } ⇒ void
(also: #deserialize_finalizer)
Defines a finalizer method/proc/block to be called after all properties have been deserialized and restored.
-
#deserialize(source, format: Serializable.default_format) ⇒ Object
Deserializes object from source data.
-
#excluded_property(*props) ⇒ void
(also: #excluded_properties, #excludes)
Excludes a serializable property for instances of this class.
-
#property(*props, **kwargs, &block) ⇒ Object
(also: #properties, #contains)
Adds (a) serializable property(-ies) for instances of his class (and derived classes).
Methods included from JSON::SerializeClassMethods
Methods included from XML::SerializeClassMethods
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.
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
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)
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)
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 |