Class: Python::Pickle::PyObject
- Inherits:
-
Object
- Object
- Python::Pickle::PyObject
- Defined in:
- lib/python/pickle/py_object.rb
Overview
Represents a Python object.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash{String => Object}
readonly
The populated attributes of the Python object.
-
#init_args ⇒ Array
readonly
The arguments used to initialize the Python object.
-
#init_kwargs ⇒ Array
readonly
The keyword arguments used to initialize the Python object.
-
#py_class ⇒ PyClass
readonly
The Python class of the Python object.
Instance Method Summary collapse
-
#__setstate__(new_attributes) ⇒ Object
private
Sets the state of the Python object.
-
#getattr(attribute) ⇒ Object
Fetches the attribute of the Python object.
-
#initialize(py_class, *args, **kwargs) ⇒ PyObject
constructor
private
Initializes the Python object.
-
#method_missing(method_name, *arguments, &block) ⇒ Object
protected
Allows for direct access to attributes.
-
#setattr(name, value) ⇒ Object
Sets an attribute in the Python object.
-
#to_h ⇒ Hash
Converts the Python object to a Hash.
Constructor Details
#initialize(py_class, *args, **kwargs) ⇒ PyObject
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the Python object.
44 45 46 47 48 49 50 |
# File 'lib/python/pickle/py_object.rb', line 44 def initialize(py_class,*args,**kwargs) @py_class = py_class @init_args = args @init_kwargs = kwargs @attributes = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object (protected)
Allows for direct access to attributes.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/python/pickle/py_object.rb', line 119 def method_missing(method_name,*arguments,&block) if method_name.end_with?('=') attr = method_name[0..-2] if arguments.length == 1 && !block return @attributes[attr] = arguments[0] else super(method_name,*arguments,&block) end else attr = method_name.to_s if @attributes.has_key?(attr) && arguments.empty? && !block return @attributes[attr] else super(method_name,*arguments,&block) end end end |
Instance Attribute Details
#attributes ⇒ Hash{String => Object} (readonly)
The populated attributes of the Python object.
28 29 30 |
# File 'lib/python/pickle/py_object.rb', line 28 def attributes @attributes end |
#init_args ⇒ Array (readonly)
The arguments used to initialize the Python object.
18 19 20 |
# File 'lib/python/pickle/py_object.rb', line 18 def init_args @init_args end |
#init_kwargs ⇒ Array (readonly)
The keyword arguments used to initialize the Python object.
23 24 25 |
# File 'lib/python/pickle/py_object.rb', line 23 def init_kwargs @init_kwargs end |
#py_class ⇒ PyClass (readonly)
The Python class of the Python object.
13 14 15 |
# File 'lib/python/pickle/py_object.rb', line 13 def py_class @py_class end |
Instance Method Details
#__setstate__(new_attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets the state of the Python object.
96 97 98 |
# File 'lib/python/pickle/py_object.rb', line 96 def __setstate__(new_attributes) @attributes = new_attributes end |
#getattr(attribute) ⇒ Object
Fetches the attribute of the Python object.
68 69 70 71 72 |
# File 'lib/python/pickle/py_object.rb', line 68 def getattr(attribute) @attributes.fetch(attribute) do raise(ArgumentError,"Python object has no attribute #{attribute.inspect}: #{self.inspect}") end end |
#setattr(name, value) ⇒ Object
Sets an attribute in the Python object.
87 88 89 |
# File 'lib/python/pickle/py_object.rb', line 87 def setattr(name,value) @attributes[name] = value end |
#to_h ⇒ Hash
Converts the Python object to a Hash.
105 106 107 |
# File 'lib/python/pickle/py_object.rb', line 105 def to_h @attributes end |