Class: RichString
- Inherits:
-
Object
- Object
- RichString
- Defined in:
- lib/brut/junk_drawer.rb
Overview
A wrapper around a string to avoid adding a ton of methods to String
.
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ Object
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
-
#camelize ⇒ Object
-
#capitalize(*options) ⇒ RichString
Capitalizes the string, with the ability to only capitalize the first letter.
-
#humanized ⇒ Object
-
#initialize(string) ⇒ RichString
constructor
A new instance of RichString.
-
#length ⇒ Object
-
#to_s ⇒ Object
-
#to_s_or_nil ⇒ Object
-
#to_str ⇒ Object
-
#underscorized ⇒ Object
Constructor Details
#initialize(string) ⇒ RichString
Returns a new instance of RichString.
61 62 63 |
# File 'lib/brut/junk_drawer.rb', line 61 def initialize(string) @string = string.to_s end |
Class Method Details
.from_string(string, blank_is_nil: true) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/brut/junk_drawer.rb', line 55 def self.from_string(string,blank_is_nil:true) if string.to_s.strip == "" && blank_is_nil return nil end self.new(string) end |
Instance Method Details
#+(other) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/brut/junk_drawer.rb', line 127 def +(other) if other.kind_of?(RichString) RichString.new(self.to_s + other.to_s) elsif other.kind_of?(String) self.to_s + other else super(other) end end |
#<=>(other) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/brut/junk_drawer.rb', line 117 def <=>(other) if other.kind_of?(RichString) self.to_s <=> other.to_s elsif other.kind_of?(String) self.to_s <=> other else super end end |
#==(other) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/brut/junk_drawer.rb', line 107 def ==(other) if other.kind_of?(RichString) self.to_s == other.to_s elsif other.kind_of?(String) self.to_s == other else false end end |
#camelize ⇒ Object
74 75 76 77 78 |
# File 'lib/brut/junk_drawer.rb', line 74 def camelize @string.to_s.split(/[_-]/).map { |part| RichString.new(part).capitalize(:first_only).to_s }.join("") end |
#capitalize(*options) ⇒ RichString
Capitalizes the string, with the ability to only capitalize the first letter.
If options
includes :first_only
, then only the first letter of the string is capitalized. The remaining letters are left
alone. If option
does not include :first_only
, this capitalizes like Ruby's standard library, which is to lower case all
letters save for the first.
88 89 90 91 92 93 94 95 |
# File 'lib/brut/junk_drawer.rb', line 88 def capitalize(*) if .include?(:first_only) .delete(:first_only) self.class.new(@string[0].capitalize(*) + @string[1..-1]) else self.class.new(@string.capitalize(*)) end end |
#humanized ⇒ Object
97 98 99 |
# File 'lib/brut/junk_drawer.rb', line 97 def humanized RichString.new(@string.tr("_-"," ")) end |
#length ⇒ Object
103 |
# File 'lib/brut/junk_drawer.rb', line 103 def length = to_s.length |
#to_s ⇒ Object
101 |
# File 'lib/brut/junk_drawer.rb', line 101 def to_s = @string |
#to_s_or_nil ⇒ Object
105 |
# File 'lib/brut/junk_drawer.rb', line 105 def to_s_or_nil = @string.empty? ? nil : self.to_s |
#to_str ⇒ Object
102 |
# File 'lib/brut/junk_drawer.rb', line 102 def to_str = self.to_s |
#underscorized ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/brut/junk_drawer.rb', line 65 def underscorized return self unless /[A-Z-]|::/.match?(@string) word = @string.gsub("::", "/") word.gsub!(/(?<=[A-Z])(?=[A-Z][a-z])|(?<=[a-z\d])(?=[A-Z])/, "_") word.tr!("-", "_") word.downcase! RichString.new(word) end |