Class: AWSCloudSearch::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_cloud_search/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_version = false) ⇒ Document

Initializes the object

Parameters:

  • auto_version (boolean) (defaults to: false)

    Set to true to automatically set the version, default is false



31
32
33
34
# File 'lib/aws_cloud_search/document.rb', line 31

def initialize(auto_version=false)
  @fields = {}
  new_version if auto_version
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



27
28
29
# File 'lib/aws_cloud_search/document.rb', line 27

def fields
  @fields
end

#idObject

Returns the value of attribute id.



27
28
29
# File 'lib/aws_cloud_search/document.rb', line 27

def id
  @id
end

#typeObject

Returns the value of attribute type.



26
27
28
# File 'lib/aws_cloud_search/document.rb', line 26

def type
  @type
end

Class Method Details

.type_attr_accessor(name, type) ⇒ Object

A typed attribute accessor helper. When the value is set, if it does not match the pre-defined type, an exception is thrown.

Parameters:

  • name (String)

    Name of the attribute

  • type (Class)

    The class type of the attribute



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/aws_cloud_search/document.rb', line 10

def self.type_attr_accessor(name, type)
  define_method(name) do
    instance_variable_get("@#{name}")
  end

  define_method("#{name}=") do |value|
    if value.is_a? type or value == nil
      instance_variable_set("@#{name}", value)
    else
      raise ArgumentError.new("Invalid Type")
    end
  end
end

Instance Method Details

#add_field(name, value) ⇒ Object

Adds a new field to the document

Parameters:

  • name (String)

    Name of the document field

  • value (String, Integer)

    Value of the document field

Raises:

  • (ArgumentError)


39
40
41
42
# File 'lib/aws_cloud_search/document.rb', line 39

def add_field(name, value)
  raise ArgumentError.new("Found invalid XML 1.0 unicode character(s)") if value.is_a? String and value =~ INVALID_CHAR_XML10
  @fields[name] = value
end

#clear_fieldsObject

Resets the fields.



52
53
54
# File 'lib/aws_cloud_search/document.rb', line 52

def clear_fields
  @fields = {}
end

#new_versionObject

Set a new version automatically



57
58
59
# File 'lib/aws_cloud_search/document.rb', line 57

def new_version
  @version = Time.now.to_i
end

#to_hashObject

Return this object as a hash



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/aws_cloud_search/document.rb', line 62

def to_hash
  @fields.delete_if {|key,val| val.nil?}
  h = {
      :type => @type,
      :id => @id,
      :version => @version,
      :fields => @fields
  }
  h[:lang] = @lang unless (@type == 'delete')

  h      
end

#to_jsonObject

Return this object as json



76
77
78
# File 'lib/aws_cloud_search/document.rb', line 76

def to_json
  to_hash.to_json
end