Class: Datasets::Ggplot2Dataset

Inherits:
Dataset
  • Object
show all
Defined in:
lib/datasets/ggplot2-dataset.rb

Direct Known Subclasses

Diamonds, FuelEconomy

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(ggplot2_dataset_name) ⇒ Ggplot2Dataset

Returns a new instance of Ggplot2Dataset.



3
4
5
6
7
8
9
10
11
# File 'lib/datasets/ggplot2-dataset.rb', line 3

def initialize(ggplot2_dataset_name)
  super()
  @ggplot2_dataset_name = ggplot2_dataset_name
  @metadata.url =
    "https://ggplot2.tidyverse.org/reference/#{@ggplot2_dataset_name}.html"
  @metadata.description = lambda do
    fetch_description
  end
end

Instance Method Details

#eachObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datasets/ggplot2-dataset.rb', line 13

def each
  return to_enum(__method__) unless block_given?

  data_base_name = "#{@ggplot2_dataset_name}.csv"
  data_path = cache_dir_path + data_base_name
  data_url = "#{download_base_url}/data-raw/#{data_base_name}"
  download(data_path, data_url)
  CSV.open(data_path, headers: :first_row, converters: :all) do |csv|
    record_class = self.class::Record
    csv.each do |row|
      record = record_class.new(*row.fields)
      yield record
    end
  end
end