Module: Redstruct::Utils::Scriptable::ClassMethods
- Defined in:
- lib/redstruct/utils/scriptable.rb
Overview
Class methods added when the module is included at the class level (i.e. extend)
Instance Method Summary collapse
-
#defscript(id, script) ⇒ Object
Creates a method with the given id, which will create a constant and a method in the class.
Instance Method Details
#defscript(id, script) ⇒ Object
Creates a method with the given id, which will create a constant and a method in the class. This allows you to use defscript as a macro for your lua scripts, which gets translated to Ruby code at compile time.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/redstruct/utils/scriptable.rb', line 21 def defscript(id, script) raise ArgumentError, 'no script given' unless script && !script.empty? script = script.strip constant = "SCRIPT_#{id.upcase}" if const_defined?(constant) Redstruct.logger.warn("cowardly aborting defscript #{id}; constant with name #{constant} already exists!") return end if method_defined?(id) Redstruct.logger.warn("cowardly aborting defscript #{id}; method with name #{id} already exists!") return end class_eval <<~METHOD, __FILE__, __LINE__ + 1 #{constant} = { script: %(#{script}).freeze, sha1: Digest::SHA1.hexdigest(%(#{script})).freeze }.freeze def #{id}(keys: [], argv: []) return @factory.script(#{constant}[:script], sha1: #{constant}[:sha1]).eval(keys: keys, argv: argv) end METHOD end |