Sha256: fd8f105425a75db518aefdb69cbb5d7431f78c24b1f733a4652ed7d7e4dcf29f

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

# String helpers
class ::String
  def bool_to_symbol
    case self
    when /NOT/
      :not
    when /AND/
      :and
    else
      :or
    end
  end

  def date?
    dup.force_encoding('utf-8').match(/^\d{4}-\d{2}-\d{2}/) ? true : false
  end

  def time?
    dup.force_encoding('utf-8').match(/ \d{1,2}(:\d\d)? *([ap]m)?/i)
  end

  def to_date
    Chronic.parse(self.dup.force_encoding('utf-8'))
  end

  def strip_time
    dup.force_encoding('utf-8').sub(/ \d{1,2}(:\d\d)? *([ap]m)?/i, '')
  end

  def to_day(time = :end)
    t = time == :end ? '23:59' : '00:00'
    Chronic.parse("#{self.strip_time} #{t}")
  end

  def number?
    to_f > 0
  end

  def bool?
    dup.force_encoding('utf-8').match(/^(?:y(?:es)?|no?|t(?:rue)?|f(?:alse)?)$/) ? true : false
  end

  def to_bool!
    replace to_bool
  end

  ##
  ## Returns a bool representation of the string.
  ##
  ## @return     [Boolean] Bool representation of the object.
  ##
  def to_bool
    case self
    when /^[yt]/i
      true
    else
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
marked-conductor-1.0.6 lib/conductor/string.rb
marked-conductor-1.0.5 lib/conductor/string.rb