Created
January 1, 2018 07:49
-
-
Save willnationsdev/1962cc62757ec370fde20c387a440bbc to your computer and use it in GitHub Desktop.
GitRef: a Reference type for tracking pending changes to another target object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # GitRef | |
| # Author: willnationsdev | |
| # Enables users to pass around a reference to pending changes for another target object. | |
| # The changes can be applied to the target or reset with the apply() and reset() functions. | |
| # To create layers of pending changes, call branch() and receive a new GitRef with the | |
| # calling GitRef as its upstream reference. | |
| # In a chained GitRef, apply() and reset() still modify the target as before, but now | |
| # revert() can be called to reset the target to the state of the upstream GitRef. | |
| extends Reference | |
| var __is_base__= true | |
| var __backup__ = null setget , get_backup | |
| var __upstream__ = null setget , get_upstream | |
| var __data__ = null setget , get_data | |
| func _init(p_node): | |
| __upstream__ = p_node | |
| if p_node and p_node is get_script(): | |
| __backup__ = p_node.get_backup() | |
| else: | |
| __backup__ = {} | |
| func _get(p_name): | |
| for a_prop in get_property_list(): | |
| if a_prop.name == p_name: | |
| return get(p_name) | |
| return __data__[p_name] | |
| func _set(p_name, p_value): | |
| for a_prop in get_property_list(): | |
| if a_prop.name == p_name: | |
| return set(p_name, p_value) | |
| __backup__[p_name] = get_base().get(p_name) | |
| __data__[p_name] = p_value | |
| func get_data(): | |
| return __data__ | |
| func get_upstream(): | |
| return __upstream__ | |
| func get_backup(): | |
| return __backup__ | |
| func branch(): | |
| return get_script().new(self) | |
| func apply(): | |
| __is_base__ = false | |
| var node = get_base() | |
| for a_prop in __data__: | |
| node.set(a_prop, __data__[a_prop]) | |
| func revert(): | |
| var is_base_val = true | |
| var node = get_base() | |
| for a_prop in __data__: | |
| var val = null | |
| if __upstream__.get_data().has(a_prop): | |
| val = __upstream__.get(a_prop) | |
| else: | |
| val = __backup__[a_prop] | |
| __backup__.erase(a_prop) | |
| node.set(a_prop, val) | |
| for a_prop in __data__: | |
| if __backup__[a_prop] != __data__[a_prop]: | |
| is_base_val = false | |
| break | |
| if is_base_val: | |
| __is_base__ = true | |
| func reset(): | |
| __is_base__ = true | |
| var node = get_base() | |
| for a_prop in __data__: | |
| node.set(a_prop, __backup__[a_prop]) | |
| func get_base(): | |
| var node = __upstream__ | |
| while node is get_script(): | |
| node = node.get_upstream() | |
| return node | |
| func is_base(): | |
| return __is_base__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment