Module: AWS::S3::ACLObject::ClassMethods

Defined in:
lib/aws/s3/acl_object.rb

Instance Method Summary collapse

Instance Method Details

#add_xml_child(method_name) ⇒ Object



247
248
249
250
251
252
253
254
# File 'lib/aws/s3/acl_object.rb', line 247

def add_xml_child(method_name)
  Core::MetaUtils.class_extend_method(self, :body_xml) do
    xml = super()
    value = send(method_name)
    xml += yield(value) if value
    xml
  end
end

#body_xml_content(method_name) ⇒ Object



232
233
234
# File 'lib/aws/s3/acl_object.rb', line 232

def body_xml_content(method_name)
  add_xml_child(method_name) { |value| value.to_s }
end

#body_xml_list_content(list_element, method_name) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'lib/aws/s3/acl_object.rb', line 236

def body_xml_list_content(list_element, method_name)
  add_xml_child(method_name) do |list|
    list_content = list.map { |member| member.to_s }.join
    if list_content.empty?
      "<#{list_element}/>"
    else
      "<#{list_element}>#{list_content}</#{list_element}>"
    end
  end
end

#body_xml_string_content(element_name, method_name) ⇒ Object



225
226
227
228
229
230
# File 'lib/aws/s3/acl_object.rb', line 225

def body_xml_string_content(element_name, method_name)
  add_xml_child(method_name) do |value|
    normalized = REXML::Text.normalize(value.to_s)
    "<#{element_name}>#{normalized}</#{element_name}>"
  end
end

#input_validator(method_name, &blk) ⇒ Object



167
168
169
170
171
172
173
174
175
176
# File 'lib/aws/s3/acl_object.rb', line 167

def input_validator(method_name, &blk)
  validator = "__validator__#{blk.object_id}"
  Core::MetaUtils.class_extend_method(self, validator, &blk)
  Core::MetaUtils.class_extend_method(self, "validate_#{method_name}_input!") do |*args|
    (value, context) = args
    context = " "+context if context
    context ||= ""
    send(validator, value, context)
  end
end

#object_attr(klass, options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aws/s3/acl_object.rb', line 91

def object_attr(klass, options = {})
  base_name = klass.name[/::([^:]*)$/, 1]
  method_name = Core::Inflection.ruby_name(base_name)
  cast = options[:cast] || Hash

  attr_reader(method_name)
  setter_option(method_name)
  object_setter(klass, method_name, cast)
  object_input_validator(klass, base_name, method_name, cast)
  validate_object(method_name) if options[:required]
  body_xml_content(method_name)
end

#object_input_validator(klass, base_name, method_name, cast) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/aws/s3/acl_object.rb', line 140

def object_input_validator(klass, base_name, method_name, cast)
  input_validator(method_name) do |value, context|
    if value.kind_of?(cast)
      klass.new(value).validate!
    else
      raise ArgumentError.new("expected #{base_name} object or hash"+context) unless
        value.nil? or value.kind_of? klass
    end
  end
end

#object_list_attr(list_element, klass, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aws/s3/acl_object.rb', line 104

def object_list_attr(list_element, klass, options = {})
  base_name = klass.name[/::([^:]*)$/, 1]
  method_name = Core::Inflection.ruby_name(options[:method_name].to_s || list_element)
  default_value = nil
  default_value = [] if options[:required]

  attr_reader(method_name)
  setter_option(method_name) { [] if options[:required] }
  object_list_setter(klass, method_name)
  object_list_input_validator(klass, base_name, method_name)
  validate_list(method_name)
  body_xml_list_content(list_element, method_name)
end

#object_list_input_validator(klass, base_name, method_name) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/aws/s3/acl_object.rb', line 151

def object_list_input_validator(klass, base_name, method_name)
  input_validator(method_name) do |value, context|
    raise ArgumentError.new("expected array"+context) unless value.kind_of?(Array)
    value.each do |member|
      if member.kind_of?(Hash)
        klass.new(member).validate!
      else
        raise ArgumentError.new("expected array#{context} "+
                                "to contain #{base_name} objects "+
                                "or hashes") unless
          member.kind_of? klass
      end
    end
  end
end

#object_list_setter(klass, method_name) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/aws/s3/acl_object.rb', line 188

def object_list_setter(klass, method_name)
  define_method("#{method_name}=") do |value|
    validate_input(method_name, value)
    list = value.map do |member|
      if member.kind_of?(Hash)
        klass.new(member)
      else
        member
      end
    end
    instance_variable_set("@#{method_name}", list)
  end
end

#object_setter(klass, method_name, cast) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/aws/s3/acl_object.rb', line 178

def object_setter(klass, method_name, cast)
  define_method("#{method_name}=") do |value|
    validate_input(method_name, value)
    if value.kind_of?(cast)
      value = klass.new(value)
    end
    instance_variable_set("@#{method_name}", value)
  end
end

#setter_option(method_name) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/aws/s3/acl_object.rb', line 118

def setter_option(method_name)
  Core::MetaUtils.class_extend_method(self, :initialize) do |*args|
    opts = args.last || {}
    instance_variable_set("@#{method_name}", yield) if block_given?
    key = method_name.to_sym

    if opts.has_key?(key)
      value = opts[key]
      validate_input(method_name, value, "for #{method_name} option")
      self.send("#{method_name}=", value)
    end
    super(opts)
  end
end

#string_attr(element_name, options = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/aws/s3/acl_object.rb', line 80

def string_attr(element_name, options = {})
  method_name = options[:method_name] ||
    Core::Inflection.ruby_name(element_name)

  attr_accessor(method_name)
  setter_option(method_name)
  string_input_validator(method_name)
  validate_string(method_name) if options[:required]
  body_xml_string_content(element_name, method_name)
end

#string_input_validator(method_name) ⇒ Object



133
134
135
136
137
138
# File 'lib/aws/s3/acl_object.rb', line 133

def string_input_validator(method_name)
  input_validator(method_name) do |value, context|
    raise ArgumentError.new("expected string"+context) unless
      value.respond_to?(:to_str)
  end
end

#validate_list(method_name) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/aws/s3/acl_object.rb', line 217

def validate_list(method_name)
  Core::MetaUtils.class_extend_method(self, :validate!) do
    super()
    raise "missing #{method_name}" unless send(method_name)
    send(method_name).each { |member| member.validate! }
  end
end

#validate_object(method_name) ⇒ Object



209
210
211
212
213
214
215
# File 'lib/aws/s3/acl_object.rb', line 209

def validate_object(method_name)
  Core::MetaUtils.class_extend_method(self, :validate!) do
    super()
    raise "missing #{method_name}" unless send(method_name)
    send(method_name).validate!
  end
end

#validate_string(method_name) ⇒ Object



202
203
204
205
206
207
# File 'lib/aws/s3/acl_object.rb', line 202

def validate_string(method_name)
  Core::MetaUtils.class_extend_method(self, :validate!) do
    super()
    raise "missing #{method_name}" unless send(method_name)
  end
end