Class: SmartSortAtom
- Inherits:
-
Object
- Object
- SmartSortAtom
- Defined in:
- lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#initialize(value) ⇒ SmartSortAtom
constructor
A new instance of SmartSortAtom.
Constructor Details
#initialize(value) ⇒ SmartSortAtom
Returns a new instance of SmartSortAtom.
5 6 7 |
# File 'lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb', line 5 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb', line 3 def value @value end |
Class Method Details
.parse(string) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb', line 22 def self.parse(string) # Loosely based on http://stackoverflow.com/a/4079031 string.scan(/[^\d\.]+|[\d\.]+/).collect do |atom| if atom.match(/\d+(\.\d+)?/) atom = atom.to_f else atom = normalize_string(atom) end new(atom) end end |
Instance Method Details
#<=>(other) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/generators/katapult/basics/templates/lib/ext/string/to_sort_atoms.rb', line 9 def <=>(other) other.is_a?(self.class) or raise "Can only smart compare with other SmartSortAtom" left_value = value right_value = other.value if left_value.class == right_value.class left_value <=> right_value elsif left_value.is_a?(Float) -1 else 1 end end |