Class: DataPackage::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/data_package/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttrHelper::Serialization

#to_hash, #to_json

Methods included from AttrHelper

#attr_missing?, #attr_present?, #attr_required?, #attributes, included, #missing_attributes, #optional_attributes, #required_attributes, #write_attribute, #write_attributes

Constructor Details

#initialize(base_path, attrs = {}) ⇒ Resource

Returns a new instance of Resource.



28
29
30
31
# File 'lib/data_package/resource.rb', line 28

def initialize(base_path, attrs = {})
  @base_path = base_path
  super(attrs)
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



26
27
28
# File 'lib/data_package/resource.rb', line 26

def base_path
  @base_path
end

Instance Method Details

#dialect=(json) ⇒ Object



37
38
39
# File 'lib/data_package/resource.rb', line 37

def dialect=(json)
  @dialect = Dialect.new(json)
end

#each_row(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/data_package/resource.rb', line 41

def each_row(&block)
  case data_source_type
  when :data
    data.each(&block)
  when :path
    case format
    when 'csv'
      DataKit::CSV::Parser.new(file_path).each_row(&block)
    else
      raise "Unrecognized resource format #{format} for resource #{name}."
    end
  when :url
    raise "URL based resources are not yet supported"
  else
    raise "Resources require one of data, path or url keys to be specified"
  end
end

#file_pathObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/data_package/resource.rb', line 59

def file_path
  case data_source_type
  when :data
    nil # do something here
  when :path
    nil # do something here
  when :url
    File.join(base_path, path)
  else
    raise "Resources require one of data, path or url keys to be specified"
  end
end

#schema=(json) ⇒ Object



33
34
35
# File 'lib/data_package/resource.rb', line 33

def schema=(json)
  @schema = Schema.new(json)
end