File: attribute.rb

package info (click to toggle)
ruby-aws-sdk 1.66.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,808 kB
  • ctags: 4,854
  • sloc: ruby: 28,354; makefile: 7
file content (156 lines) | stat: -rw-r--r-- 4,660 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.

module AWS
  class SimpleDB

    # Represents a single named item attribute in SimpleDB.
    class Attribute

      include Core::Model
      include Enumerable
      include ConsistentReadOption
      include PutAttributes
      include DeleteAttributes

      # @api private
      def initialize item, name, options = {}
        @item = item
        @name = name
        super
      end

      # @return [Item] The item this attribute belongs to.
      attr_reader :item

      # @return [String] The name of this attribute.
      attr_reader :name

      # Sets all values for this attribute, replacing current values.
      #
      # @example Setting a list of values
      #
      #   attributes['colors'].set 'red', 'blue', 'green'
      #
      # @example Setting an array of values
      #
      #   attributes['colors'].set ['red', 'blue']
      #
      # @param [String] values A list of attribute values to set.
      # @return [nil]
      def set *values
        put(values, true)
        nil
      end

      # Appends values to this attribute.  Duplicate values are ignored
      # by SimpleDB.
      #
      # @example Adding a list of values
      #
      #   attributes['colors'].add 'red', 'blue', 'green'
      #
      # @example Adding an array of values
      #
      #   attributes['colors'].add ['red', 'blue']
      #
      # @param [String] values A list of attribute values to add.
      # @return [nil]
      def add *values
        put(values, false)
        nil
      end
      alias_method :<<, :add

      # Deletes this attribute or specific values from this attribute.
      #
      # @example Delete the attribute and all of its values
      #
      #   item.attributes['color'].delete
      #
      # @example Delete specific attribute values
      #
      #   item.attributes['color'].delete('red', 'blue')
      #
      # @param values One ore more values to remove from this attribute.
      #   If values is empty, then all attribute values are deleted
      #   (which deletes this attribute).
      # @return [nil]
      def delete *values
        expect_opts = values.pop if values.last.kind_of?(Hash)

        if values.empty?
          delete_named_attributes(name, expect_opts || {})
        else
          delete_attribute_values(Hash[[[name, values]]].
                                  merge(expect_opts || {}))
        end
        nil
      end

      # Yields once for each value on this attribute.
      #
      # @yield [attribute_value] Yields once for each domain in the account.
      # @yieldparam [String] attribute_value
      # @param [Hash] options
      # @option options [Boolean] :consistent_read (false) A consistent read
      #   returns values that reflects all writes that received a successful
      #   response prior to the read.
      # @return [nil]
      def each options = {}, &block

        resp = client.get_attributes(
          :domain_name => item.domain.name,
          :item_name => item.name,
          :attribute_names => [name],
          :consistent_read => consistent_read(options))

        resp.attributes.each do |attribute|
          yield(attribute.value)
        end

        nil

      end

      # Returns all values for this attribute as an array of strings.
      #
      # @example
      #   item.attributes['ratings'].values
      #   #=> ['5', '3', '4']
      #
      # @param [Hash] options
      # @option options [Boolean] :consistent_read (false) A consistent read
      #   returns values that reflects all writes that received a successful
      #   response prior to the read.
      # @return [Array<String>] An array of attribute values
      def values options = {}
        values = []
        self.each(options) do |value|
          values << value
        end
        values
      end

      # @api private
      protected
      def put values, replace
        expect_opts = values.pop if values.last.kind_of?(Hash)
        do_put(attribute_hashes(Hash[[[name, values]]],
                                replace),
               expect_opts || {})
      end

    end
  end
end