README.md in to_collection-1.0.1 vs README.md in to_collection-2.0.0
- old
+ new
@@ -1,18 +1,21 @@
-# `to_collection`
+# ToCollection 2.0.0 Ruby Refinement
[](http://badge.fury.io/rb/to_collection)
[](https://travis-ci.org/AndyObtiva/to_collection)
[](https://coveralls.io/github/AndyObtiva/to_collection?branch=master)
Treat an array of objects and a singular object uniformly as a collection of objects.
-Especially useful in processing REST Web Service API JSON responses in a functional approach.
+Especially useful in processing REST Web Service API JSON responses in a uniform functional approach.
+
+`ToCollection` is a Ruby Refinement, so it may be safely enabled via `using ToCollection` where needed only.
+
## Introduction
Canonicalize data to treat uniformly whether it comes in as a single object or an array of objects, dropping `nils` out automatically.
-API: `object.to_collection(compact)` where `compact` is a boolean for whether to compact collection or not. It is true by default.
+API: `object#to_collection(compact=true)` where `compact` is a boolean for whether to compact collection or not. It is true by default.
Example:
```ruby
city_counts = {}
@@ -37,22 +40,29 @@
end
```
## Instructions
-### Bundler / Rails
+### Bundler
-`gem 'to_collection', '~> 1.0.1'`
+- Add `gem 'to_collection', '~> 2.0.0'` to Gemfile
+- Run `bundle`
+- Require `to_collection` ruby gem in code (e.g. via `Bundler.require(:default)` or `require 'bundler/setup'` & `require 'to_collection'`)
+- Add `using ToCollection` to the top of the Ruby file you would like to refine `Object` in with `#to_collection` method.
-### Plain Ruby
+### Manual Gem Install
-`require 'to_collection'`
+- Run `gem install to_collection -v2.0.0`
+- Add `require 'to_collection'` to code
+- Add `using ToCollection` to the top of the Ruby file you would like to refine `Object` in with `#to_collection` method.
### Note
-Code above enables `#to_collection` method on all classes inheriting from `Object`. See [options](#options) below in case you prefer to **manually** include in certain classes only.
+If '#to_collection' was already defined on `Object` in a project, requiring the `to_collection` library will print a warning.
+It is still safe to require as it does not overwrite `Object#to_collection` except in Ruby files where `using ToCollection` is added.
+
## Background
I'm sure you've encountered REST Web Service APIs that operate as follows:
HTTP Request: =>
@@ -61,11 +71,11 @@
```
<= 1 person
JSON Response:
```JSON
-{"first_name":"karim","last_name":"akram","city":"Dubai"}
+{"first_name":"John","last_name":"Barber","city":"Chicago"}
```
HTTP Request: =>
```
GET /people
@@ -73,11 +83,11 @@
<= 3 people
JSON Response:
```JSON
-[{"first_name":"karim","last_name":"akram","city":"Dubai"}, {"first_name":"muhsen","last_name":"asaad","city":"Amman"}, {"first_name":"assaf","last_name":"munir","city":"Qatar"}]
+[{"first_name":"John","last_name":"Barber","city":"Chicago"}, {"first_name":"Mark","last_name":"Jones","city":"New York"}, {"first_name":"Josh","last_name":"Beeswax","city":"Denver"}]
```
How do you work with the varied JSON responses in Ruby?
One approach for an app that needs to count people in cities:
@@ -196,11 +206,11 @@
```
How about go one step further and bake this into all objects using our previous approach of object-oriented polymorphism and Ruby open-classes? That way, we don't just collapse the difference between dealing with arrays of hashes vs hashes but also arrays of objects vs singular objects by adding. Note the use of flatten(1) below to prevent arrays or arrays from collapsing more than one level.
```ruby
-Object.class_eval do
+class Object
def to_collection
[self].flatten(1).compact
end
end
```
@@ -216,11 +226,11 @@
```
A refactored version including optional compacting would be:
```ruby
-Object.class_eval do
+class Object
def to_collection(compact=true)
collection = [self].flatten(1)
compact ? collection.compact : collection
end
end
@@ -239,81 +249,31 @@
city_counts[person["city"]] += 1
end
end
```
-You asked for "Elegant" didn't you? I hope that was what you were looking for.
+Of course, in Ruby 2+, you may use Ruby Refinements, so simply include `Object#to_collection` via this line instead:
-## How It Works
-
-A [super_module](https://github.com/AndyObtiva/super_module) called `ToCollection`
-contains the `#to_collection` method and is included (mixed) into `Object`, providing
-`#to_collection` method to inheriting classes.
-
-## Options
-
-### `Object.to_collection_already_implemented_strategy`
-Possible Values: `"raise_error"` (default), `"keep"`, `"overwrite"`
-
-Setting this option allows developer to configure handling of the case when
-`Object#to_collection` already exists before loading `to_collection` library.
-
-#### `"raise_error"` (default)
-
-For safety reasons, the library will raise AlreadyImplementedError by default to
-alert the developer and provide information about the other options.
-This prevents later surprises and puts control in the hand of the developer to
-responsibly decide what option to pick next.
-
-#### `"keep"`
-
-This keeps existing `to_collection` untouched, disabling this library.
-
-#### `"overwrite"`
-
-This overwrites existing `to_collection` method, fully enabling this library.
-
-### `ENV['TO_COLLECTION_ALREADY_IMPLEMENTED_STRATEGY']`
-Possible Values: `"raise_error"` (default), `"keep"`, `"overwrite"`
-
-Same function as `Object.to_collection_already_implemented_strategy`.
-Environment variable takes precedence over class accessor variable though.
-
-### `ENV['TO_COLLECTION_OBJECT_INCLUDE']`
-Possible Values: `"true"` (default), `"false"`
-
-Must be set before requiring/loading library. When using bundler, ensure `require` option is set to `false` or `nil`.
-
-`ToCollection` [super_module](https://github.com/AndyObtiva/super_module) is automatically included in `Object` except when `ENV['TO_COLLECTION_OBJECT_INCLUDE']` is set to `"false"`, providing developer with the option to **manually** include (mix in) `ToCollection` [super_module](https://github.com/AndyObtiva/super_module) into classes that need it.
-
-Example:
-
-Bundler would have gem require option as false:
-
```ruby
-gem 'to_collection', require: false
+using ToCollection
```
-Ruby code would then set that environment variable **manually** before requiring library:
+You asked for "Elegant" didn't you? I hope that was what you were looking for.
-```ruby
-ENV['TO_COLLECTION_OBJECT_INCLUDE'] = false
-require 'to_collection'
-Hash.instance_eval do
- include ToCollection #enables to_collection method
-end
-Array.instance_eval do
- include ToCollection #enables to_collection method
-end
-response_data = people_http_request #returns single hash or array of hashes
-response_data.to_collection.each do |person_hash|
- # do some work
-end
-```
+## How It Works
+A Ruby Refinement is activated via `using ToCollection` adding/overwriting the `#to_collection` method in `Object`, which
+is the ancestor of all Ruby objects.
+
## Release Notes
+### v2.0.0
+
+- Revamped API using Ruby Refinements (safer than monkey-patching)
+- Removed `super_module` gem dependency
+- Dropped safety options since Ruby Refinements already handle things safely
+
### v1.0.1
- Updated `super_module` gem version to relax indirect `method_source` gem version dependency
## Contributing
@@ -328,7 +288,7 @@
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. Also, do not upgrade `jeweler`. It is intentionally at an old version that is compatible with running tests in Travis with older verison of Ruby as well as supporting Coveralls, Simplecov, and Code Climate.
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
## Copyright
-Copyright (c) 2017 Andy Maleh. See LICENSE.txt for
+Copyright (c) 2017-2020 Andy Maleh. See LICENSE.txt for
further details.