Class: SpriteKit::Sprite

Inherits:
Object
  • Object
show all
Defined in:
mygame/lib/sprite_kit/sprite.rb

Direct Known Subclasses

ToolDrawer::Counter

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Sprite

Returns a new instance of Sprite.



6
7
8
# File 'mygame/lib/sprite_kit/sprite.rb', line 6

def initialize(**kwargs)
  kwargs.each { |k, v| send("#{k}=", v) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'mygame/lib/sprite_kit/sprite.rb', line 22

def method_missing(m, *args, &block)
  name = m.to_s
  return super if name.end_with?("?", "!") || block

  if name.end_with?("=") && !name.end_with?("==") && args.length == 1
    self.class.attr_accessor(name.chomp("="))   # first write defines the accessor
    send(m, *args)
  elsif args.empty?
    instance_variable_get("@#{name}")   # nil if unset, the value if set
  else
    super
  end
end

Instance Method Details

#[](key) ⇒ Object



10
11
12
# File 'mygame/lib/sprite_kit/sprite.rb', line 10

def [](key)
  send(key)
end

#[]=(key, value) ⇒ Object



14
15
16
# File 'mygame/lib/sprite_kit/sprite.rb', line 14

def []=(key, value)
  send("#{key}=", value)
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'mygame/lib/sprite_kit/sprite.rb', line 18

def key?(key)
  respond_to?(key) || instance_variable_defined?(key)
end

#respond_to_missing?(m, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'mygame/lib/sprite_kit/sprite.rb', line 36

def respond_to_missing?(m, include_private = false)
  name = m.to_s
  if name.end_with?("=") && !name.end_with?("==")
    true
  elsif name.end_with?("?", "!")
    false
  else
    instance_variable_defined?("@#{name}")
  end
end

#serialize ⇒ Object



47
48
49
50
51
# File 'mygame/lib/sprite_kit/sprite.rb', line 47

def serialize
  instance_variables.to_h do |var|
    [var.to_s.delete_prefix("@").to_sym, instance_variable_get(var)]
  end
end