Module: Zif::Serializable
- Included in:
- ExampleApp::FutureLabel, ExampleApp::TallButton, Actions::Action, Actions::Sequence, RenderTarget, Scene, Sprite, UI::Input
- Defined in:
- lib/zif/serializable.rb
Overview
A mixin for automatically definining #serialize
based on instance variables with setter methods
If you have circular references in ivars (Foo.bar <-> Bar.foo), make sure one of the classes overrides #exclude_from_serialize and specifies the attr.
Instance Method Summary collapse
-
#exclude_from_serialize ⇒ Array<Symbol>
In the included class, override this method to exclude attrs from serialization / printing.
-
#inspect ⇒ String
Convert #serialize to string.
-
#serialize ⇒ Hash<Symbol, Object>
Convert #serialize to string.
-
#to_s ⇒ String
Convert #serialize to string.
Instance Method Details
#exclude_from_serialize ⇒ Array<Symbol>
In the included class, override this method to exclude attrs from serialization / printing
43 44 45 |
# File 'lib/zif/serializable.rb', line 43 def exclude_from_serialize %w[args] # Too much spam end |
#inspect ⇒ String
Returns Convert #serialize to string.
20 21 22 |
# File 'lib/zif/serializable.rb', line 20 def inspect serialize.to_s end |
#serialize ⇒ Hash<Symbol, Object>
Returns Convert #serialize to string.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/zif/serializable.rb', line 30 def serialize attrs = {} instance_variables.each do |var| str = var.to_s.gsub('@', '') next if exclude_from_serialize.include? str attrs[str.to_sym] = instance_variable_get var if respond_to? "#{str}=" end attrs end |
#to_s ⇒ String
Returns Convert #serialize to string.
25 26 27 |
# File 'lib/zif/serializable.rb', line 25 def to_s serialize.to_s end |