Class: Saper::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/saper/core/item.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](type) ⇒ Saper::Argument

Returns a subclass with specified type.

Parameters:

  • type (Symbol)

    action type

Returns:



25
26
27
# File 'lib/saper/core/item.rb', line 25

def self.[](type)
  subclasses[type.to_s] || raise(InvalidItem, "Invalid action argument: %s" % type)
end

.exists?(type) ⇒ Boolean

Returns ‘true` if there is a subclass with specified type.

Parameters:

  • type (Symbol)

    action type

Returns:

  • (Boolean)


32
33
34
# File 'lib/saper/core/item.rb', line 32

def self.exists?(type)
  subclasses.keys.include?(type.to_s)
end

.inherited(base) ⇒ Class

Tracks subclasses of Saper::Argument.

Returns:

  • (Class)


6
7
8
# File 'lib/saper/core/item.rb', line 6

def self.inherited(base)
  subclasses[base.type] = base
end

.new(*args, &block) ⇒ Saper::Argument

Returns a new instance of Saper::Argument.

Returns:



49
50
51
52
53
54
55
# File 'lib/saper/core/item.rb', line 49

def self.new(*args, &block)
  if self == Item
    self[args.shift].new(*args, &block)
  else
    super(*args, &block)
  end
end

.subclassesHash

Returns a hash of subclasses.

Returns:

  • (Hash)


12
13
14
# File 'lib/saper/core/item.rb', line 12

def self.subclasses
  @subclasses ||= {}
end

.try(*args, &block) ⇒ Saper::Argument

Returns a new instance of Saper::Argument. Returns nil and fails silently if there is an error during initialization.

Returns:



39
40
41
42
43
44
45
# File 'lib/saper/core/item.rb', line 39

def self.try(*args, &block)
  begin
    new(*args, &block)
  rescue InvalidItem, ArgumentError
    nil
  end
end

.typeString

Returns class name as an underscored string.

Returns:

  • (String)


18
19
20
# File 'lib/saper/core/item.rb', line 18

def self.type
  name.split("::").last.gsub(/([a-z])([A-Z])/,'\1_\2').downcase
end

Instance Method Details

#==(other) ⇒ Boolean

Returns ‘true` if other item contains the same data.

Returns:

  • (Boolean)


59
60
61
# File 'lib/saper/core/item.rb', line 59

def ==(other)
  to_s == other.to_s
end

#typeSymbol

Returns class name as a Symbol.

Returns:

  • (Symbol)


65
66
67
# File 'lib/saper/core/item.rb', line 65

def type
  self.class.type.to_sym
end