lib/liquidscript/icr/code.rb in liquidscript-0.11.0.rc1 vs lib/liquidscript/icr/code.rb in liquidscript-0.11.0

- old
+ new

@@ -17,10 +17,12 @@ # This is an array. # # @return [Array] attr_reader :arguments + attr_reader :metadata + alias_method :type, :action include Representable # Initializes the code. It takes an action and @@ -30,33 +32,66 @@ # @param action [Symbol] # @param arguments [Array] def initialize(action, *arguments) @action = action @arguments = arguments + @metadata = {} end # Turns the code into an array, containing the # action and the arguments. Note that changing # this array will not change the code. # # @return [Array] def to_a - [@action, *@arguments] + part = [@action] + part.concat(@arguments) + part end # If this code respresents something with a definite # value. # # @return [Boolean] def value? @_value ||= ![ :class, :module, :if, :elseif, :unless, :else, :try, :catch, :finally, :while, :for_in, - :for_seg, :return + :for_seg, :return, :exec ].include?(@action) end + # Access either the metadata or the codes. If + # the accessor is a Symbol, it access the metadata; + # if it the accessor is a Numeric, it access the + # codes. + # + # @param key [Symbol, Numeric] the key. + # @return [Object] + def [](key) + if argument_key?(key) + super + else + @metadata[key] + end + end + + # Sets something from the metadata. Unlike the + # accessor, it does not distinguish between + # Numeric and Symbol keys. + # + # @param key [Object] the key. + # @param value [Object] the value. + # @return [Object] + def []=(key, value) + if argument_key?(key) + super + else + @metadata[key] = value + end + end + # If we don't respond to it, the @arguments array # might. Ask them if they do, and if they don't, # respond accordingly. # # @param method [Symbol] the method to check. @@ -71,9 +106,15 @@ # exist here. # # @return [Object] def method_missing(method, *args, &block) @arguments.public_send(method, *args, &block) + end + + private + + def argument_key?(key) + key.is_a?(Numeric) or key.is_a?(Range) end end end end