Sha256: 6d6949fe9f63f42cbfd01f4db02f83bbd1f45c61ee6a4437dc0c7a6a6c819806

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module CoreExtensions
  module Object
    def deep_clone
      case self
      when Integer, Float, NilClass, FalseClass, TrueClass, Symbol
        klone = self
      when Hash
        klone = clone
        each { |k, v| klone[k] = v.deep_clone }
      when Array
        klone = clone
        klone.clear
        each { |v| klone << v.deep_clone }
      else
        klone = clone
      end
      klone.deep_clone_instance_variables
      klone
    end

    def send_unless method, *args, &_block
      (block_given? ? yield : self) || send(method, *args)
    end

    def send_if     method, *args, &_block
      (block_given? ? yield : self) && send(method, *args)
    end

    def to_name
      Card::Name.new self
    end

    def name?
      # Although we want to check for instances of class Card::Name we can't use that
      # class because it is renewed with every request
      # (at least in development mode) but the name cache is persistent.
      # Hence the name objects in the cache are objects of a different instance of the
      # Card::Name class and is_a?(Card::Name) will return false
      self.is_a? Cardname
    end

    def deep_clone_instance_variables
      instance_variables.each do |v|
        instance_variable_set v, instance_variable_get(v).deep_clone
      end
    end

    def in? other
      other.include? self
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
card-1.101.4 config/initializers/01_core_extensions/object.rb
card-1.101.3 config/initializers/01_core_extensions/object.rb
card-1.101.2 config/initializers/01_core_extensions/object.rb
card-1.101.1 config/initializers/01_core_extensions/object.rb
card-1.101.0 config/initializers/01_core_extensions/object.rb