Sha256: aa4df8109b50f74213a181310e9dac712fde0449949ccc7a41c190a5c47c42da

Contents?: true

Size: 1.02 KB

Versions: 55

Compression:

Stored size: 1.02 KB

Contents

require 'json'
require 'time'

module CPEE

  class ValueHelper
    def self::generate(value)
      if [String, Integer, Float, TrueClass, FalseClass, Date].include? value.class
        value.to_s
      elsif  [Hash, Array].include? value.class
        JSON::generate(value)
      elsif value.respond_to?(:to_s)
        value.to_s
      end
    end

    def self::parse(value)
      case value.downcase
        when 'true'
          true
        when 'false'
          false
        when 'nil', 'null'
          nil
        else
          begin
            JSON::parse(value)
          rescue
            (Integer value rescue nil) || (Float value rescue nil) || value.to_s rescue nil || ''
          end
      end
    end

    def self::parse_extended(value)
      if [String].include? value.class
        self::parse(value)
      elsif [Array, NilClass, Integer, Float, TrueClass, FalseClass, Date].include? value.class
        value
      elsif value.respond_to?(:to_s)
        value.to_s
      else
        ''
      end
    end

  end

end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
cpee-1.5.13 lib/cpee/value_helper.rb
cpee-1.5.12 lib/cpee/value_helper.rb
cpee-1.5.11 lib/cpee/value_helper.rb
cpee-1.5.10 lib/cpee/value_helper.rb
cpee-1.5.9 lib/cpee/value_helper.rb
cpee-1.5.8 lib/cpee/value_helper.rb
cpee-1.5.7 lib/cpee/value_helper.rb
cpee-1.5.6 lib/cpee/value_helper.rb
cpee-1.5.4 lib/cpee/value_helper.rb
cpee-1.5.3 lib/cpee/value_helper.rb
cpee-1.5.2 lib/cpee/value_helper.rb
cpee-1.5.1.1 lib/cpee/value_helper.rb
cpee-1.5.1 lib/cpee/value_helper.rb
cpee-1.5.0 lib/cpee/value_helper.rb
cpee-1.4.32 lib/cpee/value_helper.rb