Skip to content

Commit

Permalink
Merge pull request #193 from alessandro1997/filter-subregions-by-type
Browse files Browse the repository at this point in the history
Allow filtering subregions by type
  • Loading branch information
j15e committed Jan 14, 2016
2 parents f1722a0 + 1801b70 commit cd727af
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -55,6 +55,11 @@ similarly to a `Country` to find, for instance, a specific state:
illinois = us.subregions.coded('IL')
=> <#Carmen::Region "Illinois">

You can also find all subregions with a specific type:

states = us.subregions.typed('state')
=> [<#Carmen::Region name="Alaska" type="state">, <#Carmen::Region name="Alabama" type="state">, ...]

Subregions support a smaller set of attributes than countries:

illinois.name
Expand Down
12 changes: 12 additions & 0 deletions lib/carmen/region_collection.rb
Expand Up @@ -16,6 +16,18 @@ module Carmen
class RegionCollection < Array
include Querying

# Filters the regions in this collection by type.
#
# type - The String type to filter by
#
# Returns a region collection containing all the regions with the supplied
# type.
def typed(type)
downcased_type = type.downcase
results = select{ |r| r.type == downcased_type }
Carmen::RegionCollection.new(results)
end

private

def query_collection
Expand Down
16 changes: 16 additions & 0 deletions spec/carmen/region_collection_spec.rb
@@ -0,0 +1,16 @@
require 'spec_helper'

describe Carmen::RegionCollection do

before do
@collection = Carmen::RegionCollection.new([
Carmen::Region.new('type' => 'custom_type1', 'code' => 'AA'),
Carmen::Region.new('type' => 'custom_type2', 'code' => 'BB')
])
end

it 'provides an API for filtering regions by type' do
@collection.typed('custom_type1').map(&:code).must_equal ['AA']
end

end

0 comments on commit cd727af

Please sign in to comment.