Class: String
Direct Known Subclasses
Class Method Summary collapse
-
.random(length) ⇒ Object
Generates string of random text of the specified length.
Instance Method Summary collapse
- #adjective ⇒ Object (also: #adjectivize)
- #blank? ⇒ Boolean
- #capitalize ⇒ Object
- #commaize(places = 3) ⇒ Object
- #content_length ⇒ Object
- #headercase ⇒ Object (also: #headerize)
- #html_escape ⇒ Object
-
#original_sequel_titlecase ⇒ Object
adapt titlecase method to downcase short prepositions in middle.
-
#original_sequel_underscore ⇒ Object
adapt underscore method to convert whitespace to underscores.
-
#prepend_html(addition, tag) ⇒ Object
Prepend addition just inside the specified tag of document.
- #remove_tags ⇒ Object (also: #strip_tags)
- #titlecase ⇒ Object (also: #titleize)
- #to_const(klass = Kernel) ⇒ Object
- #underscore ⇒ Object
- #unless_blank(alt_value = nil) ⇒ Object
- #unless_equal_to(value, alt_value = nil) ⇒ Object
- #url_escape ⇒ Object (also: #url_encode)
-
#url_unescape ⇒ Object
Adapted from Rack::Utils.unescape.
-
#validate(format, options = {}) ⇒ Object
Validates value against specified format.
Class Method Details
.random(length) ⇒ Object
Generates string of random text of the specified length.
567 568 569 570 571 572 573 574 575 |
# File 'lib/kiss/ext/core.rb', line 567 def random(length) chars = [('A'..'H'), ('J'..'N'), ('P'..'R'), ('T'..'Y'), ('3'..'4'), ('6'..'9')].map {|r| r.to_a }.flatten # array text = '' size = chars.size length.times { text += chars[rand(size)] } text end |
Instance Method Details
#adjective ⇒ Object Also known as: adjectivize
635 636 637 |
# File 'lib/kiss/ext/core.rb', line 635 def adjective gsub(/\s/, '-') end |
#blank? ⇒ Boolean
585 586 587 |
# File 'lib/kiss/ext/core.rb', line 585 def blank? self !~ /\S/ end |
#capitalize ⇒ Object
617 618 619 620 621 |
# File 'lib/kiss/ext/core.rb', line 617 def capitalize result = self.downcase result[0,1] = result[0,1].upcase result end |
#commaize(places = 3) ⇒ Object
578 579 580 581 582 583 584 |
# File 'lib/kiss/ext/core.rb', line 578 def commaize(places = 3) s = reverse while s.sub!(/(\d{3})(\d)/, '\1,\2') next end s.reverse end |
#content_length ⇒ Object
602 603 604 |
# File 'lib/kiss/ext/core.rb', line 602 def content_length bytesize rescue length end |
#headercase ⇒ Object Also known as: headerize
630 631 632 |
# File 'lib/kiss/ext/core.rb', line 630 def headercase original_sequel_underscore.capitalize.gsub('_', '-') end |
#html_escape ⇒ Object
653 654 655 |
# File 'lib/kiss/ext/core.rb', line 653 def html_escape Rack::Utils.escape_html(self) end |
#original_sequel_titlecase ⇒ Object
adapt titlecase method to downcase short prepositions in middle
624 |
# File 'lib/kiss/ext/core.rb', line 624 alias_method :original_sequel_titlecase, :titlecase |
#original_sequel_underscore ⇒ Object
adapt underscore method to convert whitespace to underscores
612 |
# File 'lib/kiss/ext/core.rb', line 612 alias_method :original_sequel_underscore, :underscore |
#prepend_html(addition, tag) ⇒ Object
Prepend addition just inside the specified tag of document.
648 649 650 651 |
# File 'lib/kiss/ext/core.rb', line 648 def prepend_html(addition, tag) regexp = /(\<#{tag}[^\>]*\>)/i self =~ regexp ? sub(regexp, '\1' + addition) : (addition + self) end |
#remove_tags ⇒ Object Also known as:
606 607 608 |
# File 'lib/kiss/ext/core.rb', line 606 def gsub(/<\/?[^>]*>/, "") end |
#titlecase ⇒ Object Also known as: titleize
625 626 627 |
# File 'lib/kiss/ext/core.rb', line 625 def titlecase original_sequel_titlecase.gsub(/(\S)( +)(at|for|in|of|on|to)( +)(\S)/i) {"#{$1}#{$2}#{$3.downcase}#{$4}#{$5}"} end |
#to_const(klass = Kernel) ⇒ Object
589 590 591 592 593 594 595 596 597 598 599 600 |
# File 'lib/kiss/ext/core.rb', line 589 def to_const(klass = Kernel) begin parts = self.split(/::/) while (next_part = parts.shift) klass = klass.const_get(next_part) end klass rescue raise "Constant '#{self}' not defined" end end |
#underscore ⇒ Object
613 614 615 |
# File 'lib/kiss/ext/core.rb', line 613 def underscore original_sequel_underscore.gsub(/\W+/, '_').sub(/_\Z/, '') end |
#unless_blank(alt_value = nil) ⇒ Object
640 641 642 |
# File 'lib/kiss/ext/core.rb', line 640 def unless_blank(alt_value = nil) self.blank? ? alt_value : self end |
#unless_equal_to(value, alt_value = nil) ⇒ Object
643 644 645 |
# File 'lib/kiss/ext/core.rb', line 643 def unless_equal_to(value, alt_value = nil) (self == value) ? alt_value : self end |
#url_escape ⇒ Object Also known as: url_encode
657 658 659 660 |
# File 'lib/kiss/ext/core.rb', line 657 def url_escape # encode space to '+'; don't encode letters, numbers, periods gsub(/([^\w\.])/) { sprintf("%%%02X", $&.unpack("C")[0]) } end |
#url_unescape ⇒ Object
Adapted from Rack::Utils.unescape.
664 665 666 667 668 |
# File 'lib/kiss/ext/core.rb', line 664 def url_unescape self.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end end |
#validate(format, options = {}) ⇒ Object
Validates value against specified format. If required is true, value must contain a non-whitespace character. If required is false, value need not match format if and only if value contains only whitespace.
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/kiss/ext/core.rb', line 673 def validate(format, = {}) label = [:label] || 'value' if [:required] && (self.blank?) # value required raise "#{label} required and missing" elsif format && !self.blank? format_class = Kiss::Format.lookup(format) begin format_class.validate(self, [:context] || {}) rescue Kiss::Format::ValidateError => e raise "validation error on #{label}: #{e.}" end end end |