Class: DataPackage::Resource

Inherits:
Base
  • Object
show all
Includes:
Enumerable
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::Base

#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.



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

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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

Instance Method Details

#data_source_typeObject



70
71
72
73
74
75
76
77
78
# File 'lib/data_package/resource.rb', line 70

def data_source_type
  if data
    :data
  elsif path
    :path
  elsif url
    :url
  end
end

#dialect=(json) ⇒ Object



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

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

#each(&block) ⇒ Object

Future Note:

For remote resources we should have a path *and* a url
If the path is a file on disk then use it, otherwise assume
that the path is a pointer to the destination of the URL data
this makes it possible for scripts to rely on a file location


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/data_package/resource.rb', line 50

def each(&block)
  case data_source_type
  when :data
    data.each(&block)
  when :path
    file_path = File.join(base_path, 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

#schema=(json) ⇒ Object



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

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