Class: DataPackage::Package

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

Instance Attribute Summary collapse

Class Method 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 = {}) ⇒ Package

Returns a new instance of Package.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/data_package/package.rb', line 33

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

  @resources ||= []
  @sources ||= []
  @licenses ||= []
  @maintainters ||= []
  @contributors ||= []

  super(attrs)
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



31
32
33
# File 'lib/data_package/package.rb', line 31

def base_path
  @base_path
end

Class Method Details

.exist?(base_path) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/data_package/package.rb', line 78

def exist?(base_path)
  File.exist?(full_path(base_path))
end

.full_path(base_path) ⇒ Object



82
83
84
# File 'lib/data_package/package.rb', line 82

def full_path(base_path)
  File.join(File.expand_path(base_path), 'datapackage.json')
end

.init(base_path, name) ⇒ Object



86
87
88
89
90
91
# File 'lib/data_package/package.rb', line 86

def init(base_path, name)
  pkg = new(base_path, :name => name, :version => '0.0.1')
  pkg.save

  open(base_path)
end

.open(base_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/data_package/package.rb', line 93

def open(base_path)
  full_path = full_path(base_path)
  base_path = File.expand_path(base_path)

  if File.exist?(full_path)
    file = File.open(full_path)
    new(base_path, JSON.parse(File.read(file)))
  else
    raise "Couldn't find datapackage.json at #{path}"
  end
end

Instance Method Details

#contributors=(json) ⇒ Object



63
64
65
# File 'lib/data_package/package.rb', line 63

def contributors=(json)
  @contributors = json.collect{|c| Person.new(c)}
end

#licenses=(json) ⇒ Object



55
56
57
# File 'lib/data_package/package.rb', line 55

def licenses=(json)
  @licenses = json.collect{|l| License.new(l)}
end

#maintainers=(json) ⇒ Object



59
60
61
# File 'lib/data_package/package.rb', line 59

def maintainers=(json)
  @maintainers = json.collect{|m| Person.new(m)}
end

#resources=(json) ⇒ Object



67
68
69
# File 'lib/data_package/package.rb', line 67

def resources=(json)
  @resources = json.collect{|r| Resource.new(base_path, r)}
end

#saveObject



45
46
47
48
49
# File 'lib/data_package/package.rb', line 45

def save
  File.open(full_path, 'w+') do |file|
    file.write(self.to_json)
  end
end

#sources=(json) ⇒ Object



51
52
53
# File 'lib/data_package/package.rb', line 51

def sources=(json)
  @sources = json.collect{|s| Source.new(s)}
end