Sha256: 974f1d6be0efee543e45d4f10f91452ed55cd50c0dce05eae4b11d94937ece3f

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module Trailblazer
  class Finder
    # Helper module
    module Utils
      class String
        def self.blank?(string)
          return false if numeric?(string)

          string.nil? || string.strip.empty?
        end

        def self.numeric?(string)
          !Float(string).nil?
        rescue StandardError
          false
        end

        def self.camelize(text)
          text.to_s.gsub(/(?:^|_)(.)/) { Regexp.last_match[1].upcase }
        end

        def self.underscore(text)
          text
            .to_s.gsub(/::/, "/")
            .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
            .gsub(/([a-z\d])([A-Z])/, '\1_\2')
            .tr("-", "_")
            .tr(" ", "_")
            .downcase
        end

        def self.to_date(value)
          Date.parse(value).strftime("%Y-%m-%d") if date?(value)
        end

        def self.date?(date)
          date_hash = Date._parse(date.to_s)
          Date.valid_date?(date_hash[:year].to_i, date_hash[:mon].to_i, date_hash[:mday].to_i)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
trailblazer-finder-0.2.6 lib/trailblazer/finder/utils/string.rb
trailblazer-finder-0.2.5 lib/trailblazer/finder/utils/string.rb