Class: DataMapper::Property::Key
- Inherits:
-
Object
- Object
- DataMapper::Property::Key
- Includes:
- PassThroughLoadDump
- Defined in:
- lib/dm-appengine/properties/key.rb
Direct Known Subclasses
Constant Summary collapse
- VALID_KEY_OPTS =
Set.new([:id, :name, :parent]).freeze
Class Method Summary collapse
Instance Method Summary collapse
- #dump(value) ⇒ Object
-
#initialize(model, name, options = {}, type = nil) ⇒ Key
constructor
THIS IS A HACK.
- #load(value) ⇒ Object
- #typecast(value) ⇒ Object
- #typecast_to_primitive(value) ⇒ Object
- #valid?(value, negated = false) ⇒ Boolean
Constructor Details
#initialize(model, name, options = {}, type = nil) ⇒ Key
THIS IS A HACK. I’m not sure the best way to do this. The key is not technically required at object creation time, but should be seen as required after that.
63 64 65 66 |
# File 'lib/dm-appengine/properties/key.rb', line 63 def initialize(model, name, = {}, type = nil) super @serial = .fetch(:serial, @key) end |
Class Method Details
.kind(property) ⇒ Object
50 51 52 |
# File 'lib/dm-appengine/properties/key.rb', line 50 def self.kind(property) property.model.repository.adapter.kind(property.model) end |
.typecast_to_primitive(property, value) ⇒ Object
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 |
# File 'lib/dm-appengine/properties/key.rb', line 22 def self.typecast_to_primitive(property, value) case value when ::AppEngine::Datastore::Key, ::NilClass value when ::Integer, ::String AppEngine::Datastore::Key.from_path(kind(property), value) when ::Symbol AppEngine::Datastore::Key.from_path(kind(property), value.to_s) when ::Hash parent = self.typecast_to_primitive(property, value[:parent]) id = value[:id] name = value[:name] if id id_or_name = id.to_i elsif name id_or_name = name.to_s end if parent id_or_name ||= 0 parent.getChild(kind(property), id_or_name) else self.typecast_to_primitive(property, property.typecast(id_or_name)) end else raise ArgumentError, "Unsupported key value #{value.inspect} (a #{value.class})" end end |
Instance Method Details
#dump(value) ⇒ Object
7 8 9 |
# File 'lib/dm-appengine/properties/key.rb', line 7 def dump(value) typecast(value) end |
#load(value) ⇒ Object
10 11 12 |
# File 'lib/dm-appengine/properties/key.rb', line 10 def load(value) value end |
#typecast(value) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/dm-appengine/properties/key.rb', line 14 def typecast(value) if value.nil? value else super end end |
#typecast_to_primitive(value) ⇒ Object
54 55 56 |
# File 'lib/dm-appengine/properties/key.rb', line 54 def typecast_to_primitive(value) self.class.typecast_to_primitive(self, value) end |
#valid?(value, negated = false) ⇒ Boolean
70 71 72 73 74 75 76 77 |
# File 'lib/dm-appengine/properties/key.rb', line 70 def valid?(value, negated = false) if value.kind_of? Hash return false unless value.keys.all?{|k| VALID_KEY_OPTS.include? k} !(value[:id] && value[:name]) else super end end |