Skip to content

Commit

Permalink
Suppor relations and pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
goofmint committed Sep 23, 2016
1 parent d3af6bb commit 8fce9ed
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 2 deletions.
38 changes: 38 additions & 0 deletions examples/performance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$:.unshift(File.dirname(__FILE__))
require 'rubygems'
require 'ncmb'
require 'yaml'
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']

example = NCMB::DataStore.new 'Example'

require 'benchmark'

ary = []
Benchmark.bm 10 do |r|
r.report "Save DataStore" do
100.times do |i|
item = example.new
item.set('String', "テスト#{i}00")
item.set('Integer', i)
item.set('Boolean', true)
item.set('Array', [i, i * 2, i * 3, "Orange", "Tomato"])
item.set('Object', {test1: 'a', test2: 'b'})
item.set('Location', NCMB::GeoPoint.new(30, 50))
item.set('MultipleLine', "test\ntest\n")
item.set('Increment', NCMB::Increment.new(i + 1))
item.set('Date', Time.now)
item.save
ary << item.objectId
end
end
r.report "Delete objects" do
ary.each do |objectId|
item = example.new(objectId: objectId)
item.delete
end
end
end

16 changes: 16 additions & 0 deletions examples/pointer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$:.unshift(File.dirname(__FILE__))
require 'rubygems'
require 'ncmb'
require 'yaml'
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']

Parent = NCMB::DataStore.new 'Parent'

Child = NCMB::DataStore.new 'Child'
child = Child.new(name: "Taro")
parent = Parent.new(name: "Oya")
parent.child = child
parent.save

22 changes: 22 additions & 0 deletions examples/relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$:.unshift(File.dirname(__FILE__))
require 'rubygems'
require 'ncmb'
require 'yaml'
yaml = YAML.load_file(File.join(File.dirname(__FILE__), '..', 'setting.yml'))
NCMB.initialize application_key: yaml['application_key'], client_key: yaml['client_key']

Food = NCMB::DataStore.new 'Food'

Basket = NCMB::DataStore.new('Basket')
basket = Basket.new
basket.foods = []
basket.foods << Food.new(name: "banana", type: "fruit")
basket.foods << Food.new(name: "pear", type: "fruit")

# relation = NCMB::Relation.new
# relation << Food.new(name: "banana", type: "fruit", objectId: "test1")
# relation << Food.new(name: "pear", type: "fruit", objectId: "test2")

# puts basket.fields
basket.save
1 change: 1 addition & 0 deletions lib/ncmb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
require "ncmb/increment"
require "ncmb/acl"
require "ncmb/role"
require "ncmb/relation"
require "ncmb/error"
2 changes: 1 addition & 1 deletion lib/ncmb/acl.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module NCMB
class Acl < NCMB::Object
class Acl
include NCMB

def initialize(params = nil)
Expand Down
26 changes: 26 additions & 0 deletions lib/ncmb/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def fields
@fields
end

def ClassName
@name
end

def method_missing(name, value = nil)
if name =~ /.*=$/
sym = name.to_s.gsub(/(.*?)=$/, '\1').to_sym
Expand Down Expand Up @@ -56,7 +60,29 @@ def path
"#{base_path}/#{@fields[:objectId] || '' }"
end

def saved?
@fields[:objectId] != nil
end

def post
@fields.each do |key, field|
if field.is_a?(NCMB::Object)
field.save unless field.saved?
@fields[key] = {
__type: "Pointer",
className: field.ClassName,
objectId: field.objectId
}
end
if field.is_a?(Array) && field[0].is_a?(NCMB::Object)
relation = NCMB::Relation.new
field.each do |sub_field|
sub_field.save unless sub_field.saved?
relation << sub_field
end
@fields[key] = relation
end
end
result = @@client.post path, @fields
@fields.merge!(result)
self
Expand Down
24 changes: 24 additions & 0 deletions lib/ncmb/relation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module NCMB
class Relation < Array
include NCMB

def initialize

end

def to_json(a = "")
params = {
"__op": "AddRelation",
}
params["objects"] = []
self.each do |obj|
params["objects"] << {
"__type": "Pointer",
"className": obj.ClassName,
"objectId": obj.objectId
}
end
params.to_json
end
end
end
2 changes: 1 addition & 1 deletion lib/ncmb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ncmb
VERSION = "0.1.1"
VERSION = "0.1.2"
end

0 comments on commit 8fce9ed

Please sign in to comment.