Class: AWS::Record::Attributes::StringAttr

Inherits:
BaseAttr
  • Object
show all
Defined in:
lib/aws/record/attributes.rb

Instance Attribute Summary

Attributes inherited from BaseAttr

#name, #options

Class Method Summary collapse

Methods inherited from BaseAttr

#default_value, deserialize, #deserialize, #initialize, #persist_as, #serialize, #set?, #type_cast

Constructor Details

This class inherits a constructor from AWS::Record::Attributes::BaseAttr

Class Method Details

.allow_set?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/aws/record/attributes.rb', line 147

def self.allow_set?
  true
end

.serialize(string, options = {}) ⇒ String

Returns a serialized representation of the string value suitable for storing in SimpleDB.

Parameters:

  • string (String)
  • options (Hash) (defaults to: {})

Returns:

  • (String)

    The serialized string.



138
139
140
141
142
143
144
# File 'lib/aws/record/attributes.rb', line 138

def self.serialize string, options = {}
  unless string.is_a?(String)
    msg = "expected a String value, got #{string.class}"
    raise ArgumentError, msg  
  end
  string
end

.type_cast(raw_value, options = {}) ⇒ String?

Returns the value cast to a string. Empty strings are returned as nil by default. Type casting is done by calling #to_s on the value.

string_attr.type_cast(123)
# => '123'

string_attr.type_cast('')
# => nil

string_attr.type_cast('', :preserve_empty_strings => true)
# => ''

Parameters:

  • raw_value (Mixed)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :preserve_empty_strings (Boolean) — default: false

    When true, empty strings are preserved and not cast to nil.

Returns:

  • (String, nil)

    The type casted value.



124
125
126
127
128
129
130
131
# File 'lib/aws/record/attributes.rb', line 124

def self.type_cast raw_value, options = {}
  case raw_value
  when nil     then nil
  when ''      then options[:preserve_empty_strings] ? '' : nil
  when String  then raw_value
  else raw_value.to_s
  end
end