Skip to content

Commit

Permalink
Opal Support
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyObtiva committed Dec 9, 2020
1 parent ab5f854 commit bda6372
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 38 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,13 @@
### v2.0.1

- Opal support via monkey-patching

### 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
22 changes: 11 additions & 11 deletions Gemfile.lock
Expand Up @@ -11,7 +11,7 @@ GEM
tins (~> 1.6)
descendants_tracker (0.0.4)
thread_safe (~> 0.3, >= 0.3.1)
diff-lcs (1.3)
diff-lcs (1.4.4)
docile (1.3.2)
faraday (0.9.2)
multipart-post (>= 1.2, < 3)
Expand All @@ -26,7 +26,7 @@ GEM
oauth2 (~> 1.0)
hashie (4.1.0)
highline (2.0.3)
jar-dependencies (0.4.0)
jar-dependencies (0.4.1)
jeweler (2.3.9)
builder
bundler
Expand All @@ -38,25 +38,25 @@ GEM
rake
rdoc
semver2
json (2.3.0)
json (2.3.0-java)
jwt (2.2.1)
json (2.3.1)
json (2.3.1-java)
jwt (2.2.2)
mime-types (2.99.3)
mini_portile2 (2.4.0)
multi_json (1.14.1)
multi_json (1.15.0)
multi_xml (0.6.0)
multipart-post (2.1.1)
nokogiri (1.10.9)
nokogiri (1.10.10)
mini_portile2 (~> 2.4.0)
nokogiri (1.10.9-java)
nokogiri (1.10.10-java)
oauth2 (1.4.4)
faraday (>= 0.8, < 2.0)
jwt (>= 1.0, < 3.0)
multi_json (~> 1.3)
multi_xml (~> 0.5)
rack (>= 1.2, < 3)
psych (3.1.0)
psych (3.1.0-java)
psych (3.2.0)
psych (3.2.0-java)
jar-dependencies (>= 0.1.7)
rack (2.2.3)
rake (13.0.1)
Expand Down Expand Up @@ -87,7 +87,7 @@ GEM
thor (1.0.1)
thread_safe (0.3.6)
thread_safe (0.3.6-java)
tins (1.25.0)
tins (1.26.0)
sync

PLATFORMS
Expand Down
2 changes: 0 additions & 2 deletions LICENSE
@@ -1,5 +1,3 @@
MIT License

Copyright (c) 2017-2020 Andy Maleh

Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down
35 changes: 18 additions & 17 deletions README.md
@@ -1,4 +1,4 @@
# ToCollection 2.0.0 Ruby Refinement
# ToCollection 2.0.1 Ruby Refinement
[![Gem Version](https://badge.fury.io/rb/to_collection.svg)](http://badge.fury.io/rb/to_collection)
[![Build Status](https://travis-ci.org/AndyObtiva/to_collection.svg?branch=master)](https://travis-ci.org/AndyObtiva/to_collection)
[![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/to_collection/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/to_collection?branch=master)
Expand All @@ -9,6 +9,8 @@ Especially useful in processing REST Web Service API JSON responses in a uniform

`ToCollection` is a Ruby Refinement, so it may be safely enabled via `using ToCollection` where needed only.

Now also supports [Opal Ruby](https://opalrb.com) by degrading gracefully to monkey-patching.

## Introduction

Canonicalize data to treat uniformly whether it comes in as a single object or an array of objects, dropping `nils` out automatically.
Expand Down Expand Up @@ -44,20 +46,26 @@ end

### Bundler

- Add `gem 'to_collection', '~> 2.0.0'` to Gemfile
- Add `gem 'to_collection', '~> 2.0.1'` 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.

### Manual Gem Install

- Run `gem install to_collection -v2.0.0`
- Run `gem install to_collection -v2.0.1`
- 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.

### Opal Ruby

- Follow the first step in [Bundler](#bundler) or [Manual Gem Install](#manual-gem-install) instructions to add the gem.
- Add Opal.use_gem 'to_collection' to "config/initializers/assets.rb" in Rails unless using as part of another gem.
- Add `require 'to_collection'` to code (this adds the functionality via monkey-patching not refinements in [Opal](https://opalrb.com))

### Note

If '#to_collection' was already defined on `Object` in a project, requiring the `to_collection` library will print a warning.
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.

Expand Down Expand Up @@ -261,20 +269,12 @@ You asked for "Elegant" didn't you? I hope that was what you were looking for.

## How It Works

A Ruby Refinement is activated via `using ToCollection` adding/overwriting the `#to_collection` method in `Object`, which
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
## Change Log

- 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
[CHANGELOG.md](CHANGELOG.md)

## Contributing

Expand All @@ -290,5 +290,6 @@ is the ancestor of all Ruby objects.

## Copyright

Copyright (c) 2017-2020 Andy Maleh. See LICENSE.txt for
further details.
[MIT](LICENSE.txt)

Copyright (c) 2017-2020 Andy Maleh.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -21,7 +21,7 @@ Jeweler::Tasks.new do |gem|
gem.description = %Q{ToCollection Ruby Refinement - Treat an array of objects and a singular object uniformly as a collection of objects}
gem.email = "andy.am@gmail.com"
gem.authors = ["Andy Maleh"]
gem.files = Dir['lib/**/*.rb']
gem.files = Dir['VERSION', 'README.md', 'CHANGELOG.md', 'to_collection.gemspec', 'lib/**/*.rb']
# dependencies defined in Gemfile
end
Jeweler::RubygemsDotOrgTasks.new
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
2.0.0
2.0.1
32 changes: 31 additions & 1 deletion lib/to_collection.rb
@@ -1,6 +1,29 @@
# require 'super_module'
# Copyright (c) 2017-2020 Andy Maleh
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

module ToCollection
if RUBY_PLATFORM == 'opal'
def self.refine(class_or_module, &refinement)
class_or_module.class_eval(&refinement)
end
end
refine Object do
begin
method(:to_collection)
Expand All @@ -15,3 +38,10 @@ def to_collection(compact=true)
end
end
end

if RUBY_PLATFORM == 'opal'
# Create a shim `using` method that does nothing since we monkey-patch in Opal earlier in the `refine` method
def self.using(refinement)
# NO OP
end
end
15 changes: 10 additions & 5 deletions to_collection.gemspec
Expand Up @@ -2,28 +2,33 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: to_collection 2.0.0 ruby lib
# stub: to_collection 2.0.1 ruby lib

Gem::Specification.new do |s|
s.name = "to_collection".freeze
s.version = "2.0.0"
s.version = "2.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Andy Maleh".freeze]
s.date = "2020-05-29"
s.date = "2020-12-09"
s.description = "ToCollection Ruby Refinement - Treat an array of objects and a singular object uniformly as a collection of objects".freeze
s.email = "andy.am@gmail.com".freeze
s.extra_rdoc_files = [
"CHANGELOG.md",
"LICENSE",
"README.md"
]
s.files = [
"lib/to_collection.rb"
"CHANGELOG.md",
"README.md",
"VERSION",
"lib/to_collection.rb",
"to_collection.gemspec"
]
s.homepage = "http://github.com/AndyObtiva/to_collection".freeze
s.licenses = ["MIT".freeze]
s.rubygems_version = "3.1.2".freeze
s.rubygems_version = "3.1.4".freeze
s.summary = "ToCollection Ruby Refinement - Treat an array of objects and a singular object uniformly as a collection of objects".freeze

if s.respond_to? :specification_version then
Expand Down

0 comments on commit bda6372

Please sign in to comment.