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

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



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

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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

Class Method Details

.exist?(base_path) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/data_package/package.rb', line 72

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

.full_path(base_path) ⇒ Object



76
77
78
# File 'lib/data_package/package.rb', line 76

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

.init(base_path, name) ⇒ Object



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

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



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/data_package/package.rb', line 87

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, Yajl::Parser.parse(file))
  else
    raise "Couldn't find datapackage.json at #{path}"
  end
end

Instance Method Details

#contributors=(json) ⇒ Object



57
58
59
# File 'lib/data_package/package.rb', line 57

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

#licenses=(json) ⇒ Object



49
50
51
# File 'lib/data_package/package.rb', line 49

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

#maintainers=(json) ⇒ Object



53
54
55
# File 'lib/data_package/package.rb', line 53

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

#resources=(json) ⇒ Object



61
62
63
# File 'lib/data_package/package.rb', line 61

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

#saveObject



39
40
41
42
43
# File 'lib/data_package/package.rb', line 39

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

#sources=(json) ⇒ Object



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

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