Sha256: 70789e8fe4040bf0b0b03a41962c9d4bd3181421e0dbba430cfb71c812c6f33f

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

module CoreExtensions
  module Object
    def number?
      true if Float(self)
    rescue StandardError
      false
    end

    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

    # @return [Card::Name]
    def cardname
      Card::Name.new self
    end
    alias_method :to_name, :cardname

    # @return [Card]
    def card
      Card[cardname]
    end

    # @return [Integer] id of card with name
    def card_id
      Card.id self
    end

    def codename
      Card::Codename[card_id]
    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
      is_a? Cardname
    end

    def in? other
      other.include? self
    end

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
card-1.108.1 config/initializers/01_core_extensions/object.rb
card-1.108.0 config/initializers/01_core_extensions/object.rb
card-1.107.0 config/initializers/01_core_extensions/object.rb
card-1.106.0 config/initializers/01_core_extensions/object.rb
card-1.105.6 config/initializers/01_core_extensions/object.rb
card-1.105.5 config/initializers/01_core_extensions/object.rb
card-1.105.4 config/initializers/01_core_extensions/object.rb
card-1.105.3 config/initializers/01_core_extensions/object.rb
card-1.105.2.pre1 config/initializers/01_core_extensions/object.rb
card-1.105.2 config/initializers/01_core_extensions/object.rb
card-1.105.1 config/initializers/01_core_extensions/object.rb
card-1.105.0 config/initializers/01_core_extensions/object.rb