Class: CEO::Iterator
- Inherits:
-
Object
- Object
- CEO::Iterator
- Defined in:
- lib/ceo/iterator.rb
Overview
Instance Attribute Summary collapse
-
#total_pages ⇒ Object
readonly
Public: Returns the total pages paginated.
Class Method Summary collapse
-
.acronymize(key) ⇒ Object
Public: Titleizes normal stuff, but upcases acronyms.
-
.except(things, blacklist) ⇒ Object
Public: Blacklists keys based on an array.
-
.filter(things, filters) ⇒ Object
Public: Filters an enum based on a hash of params.
-
.only(things, whitelist) ⇒ Object
Public: Whitelists keys based on an array.
Instance Method Summary collapse
-
#all ⇒ Object
Public: Returns a hash of titleized attributes mapped to their values.
-
#current_page ⇒ Object
Public: Returns the current page.
-
#each ⇒ Object
Public: Iterates through all parts.
-
#initialize(model, options = {}) ⇒ Iterator
constructor
model - The model to perform queries on.
-
#query_eval(scope, query) ⇒ Object
{ ‘Country Name’ => ‘South Korea’ }.
Constructor Details
#initialize(model, options = {}) ⇒ Iterator
model - The model to perform queries on. options - A hash of options for the iterator.
query - An array of nested queries.
filters - A hash of filters (only/except).
current_page - The current page to be paginated.
20 21 22 23 24 25 |
# File 'lib/ceo/iterator.rb', line 20 def initialize(model, = {}) @model = model @options = @queries = @options[:query] || [] @filters = @options[:filters] || {} end |
Instance Attribute Details
#total_pages ⇒ Object
Public: Returns the total pages paginated.
13 14 15 |
# File 'lib/ceo/iterator.rb', line 13 def total_pages @total_pages end |
Class Method Details
.acronymize(key) ⇒ Object
Public: Titleizes normal stuff, but upcases acronyms.
153 154 155 156 157 |
# File 'lib/ceo/iterator.rb', line 153 def self.acronymize(key) parsed_key = key.to_s.titleize parsed_key = parsed_key.gsub 'Id', 'ID' parsed_key.gsub 'Iata Code', 'IATA' end |
.except(things, blacklist) ⇒ Object
Public: Blacklists keys based on an array.
things - An array of keys to be filtered. blacklist - An array of keys that are not allowed.
Examples
blacklist = [:this, :that]
except([:this, :that, :here, :now], blacklist)
# => [:here, :now]
Returns a filtered hash of keys.
123 124 125 126 127 |
# File 'lib/ceo/iterator.rb', line 123 def self.except(things, blacklist) blacklist = blacklist.map(&:to_s) things = things.map(&:to_s) things.select { |thing| !blacklist.include? thing } end |
.filter(things, filters) ⇒ Object
Public: Filters an enum based on a hash of params.
things - An enum of things to filter. filters - A hash of filters (ALLOWED: [:only, :except]).
Since ‘only’ and ‘except’ are mutually exclusive, ‘only’ will be prefered over ‘except’.
Returns a hash of filtered keys.
104 105 106 107 108 109 |
# File 'lib/ceo/iterator.rb', line 104 def self.filter(things, filters) return self.only(things, filters[:only]) unless filters[:only].nil? || filters[:only].empty? return self.except(things, filters[:except]) unless filters[:except].nil? || filters[:except].empty? return things # if nothing to filter, just return the things end |
.only(things, whitelist) ⇒ Object
Public: Whitelists keys based on an array.
things - An array of keys to be filtered. whitelist - An array of keys that are only allowed.
Examples
whitelist = [:this, :that]
only([:this, :that, :here, :now], whitelist)
# => [:this, :that]
Returns a filtered hash of keys.
141 142 143 144 145 |
# File 'lib/ceo/iterator.rb', line 141 def self.only(things, whitelist) whitelist = whitelist.map(&:to_s) things = things.map(&:to_s) things.select { |thing| whitelist.include? thing } end |
Instance Method Details
#all ⇒ Object
Public: Returns a hash of titleized attributes mapped to their values.
Uses pagination.
options -
current_page - currently paginated page
per_page - # of things to list per page
Returns a paginated hash of data.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ceo/iterator.rb', line 49 def all attribute_maps = [] # [{...}, {...}] @pages = CEO::Paginator.new( @model, current_page: current_page, per_page: @options.fetch(:per_page, 20) ) self.total_pages = @pages.total_pages @pages.each do |thing| attr_object = {} # TODO: Make all of this into a method. # map first-level values to a hash keys.each do |key| attr_object[self.class.acronymize(key)] = thing[key.to_s] end # map nested values to a hash @queries.each do |query| attr_object.merge! query_eval(thing, query) end attribute_maps << attr_object end attribute_maps end |
#current_page ⇒ Object
Public: Returns the current page.
148 149 150 |
# File 'lib/ceo/iterator.rb', line 148 def current_page (@options[:current_page] || 1).to_i end |
#each ⇒ Object
Public: Iterates through all parts.
Yields or returns an Enumerator.
30 31 32 33 34 35 36 37 38 |
# File 'lib/ceo/iterator.rb', line 30 def each if block_yielded? all.each do |thing| yield(thing) end else all.to_enum end end |
#query_eval(scope, query) ⇒ Object
{ ‘Country Name’ => ‘South Korea’ }
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ceo/iterator.rb', line 78 def query_eval(scope, query) query_parts = query.split('.') if query_parts.length > 2 title = self.class.acronymize(query_parts[-2..-1].join(' ')) resp = 'None' if scope.instance_eval(query_parts[0]).nil? || scope.instance_eval(query_parts[0..1].join('.')).nil? elsif query_parts[-1] == 'name' title = self.class.acronymize(query_parts.join(' ')) resp = 'None' if scope.instance_eval(query_parts[0]).nil? else title = self.class.acronymize query_parts[-1] resp = 'None' if scope.instance_eval(query_parts[0]).nil? end resp = scope.instance_eval(query) unless resp == 'None' { title => resp } end |