Class: AppEngine::MirahDatastorePlugin::ModelState

Inherits:
Object
  • Object
show all
Includes:
Mirah::AST
Defined in:
lib/mirah_model/datastore.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transformer, klass, parent, position, ast) ⇒ ModelState

Returns a new instance of ModelState.



57
58
59
60
61
62
63
64
# File 'lib/mirah_model/datastore.rb', line 57

def initialize(transformer, klass, parent, position, ast)
  @transformer = transformer
  @kind = klass.name.split('.')[-1]
  init_query(klass.name, parent, position, ast)
  init_static(parent, ast)
  init_read(parent, position, ast)
  init_save(parent, position, ast)
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



55
56
57
# File 'lib/mirah_model/datastore.rb', line 55

def kind
  @kind
end

#queryObject (readonly)

Returns the value of attribute query.



55
56
57
# File 'lib/mirah_model/datastore.rb', line 55

def query
  @query
end

#readObject (readonly)

Returns the value of attribute read.



55
56
57
# File 'lib/mirah_model/datastore.rb', line 55

def read
  @read
end

#saveObject (readonly)

Returns the value of attribute save.



55
56
57
# File 'lib/mirah_model/datastore.rb', line 55

def save
  @save
end

#transformerObject (readonly)

Returns the value of attribute transformer.



55
56
57
# File 'lib/mirah_model/datastore.rb', line 55

def transformer
  @transformer
end

Instance Method Details

#eval(parent, src) ⇒ Object



231
232
233
# File 'lib/mirah_model/datastore.rb', line 231

def eval(parent, src)
  transformer.eval(src, __FILE__, parent)
end

#extend_get_properties(code) ⇒ Object



243
244
245
# File 'lib/mirah_model/datastore.rb', line 243

def extend_get_properties(code)
  @get_properties << eval(@get_properties, code)
end

#extend_query(code) ⇒ Object



235
236
237
# File 'lib/mirah_model/datastore.rb', line 235

def extend_query(code)
  query.body << eval(query.body, code)
end

#extend_read(code) ⇒ Object



247
248
249
250
251
252
# File 'lib/mirah_model/datastore.rb', line 247

def extend_read(code)
  code = 'e=nil;' + code
  eval(read.body, code).children[1..-1].each do |node|
    read.body << node
  end
end

#extend_save(code) ⇒ Object



254
255
256
257
258
259
# File 'lib/mirah_model/datastore.rb', line 254

def extend_save(code)
  code = 'e=nil;' + code
  eval(save.body, code).children[1..-1].each do |node|
    save.body << node
  end
end

#extend_update(code) ⇒ Object



239
240
241
# File 'lib/mirah_model/datastore.rb', line 239

def extend_update(code)
  @update << eval(@update, code)
end

#init_query(classname, parent, position, ast) ⇒ Object



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
# File 'lib/mirah_model/datastore.rb', line 66

def init_query(classname, parent, position, ast)
  name = "#{classname}$Query"
  @query = ClassDefinition.new(parent, position, name) do |classdef|
    queryinit = <<-EOF
      def initialize; end

      def kind
        "#{kind}"
      end

      def first
        it = _prepare.asIterator
        if it.hasNext
          e = Entity(it.next)
          m = #{kind}.new
          m._read_from(e)
          m
        else
          #{kind}(nil)
        end
      end

      def run
        entities = _prepare.asList(_options)
        models = #{kind}[entities.size]
        it = entities.iterator
        i = 0
        while (it.hasNext)
          e = Entity(it.next)
          m = #{kind}.new
          m._read_from(e)
          models[i] = m
          i += 1
        end
        models
      end

      def sort(name:String)
        sort(name, false)
      end

      def sort(name:String, descending:boolean)
        _sort(name, descending)
        self
      end
    EOF
    [Mirah::AST.type(nil, 'com.google.appengine.ext.mirah.db.DQuery'),
     eval(classdef, queryinit)]
  end
  ast << @query
end

#init_read(parent, position, ast) ⇒ Object



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
# File 'lib/mirah_model/datastore.rb', line 118

def init_read(parent, position, ast)
  @read = eval(parent, <<-EOF)
    def _read_from(e:Entity)
      self.entity = e
      nil
    end
  EOF
  ast << @read
  get_properties = eval(parent, <<-EOF)
    def properties
      result = super()
      nil
      result
    end
  EOF
  @get_properties = get_properties.body.children[1] =
      Mirah::AST::Body.new(get_properties.body, position)
  ast << get_properties
  update = eval(parent, <<-EOF)
    def update(properties:Map)
      nil
      self
    end
  EOF
  @update = update.body.children[0] = Body.new(update.body, position) {[]}
  ast << update
end

#init_save(parent, position, ast) ⇒ Object



146
147
148
149
150
151
152
153
# File 'lib/mirah_model/datastore.rb', line 146

def init_save(parent, position, ast)
  @save = eval(parent, <<-EOF)
    def _save_to(e:Entity)
    end
  EOF
  @save.body = Body.new(@save, position) {[]}
  ast << @save
end

#init_static(parent, ast) ⇒ Object



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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mirah_model/datastore.rb', line 155

def init_static(parent, ast)
  # TODO These imports don't work any more.  Figure out how to fix that.
  scope = ast.static_scope
  package = "#{scope.package}." unless scope.package.empty?
  scope.import('java.util.Map', 'Map')
  scope.import("#{package}#{kind}$Query", "#{kind}__Query__")
  %w( Entity Blob Category Email GeoPt IMHandle Key
      Link PhoneNumber PostalAddress Rating ShortBlob
      Text KeyFactory EntityNotFoundException ).each do |name|
    scope.import("com.google.appengine.api.datastore.#{name}", name)
  end
  ast << eval(parent, <<-EOF)
    def initialize
      super
    end

    def initialize(key_name:String)
      super
    end
    
    def initialize(parent:Model)
      super
    end
    
    def initialize(parent:Key)
      super
    end
    
    def initialize(parent:Model, key_name:String)
      super
    end
    
    def initialize(parent:Key, key_name:String)
      super
    end

    def self.get(key:Key)
      begin
        m = #{kind}.new
        m._read_from(Model._datastore.get(key))
        m
      rescue EntityNotFoundException
        nil
      end
    end

    def self.get(key_name:String)
      get(KeyFactory.createKey("#{kind}", key_name))
    end

    def self.get(id:long)
      get(KeyFactory.createKey("#{kind}", id))
    end

    def self.get(parent:Key, key_name:String)
      get(KeyFactory.createKey(parent, "#{kind}", key_name))
    end

    def self.get(parent:Key, id:long)
      get(KeyFactory.createKey(parent, "#{kind}", id))
    end

    def self.get(parent:Model, key_name:String)
      get(KeyFactory.createKey(parent.key, "#{kind}", key_name))
    end

    def self.get(parent:Model, id:long)
      get(KeyFactory.createKey(parent.key, "#{kind}", id))
    end

    def self.all
      #{kind}__Query__.new
    end
  EOF
end