Class: Saper::Item
- Inherits:
-
Object
- Object
- Saper::Item
- Defined in:
- lib/saper/core/item.rb
Direct Known Subclasses
Saper::Items::Atom, Saper::Items::Document, Saper::Items::HTML, Saper::Items::JSON, Saper::Items::Markdown, Saper::Items::Nothing, Saper::Items::Text, Saper::Items::Time, Saper::Items::URL, Saper::Items::XML
Class Method Summary collapse
-
.[](type) ⇒ Saper::Argument
Returns a subclass with specified type.
-
.exists?(type) ⇒ Boolean
Returns ‘true` if there is a subclass with specified type.
-
.inherited(base) ⇒ Class
Tracks subclasses of Saper::Argument.
-
.new(*args, &block) ⇒ Saper::Argument
Returns a new instance of Saper::Argument.
-
.subclasses ⇒ Hash
Returns a hash of subclasses.
-
.try(*args, &block) ⇒ Saper::Argument
Returns a new instance of Saper::Argument.
-
.type ⇒ String
Returns class name as an underscored string.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns ‘true` if other item contains the same data.
-
#type ⇒ Symbol
Returns class name as a Symbol.
Class Method Details
.[](type) ⇒ Saper::Argument
Returns a subclass with specified type.
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.
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.
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.
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 |
.subclasses ⇒ Hash
Returns a hash of subclasses.
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.
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 |
.type ⇒ String
Returns class name as an underscored 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.
59 60 61 |
# File 'lib/saper/core/item.rb', line 59 def ==(other) to_s == other.to_s end |
#type ⇒ Symbol
Returns class name as a Symbol.
65 66 67 |
# File 'lib/saper/core/item.rb', line 65 def type self.class.type.to_sym end |