Class: GemDating::Result
- Inherits:
-
Object
- Object
- GemDating::Result
- Defined in:
- lib/gem_dating/result.rb
Instance Attribute Summary collapse
-
#specs ⇒ Object
readonly
Returns the value of attribute specs.
Instance Method Summary collapse
-
#initialize(specs) ⇒ Result
constructor
A new instance of Result.
- #older_than(date) ⇒ Object
- #sort(options = {}) ⇒ Object
- #table_print ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(specs) ⇒ Result
Returns a new instance of Result.
7 8 9 |
# File 'lib/gem_dating/result.rb', line 7 def initialize(specs) @specs = specs end |
Instance Attribute Details
#specs ⇒ Object (readonly)
Returns the value of attribute specs.
6 7 8 |
# File 'lib/gem_dating/result.rb', line 6 def specs @specs end |
Instance Method Details
#older_than(date) ⇒ Object
33 34 35 36 |
# File 'lib/gem_dating/result.rb', line 33 def older_than(date) specs.select! { |spec| spec.date.to_date < cut_off(date) } self end |
#sort(options = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gem_dating/result.rb', line 38 def sort( = {}) field = [:sort_by] || "name" direction = [:order] || "asc" @specs = @specs.sort_by do |spec| case field when "name" spec.name.downcase when "date" spec.date else spec.name.downcase end end @specs = @specs.reverse if direction.downcase == "desc" self end |
#table_print ⇒ Object
25 26 27 |
# File 'lib/gem_dating/result.rb', line 25 def table_print TablePrint::Printer.table_print(specs, [:name, :version, {date: {time_format: "%Y-%m-%d", width: 10}}]).encode("utf-8") end |
#to_a ⇒ Object
11 12 13 |
# File 'lib/gem_dating/result.rb', line 11 def to_a specs end |
#to_h ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/gem_dating/result.rb', line 15 def to_h specs.each_with_object({}) do |spec, result| result[spec.name] = { "name" => spec.name, "version" => spec.version.to_s, "date" => spec.date.strftime("%Y-%m-%d") } end end |
#to_json ⇒ Object
29 30 31 |
# File 'lib/gem_dating/result.rb', line 29 def to_json JSON.generate(to_h) end |