Class: Graphene::ResultSet
- Inherits:
-
Object
- Object
- Graphene::ResultSet
- Includes:
- OneDGraphs, Tablize, LazyEnumerable
- Defined in:
- lib/graphene/result_set.rb
Overview
A generic class for Graphene stat result sets, which implements Enumerable and LazyEnumerable. Calculations are performed lazily, so ResultSet objects are cheap to create, but not necessarily cheap to use.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
The attribute(s) that are being statted, passed in the constructor.
-
#options ⇒ Object
readonly
The options Hash passed in the constructor.
-
#resources ⇒ Object
readonly
The original array of objects from which the stats were generated.
Instance Method Summary collapse
-
#initialize(resources, *args) ⇒ ResultSet
constructor
Accepts an array of objects, and an unlimited number of method symbols (which sould be methods in the objects).
-
#max_result ⇒ Object
Returns the maximum result.
-
#over(over_x, filler = nil, &filler_block) ⇒ Object
Calculates the resources, gropuing them by "over_x", which can be a method symbol or lambda.
-
#to_s ⇒ Object
Returns a string representation of the results.
Methods included from OneDGraphs
#bar_chart, #pie_chart, #side_bar_chart, #side_stacked_bar_chart, #spider_chart, #stacked_bar_chart
Methods included from Tablize
Methods included from LazyEnumerable
Constructor Details
#initialize(resources, *args) ⇒ ResultSet
Accepts an array of objects, and an unlimited number of method symbols (which sould be methods in the objects). Optionally accepts an options hash as a last argument.
Implements Enumerable, so the results can be accessed by any of those methods, including each and to_a.
20 21 22 23 24 25 |
# File 'lib/graphene/result_set.rb', line 20 def initialize(resources, *args) @options = args.last.is_a?(Hash) ? args.pop : {} @attributes = args @resources = resources @results = [] end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
The attribute(s) that are being statted, passed in the constructor
12 13 14 |
# File 'lib/graphene/result_set.rb', line 12 def attributes @attributes end |
#options ⇒ Object (readonly)
The options Hash passed in the constructor
10 11 12 |
# File 'lib/graphene/result_set.rb', line 10 def @options end |
#resources ⇒ Object (readonly)
The original array of objects from which the stats were generated
14 15 16 |
# File 'lib/graphene/result_set.rb', line 14 def resources @resources end |
Instance Method Details
#max_result ⇒ Object
Returns the maximum result
52 53 54 55 |
# File 'lib/graphene/result_set.rb', line 52 def max_result x = attributes.size sort_by { |result| result[x] }.last[x] end |
#over(over_x, filler = nil, &filler_block) ⇒ Object
Calculates the resources, gropuing them by "over_x", which can be a method symbol or lambda
Example by date
Graphene.percentages(logs, :browser).over(:date)
=> 2012-07-22> => [["Firefox", 45], ["Chrome", 40], ["Internet Explorer", 15]],
#
If your X objects can be put into a range, Graphene will attempt to fill in any gaps along the X axis. But if they can't be ranged (e.g. formatted strings), you have several options to fill them in yourself:
Graphene.percentages(logs, :browser).over(->(l) { l.date.strftime('%B %Y') }, [an array of all month/year in the logs]) Graphene.percentages(logs, :browser).over(->(l) { l.date.strftime('%B %Y') }) { |str| somehow parse the month/year and return the next one }
42 43 44 |
# File 'lib/graphene/result_set.rb', line 42 def over(over_x, filler=nil, &filler_block) OverX.new(self, over_x, filler, &filler_block) end |
#to_s ⇒ Object
Returns a string representation of the results
47 48 49 |
# File 'lib/graphene/result_set.rb', line 47 def to_s to_a.to_s end |