Module: IseshimaStore::Base
- Defined in:
- lib/iseshima_store/base.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #assign_entities(hash) ⇒ Object
- #destroy ⇒ Object
- #key ⇒ Object
- #parent ⇒ Object
- #parent=(model) ⇒ Object
- #save! ⇒ Object
- #to_entity ⇒ Object
Class Method Details
.included(klass) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/iseshima_store/base.rb', line 11 def self.included(klass) klass.extend SingleForwardable klass.extend ClassMethods klass.def_delegators :scoping, :where, :all, :first, :last, :to_a, :parent, :find, :find_by klass.instance_eval do attr_accessor :id, :created_at, :description, :parent_key end end |
Instance Method Details
#assign_entities(hash) ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/iseshima_store/base.rb', line 96 def assign_entities(hash) hash.each { |k, v| k = k.to_s if respond_to? "#{k}=" send("#{k}=", v) end } self end |
#destroy ⇒ Object
92 93 94 |
# File 'lib/iseshima_store/base.rb', line 92 def destroy IseshimaStore::Connection.current.delete(key) end |
#key ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/iseshima_store/base.rb', line 145 def key path = [] obj = self path << [obj.class.to_s, obj.id] while obj.parent obj = obj.parent path << [obj.class.to_s, obj.id] end IseshimaStore::Connection.current.key(path.reverse) end |
#parent ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/iseshima_store/base.rb', line 134 def parent return @parent if @parent if @parent_key klass = Object.const_get(@parent_key.kind) if klass.include?(IseshimaStore::Base) @parent = klass.find(@parent_key.id) end end end |
#parent=(model) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/iseshima_store/base.rb', line 125 def parent=(model) if model key = model.to_entity.key self.parent_key = key else self.parent_key = '' end end |
#save! ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/iseshima_store/base.rb', line 80 def save! entity = to_entity if @parent_key == '' entity.key.parent = nil else entity.key.parent = @parent_key end IseshimaStore::Connection.current.save(entity) self.id = entity.key.id self end |
#to_entity ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/iseshima_store/base.rb', line 106 def to_entity entity = Gcloud::Datastore::Entity.new entity.key = Gcloud::Datastore::Key.new(self.class.to_s, id) unless self.class.properties logger = Logger.new(STDOUT) logger.warn "You may have to define attr_properties in your model" end self.class.properties.each do |property| property = property.to_s value = send(property) if value.is_a?(String) value = value.force_encoding('UTF-8') end entity[property] = value end entity end |