Sha256: ee51d115f22c403e4b303a925b8a9d119ff4354103e9f1be4580e5338dcc53d4

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

##
# A wrapper that allows instance variables to be manipulated using +[]+ and
# +[]=+

module Spec
  module Rails
    module DSL
      class IvarProxy #:nodoc:

        ##
        # Wraps +object+ allowing its instance variables to be manipulated.

        def initialize(object)
          @object = object
        end

        ##
        # Retrieves +ivar+ from the wrapped object.

        def [](ivar)
          get_variable "@#{ivar}"
        end

        ##
        # Sets +ivar+ to +val+ on the wrapped object.

        def []=(ivar, val)
          set_variable "@#{ivar}", val
        end

        def each
          @object.instance_variables.each do |variable_full_name|
            variable_name = variable_full_name[1...variable_full_name.length]
            yield variable_name, get_variable(variable_full_name)
          end
        end

        def delete(key)
          var_name = "@#{key}"
          if @object.instance_variables.include?(var_name)
            @object.send(:remove_instance_variable, var_name)
          else
            return nil
          end
        end

        def has_key?(key)
          @object.instance_variables.include?("@#{key}")
        end

        protected
        def get_variable(name)
          @object.instance_variable_get name
        end

        def set_variable(name, value)
          @object.instance_variable_set name, value
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
deckshuffler-0.0.2 vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/ivar_proxy.rb
has_finder-0.1.1 spec/rails/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/ivar_proxy.rb
has_finder-0.1.2 spec/rails/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/ivar_proxy.rb
has_finder-0.1.3 spec/rails/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/ivar_proxy.rb