Sha256: 77436f1346dfa770bc47ad94870f7dc76901eed27542ae447c263ace238dfd07

Contents?: true

Size: 1.56 KB

Versions: 5

Compression:

Stored size: 1.56 KB

Contents

module OData4
  module Properties
    # Defines the String OData4 type.
    class String < OData4::Property
      # Returns the property value, properly typecast
      # @return [String,nil]
      def value
        if (@value.nil? || @value.empty?) && allows_nil?
          nil
        else
          encode_value(@value)
        end
      end

      # Sets the property value
      # @params new_value [to_s,nil]
      def value=(new_value)
        validate(new_value)
        @value = new_value.nil? ? nil : encode_value(new_value.to_s)
      end

      # Value to be used in URLs.
      # @return [String]
      def url_value
        "'#{value}'"
      end

      # The OData4 type name
      def type
        'Edm.String'
      end

      # Is the property value Unicode encoded
      def is_unicode?
        options[:unicode]
      end

      # Does the property have a default value
      def has_default_value?
        not(options[:default_value].nil?)
      end

      # The default value for the property
      def default_value
        options[:default_value]
      end

      private

      def default_options
        super.merge({
          unicode: true,
          default_value: nil
        })
      end

      def validate(new_value)
        if new_value.nil? && !allows_nil?
          validation_error 'This property does not allow for nil values to be set'
        end
      end

      def encode_value(new_value)
        if options[:unicode]
          new_value.encode(Encoding::UTF_8)
        else
          new_value.encode(Encoding::ASCII)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
odata4-0.9.1 lib/odata4/properties/string.rb
odata4-0.9.0 lib/odata4/properties/string.rb
odata4-0.8.2 lib/odata4/properties/string.rb
odata4-0.8.1 lib/odata4/properties/string.rb
odata4-0.8.0 lib/odata4/properties/string.rb