Skip to content

Commit

Permalink
Remove monkey patch on Enumerable (#71)
Browse files Browse the repository at this point in the history
* Remove monkey patch on Enumerable

Instead of monkey-patching Enumerable to get some
methods we want on to arrays, we can instantiate
our own `BoxrCollection` object which behaves like
an Array. This is preferrable to monkey-patching
because we're not polluting the namespace of other
objects in our app which might use the Enumerable module.

* Bump version to 1.5.1
  • Loading branch information
mctaylorpants authored and xhocquet committed May 11, 2019
1 parent 1e41937 commit 39d6677
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions lib/boxr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@
require 'boxr/web_links'
require 'boxr/watermarking'

module Enumerable
class BoxrCollection < Array
def files
self.select{|i| i.type == 'file'}
collection_for_type('file')
end

def folders
self.select{|i| i.type == 'folder'}
collection_for_type('folder')
end

def web_links
self.select{|i| i.type == 'web_link'}
collection_for_type('web_link')
end

private

def collection_for_type(type)
items = select { |i| i.type == type }
BoxrCollection.new(items)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/boxr/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def get_all_with_pagination(uri, query: {}, offset: 0, limit: DEFAULT_LIMIT, fol
end
end until offset - total_count >= 0

entries.flatten.map{|i| BoxrMash.new(i)}
BoxrCollection.new(entries.flatten.map{ |i| BoxrMash.new(i) })
end

def post(uri, body, query: nil, success_codes: [201], process_body: true, content_md5: nil, content_type: nil, if_match: nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/boxr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Boxr
VERSION = "1.5.0"
VERSION = "1.5.1"
end

0 comments on commit 39d6677

Please sign in to comment.