X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Factivesupport%2Flib%2Factive_support%2Fcore_ext%2Fobject%2Finstance_variables.rb;fp=vendor%2Frails%2Factivesupport%2Flib%2Factive_support%2Fcore_ext%2Fobject%2Finstance_variables.rb;h=4ecaab3bbb2abe3b1dc1c5e8444f45f953db714a;hb=d115f2e23823271635bad69229a42cd8ac68debe;hp=0000000000000000000000000000000000000000;hpb=37cb670bf3ddde90b214e591f100ed4446469484;p=depot.git diff --git a/vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb b/vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb new file mode 100644 index 0000000..4ecaab3 --- /dev/null +++ b/vendor/rails/activesupport/lib/active_support/core_ext/object/instance_variables.rb @@ -0,0 +1,74 @@ +class Object + # Available in 1.8.6 and later. + unless respond_to?(:instance_variable_defined?) + def instance_variable_defined?(variable) + instance_variables.include?(variable.to_s) + end + end + + # Returns a hash that maps instance variable names without "@" to their + # corresponding values. Keys are strings both in Ruby 1.8 and 1.9. + # + # class C + # def initialize(x, y) + # @x, @y = x, y + # end + # end + # + # C.new(0, 1).instance_values # => {"x" => 0, "y" => 1} + def instance_values #:nodoc: + instance_variables.inject({}) do |values, name| + values[name.to_s[1..-1]] = instance_variable_get(name) + values + end + end + + # Returns an array of instance variable names including "@". They are strings + # both in Ruby 1.8 and 1.9. + # + # class C + # def initialize(x, y) + # @x, @y = x, y + # end + # end + # + # C.new(0, 1).instance_variable_names # => ["@y", "@x"] + if RUBY_VERSION >= '1.9' + def instance_variable_names + instance_variables.map { |var| var.to_s } + end + else + alias_method :instance_variable_names, :instance_variables + end + + # Copies the instance variables of +object+ into +self+. + # + # Instance variable names in the +exclude+ array are ignored. If +object+ + # responds to protected_instance_variables the ones returned are + # also ignored. For example, Rails controllers implement that method. + # + # In both cases strings and symbols are understood, and they have to include + # the at sign. + # + # class C + # def initialize(x, y, z) + # @x, @y, @z = x, y, z + # end + # + # def protected_instance_variables + # %w(@z) + # end + # end + # + # a = C.new(0, 1, 2) + # b = C.new(3, 4, 5) + # + # a.copy_instance_variables_from(b, [:@y]) + # # a is now: @x = 3, @y = 1, @z = 2 + def copy_instance_variables_from(object, exclude = []) #:nodoc: + exclude += object.protected_instance_variables if object.respond_to? :protected_instance_variables + + vars = object.instance_variables.map(&:to_s) - exclude.map(&:to_s) + vars.each { |name| instance_variable_set(name, object.instance_variable_get(name)) } + end +end