Last active
December 17, 2015 10:39
-
-
Save kouky/5596294 to your computer and use it in GitHub Desktop.
Working with blake-data-models to filter by authors
This file contains hidden or 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
# Get access to a Library object by locale, always scope by locale. | |
# A library object gives us convenience methods to access | |
# books, authors, fiction and non-fiction book code indices by locale. | |
# The files that drive these indices are located in blake/data/models/db/library/[product]/[subject-index].yml | |
library = BlakeDataModels::Library.new(locale: 'au', name: 'reading_eggspress') | |
# What series are available in the locale's library? | |
library.series_names # Generates array ["abc", "blake_novels", "boffin_boy", "brainwaves", ...] | |
# What authors have books in this locale's library? | |
library.author_names # Generates array ["Alexander, Goldie", "Andersen, Hans Christian", "Anderson, WM", ...] | |
# Give me a list of book summaries for a particular book series? | |
# Generates an array of book summaries | |
# [ {"locale"=>"au", "series"=>"abc", "code"=>"abc_40_cool_science_tricks", "set"=>"the surfing scientist", "authors"=>["Meerman, Ruben"], "name"=>"40 Cool Science Tricks", ...}] | |
library.series('abc') | |
# Generate your collection of all book summaries for the entire locale | |
# This should generate an array of approx 1200 book summaries, | |
# use this to initialise your Ember data models | |
all_books = library.series_names.map { |series_code| library.series(series_code)}.flatten | |
# Filter all books by author | |
# This should generate an array of approx 200 book summaries | |
all_books.find_all {|book| book[:authors].include?("Pike, Katy") } | |
end | |
No problem, thought I'd spell it out a little. The library object's main purpose is to operate on indices which serve as a fast access cache and secondly as a locale control. Some books like the abc series shouldn't be available under the US locale for example. It also helps us release in progress books to areas such as the staff library for testing and review before they make their way into reading eggspress library.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks mate, just what I needed.