Class: TinyDS::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_ds/base.rb,
lib/tiny_ds/validations.rb

Constant Summary collapse

RESERVED_PROPERTY_NAME =
[:id, :name, :key, :entity, :parent_key, :parent]
VALID_PROPERTY_TYPE =
[:string, :integer, :text, :time, :list]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, opts = {}) ⇒ Base

Foo.new



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/tiny_ds/base.rb', line 106

def initialize(attrs={}, opts={})
  @entity = opts.delete(:entity)
  unless @entity
    if opts[:parent] && opts[:parent].kind_of?(Base)
      opts = opts.dup
      opts[:parent] = opts[:parent].entity
    end
    @entity = LowDS.build(self.class.kind, {}, opts)
    self.attributes = self.class.default_attrs.merge(attrs || {})
    @new_record = true
  else
    @new_record = false
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/tiny_ds/base.rb', line 271

def method_missing(m, *args)
  k, is_set = if m.to_s =~ /(.+)=$/
                [$1.to_sym, true]
              else
                [m.to_sym, false]
              end
  if prop_def = self.class.property_definitions[k]
    # TODO define method.
    if is_set
      raise if args.size!=1
      set_property(k, args.first)
    else
      raise if args.size!=0
      get_property(k)
    end
  else
    super(m, *args)
  end
end

Class Attribute Details

._property_definitionsObject

Returns the value of attribute _property_definitions.



26
27
28
# File 'lib/tiny_ds/base.rb', line 26

def _property_definitions
  @_property_definitions
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



124
125
126
# File 'lib/tiny_ds/base.rb', line 124

def entity
  @entity
end

Class Method Details

._get_by_id_or_name!(id_or_name, parent) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/tiny_ds/base.rb', line 173

def self._get_by_id_or_name!(id_or_name, parent)
  key = if parent
          parent = to_key(parent)
          kfb = LowDS::KeyFactory::Builder.new(parent)
          kfb.addChild(kind, id_or_name)
          kfb.key
        else
          LowDS::KeyFactory::Builder.new(kind, id_or_name).key
        end
  get!(key)
end

.countObject



224
225
226
# File 'lib/tiny_ds/base.rb', line 224

def self.count
  query.count
end

.create(attrs = {}, opts = {}) ⇒ Object

Foo.create(:title=>“hello”,…, :parent=>aaa, :id=>bbb, :name=>ccc, :key=>…)



99
100
101
102
103
# File 'lib/tiny_ds/base.rb', line 99

def self.create(attrs={}, opts={})
  m = new(attrs, opts)
  m.save
  m
end

.create!(attrs = {}, opts = {}) ⇒ Object



6
7
8
9
10
# File 'lib/tiny_ds/validations.rb', line 6

def self.create!(attrs={}, opts={})
  m = new(attrs, opts)
  m.save!
  m
end

.default_attrsObject



45
46
47
48
49
50
51
52
53
# File 'lib/tiny_ds/base.rb', line 45

def self.default_attrs
  attrs = {}
  property_definitions.each do |pname,pdef|
    if pdef.has_default?
      attrs[pname] = pdef.default_value
    end
  end
  attrs
end

.destroy(array) ⇒ Object

Foo.destroy([model, entity, key, …])



234
235
236
237
238
239
240
# File 'lib/tiny_ds/base.rb', line 234

def self.destroy(array)
  array = [array] unless array.kind_of?(Array)
  keys = array.collect do |m|
    to_key(m)
  end
  AppEngine::Datastore.delete(keys)
end

.destroy_allObject



241
242
243
# File 'lib/tiny_ds/base.rb', line 241

def self.destroy_all
  destroy(query.keys)
end

.get(key) ⇒ Object



167
168
169
170
171
# File 'lib/tiny_ds/base.rb', line 167

def self.get(key)
  get!(key)
rescue AppEngine::Datastore::EntityNotFound => e
  nil
end

.get!(key) ⇒ Object

Foo.get(key)



164
165
166
# File 'lib/tiny_ds/base.rb', line 164

def self.get!(key)
  self.new_from_entity(LowDS.get(key, :kind=>self.kind))
end

.get_by_id(id, parent = nil) ⇒ Object



196
197
198
199
200
201
202
203
# File 'lib/tiny_ds/base.rb', line 196

def self.get_by_id(id, parent=nil)
  if id.kind_of?(String) && id==id.to_i.to_s
    id = id.to_i
  end
  _get_by_id_or_name!(id, parent)
rescue AppEngine::Datastore::EntityNotFound => e
  nil
end

.get_by_id!(id, parent = nil) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/tiny_ds/base.rb', line 185

def self.get_by_id!(id, parent=nil)
  if id.kind_of?(String) && id==id.to_i.to_s
    id = id.to_i
  end
  raise "id is not Integer" unless id.kind_of?(Integer)
  _get_by_id_or_name!(id, parent)
end

.get_by_name(name, parent = nil) ⇒ Object



204
205
206
207
208
# File 'lib/tiny_ds/base.rb', line 204

def self.get_by_name(name, parent=nil)
  _get_by_id_or_name!(name, parent)
rescue AppEngine::Datastore::EntityNotFound => e
  nil
end

.get_by_name!(name, parent = nil) ⇒ Object



