Class: DmAdapterSimpledb::Record
- Inherits:
-
Object
- Object
- DmAdapterSimpledb::Record
- Includes:
- Utils
- Defined in:
- lib/dm-adapter-simpledb/record.rb
Direct Known Subclasses
Constant Summary collapse
- METADATA_KEY =
"__dm_metadata"
- STORAGE_NAME_KEY =
"simpledb_type"
- META_KEYS =
[METADATA_KEY, STORAGE_NAME_KEY]
- CURRENT_VERSION =
"01.01.00"
Instance Attribute Summary collapse
-
#deletable_attributes ⇒ Object
readonly
Returns the value of attribute deletable_attributes.
-
#item_name ⇒ Object
readonly
Returns the value of attribute item_name.
-
#simpledb_attributes ⇒ Object
(also: #writable_attributes)
readonly
Returns the value of attribute simpledb_attributes.
Class Method Summary collapse
- .data_version(simpledb_attributes) ⇒ Object
- .from_resource(resource) ⇒ Object
- .from_simpledb_hash(hash) ⇒ Object
- .register(klass, version) ⇒ Object
- .simpledb_attributes(hash) ⇒ Object
- .version(version = nil) ⇒ Object
- .versions ⇒ Object
Instance Method Summary collapse
- #[](attribute, type) ⇒ Object
- #add_metadata_to!(hash, table_name) ⇒ Object
- #coerce_to(values, type_or_property) ⇒ Object
- #coerce_to_property(value, property) ⇒ Object
- #coerce_to_type(values, type) ⇒ Object
-
#initialize(hash_or_resource) ⇒ Record
constructor
A new instance of Record.
- #metadata ⇒ Object
-
#migrate ⇒ Object
Returns a record of the current version.
-
#storage_name ⇒ Object
Deprecated - we are moving the type information under the metadata key.
-
#table ⇒ Object
Returns the “Table” this record belongs to.
- #table_name_from_model(repository = DataMapper.repository.name) ⇒ Object
-
#to_resource_hash(fields) ⇒ Object
Convert to a Hash suitable for initializing a Resource.
- #version ⇒ Object
- #version_token ⇒ Object
Methods included from Utils
Constructor Details
#initialize(hash_or_resource) ⇒ Record
Returns a new instance of Record.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dm-adapter-simpledb/record.rb', line 64 def initialize(hash_or_resource) case hash_or_resource when DataMapper::Resource then attrs_to_update, attrs_to_delete = extract_attributes(hash_or_resource) @simpledb_attributes = attrs_to_update @deletable_attributes = attrs_to_delete @item_name = item_name_for_resource(hash_or_resource) @model = hash_or_resource.model when Hash hash = hash_or_resource @item_name = hash.keys.first @simpledb_attributes = hash.values.first @deletable_attributes = [] else raise "Don't know how to initialize from #{hash_or_resource.inspect}" end end |
Instance Attribute Details
#deletable_attributes ⇒ Object
Returns the value of attribute deletable_attributes.
60 61 62 |
# File 'lib/dm-adapter-simpledb/record.rb', line 60 def deletable_attributes @deletable_attributes end |
#item_name ⇒ Object
Returns the value of attribute item_name.
61 62 63 |
# File 'lib/dm-adapter-simpledb/record.rb', line 61 def item_name @item_name end |
#simpledb_attributes ⇒ Object Also known as: writable_attributes
Returns the value of attribute simpledb_attributes.
59 60 61 |
# File 'lib/dm-adapter-simpledb/record.rb', line 59 def simpledb_attributes @simpledb_attributes end |
Class Method Details
.data_version(simpledb_attributes) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/dm-adapter-simpledb/record.rb', line 47 def self.data_version(simpledb_attributes) simpledb_attributes.fetch(METADATA_KEY){[]}.grep(/v\d\d\.\d\d\.\d\d/) do |version_stamp| return version_stamp[1..-1] end return "00.00.00" end |
.from_resource(resource) ⇒ Object
26 27 28 |
# File 'lib/dm-adapter-simpledb/record.rb', line 26 def self.from_resource(resource) versions.fetch(CURRENT_VERSION).new(resource) end |
.from_simpledb_hash(hash) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/dm-adapter-simpledb/record.rb', line 19 def self.from_simpledb_hash(hash) data_version = data_version(simpledb_attributes(hash)) versions.fetch(data_version) do raise "Unknown data version for: #{hash.inspect}" end.new(hash) end |
.register(klass, version) ⇒ Object
30 31 32 |
# File 'lib/dm-adapter-simpledb/record.rb', line 30 def self.register(klass, version) versions[version] = klass end |
.simpledb_attributes(hash) ⇒ Object
55 56 57 |
# File 'lib/dm-adapter-simpledb/record.rb', line 55 def self.simpledb_attributes(hash) hash.values.first end |
.version(version = nil) ⇒ Object
38 39 40 41 42 43 44 45 |
# File 'lib/dm-adapter-simpledb/record.rb', line 38 def self.version(version=nil) if version Record.register(self, version) @version = version else @version end end |
.versions ⇒ Object
34 35 36 |
# File 'lib/dm-adapter-simpledb/record.rb', line 34 def self.versions @versions ||= {} end |
Instance Method Details
#[](attribute, type) ⇒ Object
98 99 100 101 |
# File 'lib/dm-adapter-simpledb/record.rb', line 98 def [](attribute, type) values = Array(simpledb_attributes[attribute]) coerce_to(values, type) end |
#add_metadata_to!(hash, table_name) ⇒ Object
183 184 185 186 187 188 |
# File 'lib/dm-adapter-simpledb/record.rb', line 183 def (hash, table_name) hash.merge!({ STORAGE_NAME_KEY => [table_name], METADATA_KEY => [version_token, Table.token_for(table_name)] }) end |
#coerce_to(values, type_or_property) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/dm-adapter-simpledb/record.rb', line 103 def coerce_to(values, type_or_property) case type_or_property when DataMapper::Property coerce_to_property(values, type_or_property) when Class coerce_to_type(values, type_or_property) else raise "Should never get here" end end |
#coerce_to_property(value, property) ⇒ Object
113 114 115 |
# File 'lib/dm-adapter-simpledb/record.rb', line 113 def coerce_to_property(value, property) property.typecast(coerce_to_type(value, property.type)) end |
#coerce_to_type(values, type) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dm-adapter-simpledb/record.rb', line 117 def coerce_to_type(values, type) case when type <= String case values.size when 0 nil when 1 values.first else ChunkedString.new(values) end when type <= Array, type <= DataMapper::Types::SdbArray values else values.first end end |
#metadata ⇒ Object
156 157 158 |
# File 'lib/dm-adapter-simpledb/record.rb', line 156 def simpledb_attributes[METADATA_KEY] end |
#migrate ⇒ Object
Returns a record of the current version
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/dm-adapter-simpledb/record.rb', line 161 def migrate new_record = Record.versions[CURRENT_VERSION].allocate new_record.item_name = item_name data = transform_hash(simpledb_attributes) { |hash, key, values| hash[key] = coerce_heuristically(values) } updates = {} deletes = [] data.each_pair do |key, values| if Array(values).empty? deletes << key else updates[key] = values end end new_record.(updates, table) new_record.simpledb_attributes = updates new_record.deletable_attributes = deletes new_record end |
#storage_name ⇒ Object
Deprecated - we are moving the type information under the metadata key
94 95 96 |
# File 'lib/dm-adapter-simpledb/record.rb', line 94 def storage_name simpledb_attributes[STORAGE_NAME_KEY].first end |
#table ⇒ Object
Returns the “Table” this record belongs to. SimpleDB has no concept of tables, but we fake it with metadata.
145 146 147 148 |
# File 'lib/dm-adapter-simpledb/record.rb', line 145 def table table_name_from_model || Table.() || storage_name end |
#table_name_from_model(repository = DataMapper.repository.name) ⇒ Object
150 151 152 153 154 |
# File 'lib/dm-adapter-simpledb/record.rb', line 150 def table_name_from_model(repository=DataMapper.repository.name) if defined?(@model) && @model @model.storage_name(repository) end end |
#to_resource_hash(fields) ⇒ Object
Convert to a Hash suitable for initializing a Resource
86 87 88 89 90 91 |
# File 'lib/dm-adapter-simpledb/record.rb', line 86 def to_resource_hash(fields) result = transform_hash(fields) {|hash, property| hash[property.name.to_s] = self[property.field, property] } result end |
#version ⇒ Object
135 136 137 |
# File 'lib/dm-adapter-simpledb/record.rb', line 135 def version self.class.version || self.class.data_version(simpledb_attributes) end |
#version_token ⇒ Object
139 140 141 |
# File 'lib/dm-adapter-simpledb/record.rb', line 139 def version_token "v#{version}" end |