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



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

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.



566
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
# File 'lib/demiurge/action_item.rb', line 566

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



553
554
555
556
557
558
559
560
# File 'lib/demiurge/action_item.rb', line 553

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



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

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



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

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



593
594
595
# File 'lib/demiurge/action_item.rb', line 593

def respond_to_missing?(method_name, include_private = false)
  @item.state.has_key?(method_name.to_s) || super
end