Sha256: 73c01ddb3d87315804d3e01262dbb41a9882e370b9a5f3ba0ba190420ca0a83d

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true

module Anyway
  module Ext
    # Extend String through refinements
    module StringSerialize
      # Regexp to detect array values
      # Array value is a values that contains at least one comma
      # and doesn't start/end with quote
      ARRAY_RXP = /\A[^'"].*\s*,\s*.*[^'"]\z/

      refine ::String do
        def serialize
          case self
          when ARRAY_RXP
            split(/\s*,\s*/).map { |word| word.serialize }
          when /\A(true|t|yes|y)\z/i
            true
          when /\A(false|f|no|n)\z/i
            false
          when /\A(nil|null)\z/i
            nil
          when /\A\d+\z/
            to_i
          when /\A\d*\.\d+\z/
            to_f
          when /\A['"].*['"]\z/
            gsub(/(\A['"]|['"]\z)/, "")
          else
            self
          end
        end
      end

      using self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
anyway_config-2.0.0.pre2 lib/anyway/ext/string_serialize.rb
anyway_config-2.0.0.pre lib/anyway/ext/string_serialize.rb