Skip to content

Commit

Permalink
added ability to use create_* methods to application
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo committed Nov 8, 2012
1 parent 354de05 commit 1db04b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/usergrid/core/application.rb
Expand Up @@ -16,12 +16,23 @@ def create_user(username, password, email=nil, name=nil, invite=false)
create_entity 'users', user_hash
end

# note: collection_name s/b plural!
# note: collection_name s/b plural, but the server will change it if not
def create_entity(collection_name, entity_data)
self[collection_name].post entity_data
end
alias_method :create_entities, :create_entity

# allow create_something(hash_or_array) method
def method_missing(method, *args, &block)
method_s = method.to_s
if method_s.start_with? 'create_'
entity = method_s.split('_')[1]
create_entity entity, *args
else
super method, args, block
end
end

def users(query=nil, options={})
self[__method__].query(query, options)
end
Expand Down
13 changes: 12 additions & 1 deletion spec/usergrid/core/application_spec.rb
Expand Up @@ -201,7 +201,8 @@
response = @application.counter 'test'
counter = response.data.counters.first
counter.name.should eq 'test'
counter.values.last.first.value.should be > 0
# can't reliably test this - counters are batched on server
#counter.values.last.first.value.should be > 0
end

it "should be able to create, retrieve, and delete roles" do
Expand Down Expand Up @@ -260,4 +261,14 @@
collection.size.should eq 4
end

it "should be able to create a new collection via create_ method and access it" do
entities = (1..4).collect do |i|
{ name: "test_#{i}" }
end
@application.create_moretests entities
response = @application['moretests'].get
collection = response.collection
collection.size.should eq 4
end

end

0 comments on commit 1db04b4

Please sign in to comment.