class MtgDb::Models::Card

Public Instance Methods

is_planeswalker?() click to toggle source
# File lib/mtg_db/models.rb, line 64
def is_planeswalker?
  supertype.name == 'Planeswalker'
end
is_transformable?() click to toggle source

Card.collect {|c| c if c.is_transformable?}.compact

# File lib/mtg_db/models.rb, line 69
def is_transformable?
  rules =~ /transform/io
end
is_vanguard?() click to toggle source
# File lib/mtg_db/models.rb, line 60
def is_vanguard?
  supertype.name == 'Vanguard'
end
power=(new_power) click to toggle source

When power is non-integer (e.g. *), store it in the NonIntAttribute table and use a dummy value in Card table

Calls superclass method
# File lib/mtg_db/models.rb, line 32
def power=(new_power)
  if !new_power.nil?
    if !looks_like_number?(new_power)
      attr = NonIntAttribute.find(original_attribute: new_power)
      if attr.nil?
        card_attribute = -65_535 + NonIntAttribute.all.size
        attr = NonIntAttribute.find_or_create(original_attribute: new_power, card_attribute: card_attribute)
      end
      super(attr.card_attribute)
    else
      super(new_power)
    end
  else
    super(new_power)
  end
end
subtype=(new_subtype) click to toggle source

Take care of assignment to the foreign key

Calls superclass method
# File lib/mtg_db/models.rb, line 24
def subtype=(new_subtype)
  if new_subtype.is_a? String
    value = Subtype.find_or_create(name: new_subtype)
  end
  super(value)
end
supertype=(new_supertype) click to toggle source

Take care of assignment to the foreign key

Calls superclass method
# File lib/mtg_db/models.rb, line 16
def supertype=(new_supertype)
  if new_supertype.is_a? String
    value = Supertype.find_or_create(name: new_supertype)
  end
  super(value)
end
toughness=(new_toughness) click to toggle source
Calls superclass method
# File lib/mtg_db/models.rb, line 49
def toughness=(new_toughness)
  return if new_toughness.nil?

  if looks_like_number?(new_toughness)
    super(new_toughness)
  else
    attr = find_or_create_non_int_attribute(new_toughness)
    super(attr.card_attribute)
  end
end

Private Instance Methods

find_or_create_non_int_attribute(attr) click to toggle source

Find or create the NonIntAttribute relevant to the given power or toughness

# File lib/mtg_db/models.rb, line 88
def find_or_create_non_int_attribute(attr)
  non_int_attr = NonIntAttribute.find(original_attribute: attr)
  if non_int_attr.nil?
    card_attribute = -65_535 + NonIntAttribute.all.size
    non_int_attr = NonIntAttribute.find_or_create(original_attribute: attr, card_attribute: card_attribute)
  end
  return non_int_attr
end
looks_like_number?(attr) click to toggle source

Test if the given value is a number or not Used for testing power and toughness, as they are sometimes non-int. e.g. *, {1/2}, etc

# File lib/mtg_db/models.rb, line 77
def looks_like_number?(attr)
  return false if attr.nil?

  if attr.match?(/^[+-]?\d+$/)
    true
  else
    false
  end
end