Class: GemDating::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_dating/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#specsObject (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(options = {})
  field = options[:sort_by] || "name"
  direction = options[: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_printObject



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_aObject



11
12
13
# File 'lib/gem_dating/result.rb', line 11

def to_a
  specs
end

#to_hObject



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_jsonObject



29
30
31
# File 'lib/gem_dating/result.rb', line 29

def to_json
  JSON.generate(to_h)
end