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.7 lib/cpee/value_helper.rb
cpee-2.0.6 lib/cpee/value_helper.rb
cpee-2.0.5 lib/cpee/value_helper.rb
cpee-2.0.3 lib/cpee/value_helper.rb
cpee-2.0.2 lib/cpee/value_helper.rb
cpee-2.0.1 lib/cpee/value_helper.rb
cpee-1.5.27 lib/cpee/value_helper.rb
cpee-1.5.26 lib/cpee/value_helper.rb
cpee-1.5.25 lib/cpee/value_helper.rb
cpee-2.0 lib/cpee/value_helper.rb
cpee-1.5.24 lib/cpee/value_helper.rb
cpee-1.5.23 lib/cpee/value_helper.rb
cpee-1.5.22 lib/cpee/value_helper.rb
cpee-1.5.21 lib/cpee/value_helper.rb
cpee-1.5.20 lib/cpee/value_helper.rb
cpee-1.5.19 lib/cpee/value_helper.rb
cpee-1.5.18 lib/cpee/value_helper.rb
cpee-1.5.17 lib/cpee/value_helper.rb
cpee-1.5.16 lib/cpee/value_helper.rb
cpee-1.5.15 lib/cpee/value_helper.rb