class Esse::Hstring
The idea here is to add useful methods to the ruby core objects without monkey patching. And on this state and not thinking about to add ActiveSupport dependency
Attributes
value[R]
Public Class Methods
def_conventional(bang_method, conv_method = nil)
click to toggle source
# File lib/esse/primitives/hstring.rb, line 15 def self.def_conventional(bang_method, conv_method = nil) conv_method ||= bang_method.to_s.sub(/[!?]*$/, '') if public_instance_methods.include?(conv_method) msg = format( 'Equivalent %<conv>p already defined for the bang method %<bang>p', conv: conv_method.to_s, bang: bang_method.to_s, ) raise(SyntaxError, msg) end unless public_instance_methods.include?(bang_method) msg = format( 'Undefined method %<bang>p for %<klass>p', bang: bang_method.to_s, klass: self, ) raise(SyntaxError, msg) end define_method(conv_method) do |*args| self.class.new(self).public_send(bang_method, *args) end end
new(value)
click to toggle source
# File lib/esse/primitives/hstring.rb, line 40 def initialize(value) @value = value.to_s end
Public Instance Methods
camelize!()
click to toggle source
# File lib/esse/primitives/hstring.rb, line 44 def camelize! @value = @value.split(/(?=[_A-Z])/).map { |str| str.tr('_', '').capitalize }.join self end
demodulize!()
click to toggle source
# File lib/esse/primitives/hstring.rb, line 50 def demodulize! @value = @value.split('::').last self end
modulize!()
click to toggle source
# File lib/esse/primitives/hstring.rb, line 56 def modulize! @value = @value.split(%r{\:\:|\\|/}).map { |part| self.class.new(part).camelize.to_s }.join('::') self end
presence!()
click to toggle source
# File lib/esse/primitives/hstring.rb, line 77 def presence! return @value = nil if @value == '' return @value = nil unless @value @value end
underscore!()
click to toggle source
# File lib/esse/primitives/hstring.rb, line 62 def underscore! @value = @value .sub(/^\:\:/, '') .gsub('::', '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .gsub(/\s/, '_') .gsub(/__+/, '_') .downcase self end