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-2.0.30 lib/cpee/value_helper.rb
cpee-2.0.28 lib/cpee/value_helper.rb
cpee-2.0.27 lib/cpee/value_helper.rb
cpee-2.0.26 lib/cpee/value_helper.rb
cpee-2.0.25 lib/cpee/value_helper.rb
cpee-2.0.24 lib/cpee/value_helper.rb
cpee-2.0.23 lib/cpee/value_helper.rb
cpee-2.0.22 lib/cpee/value_helper.rb
cpee-2.0.21 lib/cpee/value_helper.rb
cpee-2.0.20 lib/cpee/value_helper.rb
cpee-2.0.19 lib/cpee/value_helper.rb
cpee-2.0.18 lib/cpee/value_helper.rb
cpee-2.0.17 lib/cpee/value_helper.rb
cpee-2.0.16 lib/cpee/value_helper.rb
cpee-2.0.15 lib/cpee/value_helper.rb
cpee-2.0.14 lib/cpee/value_helper.rb
cpee-2.0.13 lib/cpee/value_helper.rb
cpee-2.0.11 lib/cpee/value_helper.rb
cpee-2.0.9 lib/cpee/value_helper.rb
cpee-2.0.8 lib/cpee/value_helper.rb