module Gamefic::Stage

A safe execution environment for narrative code.

Constants

OVERWRITEABLE_CLASSES
SWAPPABLE_VALUES

Private Class Methods

overwriteable?(cval, nval) click to toggle source
# File lib/gamefic/stage.rb, line 39
def overwriteable? cval, nval
  return true if cval.nil? || swappable?(cval, nval)

  allowed = OVERWRITEABLE_CLASSES.find { |klass| cval.is_a?(klass) }
  allowed && cval.is_a?(allowed)
end
swappable?(*values) click to toggle source
# File lib/gamefic/stage.rb, line 46
def swappable? *values
  values.all? { |val| SWAPPABLE_VALUES.include?(val) }
end
validate_changes(narrative, container, code) click to toggle source
# File lib/gamefic/stage.rb, line 22
def validate_changes narrative, container, code
  container.instance_variables.each do |var|
    next unless narrative.instance_variables.include?(var)

    cval = container.instance_variable_get(var)

    nval = narrative.instance_variable_get(var)
    next if cval == nval

    validate_overwriteable(cval, nval, "Unsafe reassignment of #{var} in #{code}")
  end
end
validate_overwriteable(cval, nval, error) click to toggle source
# File lib/gamefic/stage.rb, line 35
def validate_overwriteable cval, nval, error
  raise error unless overwriteable?(cval, nval)
end

Public Instance Methods

run(narrative, *args, &code) click to toggle source

@param narrative [Narrative]

# File lib/gamefic/stage.rb, line 10
def run(narrative, *args, &code)
  container = narrative.clone
  narrative.instance_exec(*args, &code).tap { validate_changes narrative, container, code }
end