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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
# 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
module Record
class HashModel
class << self
# Adds a string attribute to this class.
#
# @example A standard string attribute
#
# class Recipe < AWS::Record::HashModel
# string_attr :name
# end
#
# recipe = Recipe.new(:name => "Buttermilk Pancakes")
# recipe.name #=> 'Buttermilk Pancakes'
#
# @example A string attribute with `:set` set to true
#
# class Recipe < AWS::Record::HashModel
# string_attr :tags, :set => true
# end
#
# recipe = Recipe.new(:tags => %w(popular dessert))
# recipe.tags #=> #<Set: {"popular", "desert"}>
#
# @param [Symbol] name The name of the attribute.
# @param [Hash] options
# @option options [Boolean] :set (false) When true this attribute
# can have multiple values.
def string_attr name, options = {}
add_attribute(Attributes::StringAttr.new(name, options))
end
# Adds an integer attribute to this class.
#
# class Recipe < AWS::Record::HashModel
# integer_attr :servings
# end
#
# recipe = Recipe.new(:servings => '10')
# recipe.servings #=> 10
#
# @param [Symbol] name The name of the attribute.
# @param [Hash] options
# @option options [Boolean] :set (false) When true this attribute
# can have multiple values.
def integer_attr name, options = {}
add_attribute(Attributes::IntegerAttr.new(name, options))
end
# Adds a float attribute to this class.
#
# class Listing < AWS::Record::HashModel
# float_attr :score
# end
#
# listing = Listing.new(:score => '123.456')
# listing.score # => 123.456
#
# @param [Symbol] name The name of the attribute.
# @param [Hash] options
# @option options [Boolean] :set (false) When true this attribute
# can have multiple values.
def float_attr name, options = {}
add_attribute(Attributes::FloatAttr.new(name, options))
end
# Adds a boolean attribute to this class.
#
# @example
#
# class Book < AWS::Record::HashModel
# boolean_attr :read
# end
#
# b = Book.new
# b.read? # => false
# b.read = true
# b.read? # => true
#
# listing = Listing.new(:score => '123.456'
# listing.score # => 123.456
#
# @param [Symbol] name The name of the attribute.
def boolean_attr name, options = {}
attr = add_attribute(Attributes::BooleanAttr.new(name, options))
# add the boolean question mark method
define_method("#{attr.name}?") do
!!__send__(attr.name)
end
end
# Adds a datetime attribute to this class.
#
# @example A standard datetime attribute
#
# class Recipe < AWS::Record::HashModel
# datetime_attr :invented
# end
#
# recipe = Recipe.new(:invented => Time.now)
# recipe.invented #=> <DateTime ...>
#
# If you add a datetime_attr for `:created_at` and/or `:updated_at` those
# will be automanaged.
#
# @param [Symbol] name The name of the attribute.
#
# @param [Hash] options
#
# @option options [Boolean] :set (false) When true this attribute
# can have multiple date times.
#
def datetime_attr name, options = {}
add_attribute(Attributes::DateTimeAttr.new(name, options))
end
# Adds a date attribute to this class.
#
# @example A standard date attribute
#
# class Person < AWS::Record::HashModel
# date_attr :birthdate
# end
#
# baby = Person.new
# baby.birthdate = Time.now
# baby.birthdate #=> <Date: ....>
#
# @param [Symbol] name The name of the attribute.
#
# @param [Hash] options
#
# @option options [Boolean] :set (false) When true this attribute
# can have multiple dates.
#
def date_attr name, options = {}
add_attribute(Attributes::DateAttr.new(name, options))
end
# Adds a DynamoDB binary attribute to this class. A binary
# attribute acts the same as a string attribute, except
#
# @param [Symbol] name The name of the attribute.
#
# @param [Hash] options
#
# @option options [Boolean] :set (false) When true this attribute
# can have multiple values.
#
# @note This should not be used for large objects.
#
def binary_attr name, options = {}
end
# A convenience method for adding the standard two datetime attributes
# `:created_at` and `:updated_at`.
#
# @example
#
# class Recipe < AWS::Record::HashModel
# timestamps
# end
#
# recipe = Recipe.new
# recipe.save
# recipe.created_at #=> <DateTime ...>
# recipe.updated_at #=> <DateTime ...>
#
def timestamps
c = datetime_attr :created_at
u = datetime_attr :updated_at
[c, u]
end
end
end
end
end
|