orchestrate-io/orchestrate-ruby — pull request #51 by mattly (2014-06-18 00:33:18+00:00)
Title: WIP rubyisms

It's not ready yet, just wanted to provide a place to talk about these.

The basics:

```ruby
app = Orchestrate::Application.new "your api key"
users = app[:users]
```

`Application` is currently a very thin wrapper around `Client`, and is
constructed the same way. I eventually plan to put a factory on the base
`Orchestrate` module with a memoized accessor.

`Collection` is pretty basic for now, but here's some things it can do:

```ruby
users.create(:rich, {"name" => "Richard"}) # performs put-if-absent, returns KeyValue
users.set(:rich, {"name" => "Rich"}) # performs put, returns KeyValue
users[:rich] = {"name" => "Richard III"} # performs put, returns input (per ruby standard)
users[:rich] # returns KV
```

I've also got a very loose work-in-progress Lazy Enumerable around the
list query by implementing Enumerable in collection. So you can do for
example:

```ruby
users.map {|user| [user.key, user.refime] }
```

to iterate over **all** the items in the collection (10 at a time). I'm
trying to think about scoping and whatnot, with start/end/etc and limits.
The ruby way to do limits would be `users.take(15).map {|u| ... }` but
I have no way to say, do a larger query because I know I only need 15
users. So, thinking about this.

Here's some stuff with `KeyValue`:

```ruby
rich = users[:rich]
rich.value # the item's value
rich[:name] # does a lookup against the item's value, converting keys to strings as needed
rich[:name] = "Dick" # again, converts the key to a string
if rich.save # performs put-if-match with current ref, returns boolean for success/failure. indexing_conflict error is considered success.
puts rich.ref # the new ref from the response
else
rich.reload! # retrieves the current value
# ...
rich.save! # performs put-if-match, raises on exception. indexing_conflict is again considered a success.
end
```

There's also `KeyValue#destroy` and `KeyValue#destroy!` that perform
delete-if-match and the bang version raises on error. The bang version
raising on error is a convention from rails I've adopted.

Next up, I'm going to round out the list and search stuff on collection,
and go back to KV and do Refs.