192
193
194
195
# File 'lib/tiny_ds/base.rb', line 192

def self.get_by_name!(name, parent=nil)
  raise "id is not String" unless name.kind_of?(String)
  _get_by_id_or_name!(name, parent)
end

.kindObject

kind-string of entity



56
57
58
# File 'lib/tiny_ds/base.rb', line 56

def self.kind
  name
end

.new_from_entity(_entity) ⇒ Object



121
122
123
# File 'lib/tiny_ds/base.rb', line 121

def self.new_from_entity(_entity)
  new(nil, :entity=>_entity)
end

.property(pname, ptype, opts = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/tiny_ds/base.rb', line 29

def self.property(pname, ptype, opts={})
  pname = pname.to_sym
  if RESERVED_PROPERTY_NAME.include?(pname)
    raise "property name '#{pname}' is reserved."
  end
  property_definitions[pname] = PropertyDefinition.new(pname, ptype, opts)
end

.property_definition(name) ⇒ Object



41
42
43
# File 'lib/tiny_ds/base.rb', line 41

def self.property_definition(name)
  property_definitions[name.to_sym] or raise "unknown property='#{name}'"
end

.property_definitionsObject



37
38
39
# File 'lib/tiny_ds/base.rb', line 37

def self.property_definitions
  self._property_definitions ||= {}
end

.queryObject

# Foo.find

def self.find(*args)
  raise "todo"
  direction = dire==:desc ? AppEngine::Datastore::Query::DESCENDING : AppEngine::Datastore::Query::ASCENDING
  AppEngine::Datastore::Query.new("TxSrc").
    filter(:tx_kind, AppEngine::Datastore::Query::EQUAL, tx_kind).
    filter(:status,  AppEngine::Datastore::Query::EQUAL, status).
    sort(:created_at, direction)
end


220
221
222
# File 'lib/tiny_ds/base.rb', line 220

def self.query
  Query.new(self)
end

.to_key(m) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tiny_ds/base.rb', line 81

def self.to_key(m)
  case m
  when AppEngine::Datastore::Key
    m
  when AppEngine::Datastore::Entity
    m.key
  when String
    LowDS::KeyFactory.stringToKey(m)
  else
    if m.kind_of?(Base)
      m.key
    else
      raise "unknown key type #{m.class}/#{m.inspect}"
    end
  end
end

Instance Method Details

#__before_save_set_timestampsObject

end



154
155
156
157
158
159
160
161
# File 'lib/tiny_ds/base.rb', line 154

def __before_save_set_timestamps
  if self.class.property_definitions[:created_at] && new_record?
    self.created_at = Time.now
  end
  if self.class.property_definitions[:updated_at]
    self.updated_at = Time.now
  end
end

#attributes=(attrs) ⇒ Object

set attributes



246
247
248
249
250
251
# File 'lib/tiny_ds/base.rb', line 246

def attributes=(attrs)
  attrs.each do |k,v|
    set_property(k, v)
  end
  nil
end

#destroyObject

foo.destroy



229
230
231
# File 'lib/tiny_ds/base.rb', line 229

def destroy
  self.class.destroy(self)
end

#do_saveObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tiny_ds/base.rb', line 136

def do_save
  __before_save_set_timestamps
#    if @new_record && @entity.key && parent
#      TinyDS.tx{
#        if LowDS.get(@entity.key)
#          raise KeyIsAlreadyTaken
#        end
#        LowDS.save(@entity)
#      }
#    else
    LowDS.save(@entity)
#    end
  @new_record = false
  nil
end

#get_property(k) ⇒ Object

get property-value from @entity



266
267
268
269
# File 'lib/tiny_ds/base.rb', line 266

def get_property(k)
  prop_def = self.class.property_definition(k)
  prop_def.to_ruby_value(@entity[k])
end

#idObject



69
70
71
# File 'lib/tiny_ds/base.rb', line 69

def id
  key.id
end

#keyObject

foo.key



66
67
68
# File 'lib/tiny_ds/base.rb', line 66

def key
  @entity.key
end

#nameObject



72
73
74
# File 'lib/tiny_ds/base.rb', line 72

def name
  key.name
end

#new_record?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/tiny_ds/base.rb', line 126

def new_record?
  @new_record
end

#parent_keyObject

foo.parent_key



77
78
79
# File 'lib/tiny_ds/base.rb', line 77

def parent_key
  @entity.parent
end

#saveObject

foo.save



131
132
133
134
# File 'lib/tiny_ds/base.rb', line 131

def save
  do_save
  true
end

#save!Object



17
18
19
20
21
22
# File 'lib/tiny_ds/validations.rb', line 17

def save!
  unless valid?
    raise RecordInvalid
  end
  do_save
end

#set_property(k, v) ⇒ Object

set property-value into @entity



254
255
256
257
258
259
260
261
262
263
# File 'lib/tiny_ds/base.rb', line 254

def set_property(k,v)
  prop_def = self.class.property_definition(k)
  ds_v = prop_def.to_ds_value(v)
  if ds_v.nil?
    @entity.removeProperty(k)
  else
    @entity[k] = ds_v
  end
  # todo cache value read/write
end

#valid?Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/tiny_ds/validations.rb', line 3

def valid?
  true # todo
end