-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from gadabout/feature/filtering
Added filtering.
- Loading branch information
Showing
5 changed files
with
92 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Lurch | ||
class ParamBuilder | ||
def initialize(params) | ||
@params = Hash(params) | ||
end | ||
|
||
def encode | ||
encode_value(@params) | ||
end | ||
|
||
private | ||
|
||
def encode_value(value, key = nil) | ||
case value | ||
when Hash then value.map { |k, v| encode_value(v, append_key(key, k)) }.reject(&:empty?).join("&") | ||
when Array then value.map { |v| encode_value(v, "#{key}[]") }.reject(&:empty?).join("&") | ||
when nil then "" | ||
else | ||
"#{key}=#{CGI.escape(value.to_s)}" | ||
end | ||
end | ||
|
||
def append_key(root_key, key) | ||
root_key.nil? ? key : "#{root_key}[#{key}]" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Lurch | ||
class Query | ||
def initialize(store) | ||
@store = store | ||
@filter = {} | ||
@sort = {} | ||
@page = {} | ||
@include = {} | ||
@fields = {} | ||
end | ||
|
||
def filter(params) | ||
@filter = @filter.merge(params) | ||
self | ||
end | ||
|
||
def from(type) | ||
@type = Lurch.normalize_type(type) | ||
self | ||
end | ||
|
||
def all | ||
@store.load_from_url(resources_url) | ||
end | ||
|
||
def find(id) | ||
@store.peek(@type, id) || @store.load_from_url(resource_url(id)) | ||
end | ||
|
||
def to_params | ||
ParamBuilder.new( | ||
filter: @filter, | ||
sort: @sort, | ||
page: @page, | ||
include: @include, | ||
fields: @fields | ||
).encode | ||
end | ||
|
||
private | ||
|
||
def resources_url | ||
uri = URI.parse("/#{Inflecto.dasherize(@type.to_s)}") | ||
params = to_params | ||
uri.query = params unless params.empty? | ||
uri.to_s | ||
end | ||
|
||
def resource_url(id) | ||
uri = URI.parse("#{resources_url}/#{id}") | ||
params = to_params | ||
uri.query = params unless params.empty? | ||
uri.to_s | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module Lurch | ||
VERSION = "0.0.6".freeze | ||
VERSION = "0.0.7".freeze | ||
end |