class Demiurge::ActionItemInternal::ActionItemStateWrapper
This class acts to wrap item state to avoid reading fields that haven't been set. Later, it may prevent access to protected state from lower-privilege code. Though it should always be kept in mind that no World File DSL
code is actually secure. At best, security in this API may prevent accidents by the well-intentioned.
ActionItemStateWrappers can act as Hashes (preferred) with square-bracket assignment, or can use (deprecated) method_missing
to set fields. The method_missing
version is deprecated both because it's slower and because it only allows certain key names to be used.
@api private @since 0.0.1
Public Class Methods
new(item)
click to toggle source
# File lib/demiurge/action_item.rb, line 546 def initialize(item) @item = item end
Public Instance Methods
[](key)
click to toggle source
# File lib/demiurge/action_item.rb, line 554 def [](key) unless @item.__state_internal.has_key?(key) raise ::Demiurge::Errors::NoSuchStateKeyError.new("No such state key as #{method_name.inspect}", "method" => method_name, "item" => @item.name, execution_context: @item.engine.execution_context) end @item.__state_internal[key] end
[]=(key, value)
click to toggle source
# File lib/demiurge/action_item.rb, line 563 def []=(key, value) @item.__state_internal[key] = value end
has_key?(key)
click to toggle source
# File lib/demiurge/action_item.rb, line 550 def has_key?(key) @item.__state_internal.has_key?(key) end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/demiurge/action_item.rb, line 567 def method_missing(method_name, *args, &block) if method_name.to_s[-1] == "=" getter_name = method_name.to_s[0..-2] setter_name = method_name.to_s else getter_name = method_name.to_s setter_name = method_name.to_s + "=" end if @item.state.has_key?(getter_name) || method_name.to_s[-1] == "=" self.class.send(:define_method, getter_name) do @item.__state_internal[getter_name] end self.class.send(:define_method, setter_name) do |val| @item.__state_internal[getter_name] = val end # Call to new defined method return self.send(method_name, *args, &block) end # Nope, no matching state. raise ::Demiurge::Errors::NoSuchStateKeyError.new("No such state key as #{method_name.inspect}", "method" => method_name, "item" => @item.name, execution_context: @item.engine.execution_context) super end
respond_to_missing?(method_name, include_private = false)
click to toggle source
Calls superclass method
# File lib/demiurge/action_item.rb, line 594 def respond_to_missing?(method_name, include_private = false) @item.state.has_key?(method_name.to_s) || super end