Class: Demiurge::ActionItemInternal::ActionItemStateWrapper Private

Inherits:
Object
  • Object
show all
Defined in:
lib/demiurge/action_item.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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.

Since:

  • 0.0.1

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ ActionItemStateWrapper

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.

Returns a new instance of ActionItemStateWrapper

Since:

  • 0.0.1



546
547
548
# File 'lib/demiurge/action_item.rb', line 546

def initialize(item)
  @item = item
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ 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.



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# 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

Instance Method Details

#[](key) ⇒ 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.

Since:

  • 0.0.1



554
555
556
557
558
559
560
561
# 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) ⇒ 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.

Since:

  • 0.0.1



563
564
565
# File 'lib/demiurge/action_item.rb', line 563

def []=(key, value)
  @item.__state_internal[key] = value
end

#has_key?(key) ⇒ Boolean

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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



550
551
552
# File 'lib/demiurge/action_item.rb', line 550

def has_key?(key)
  @item.__state_internal.has_key?(key)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

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.

Returns:

  • (Boolean)

Since:

  • 0.0.1



594
595
596
# 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