Class: DataPackage::Package
- Inherits:
-
Hash
- Object
- Hash
- DataPackage::Package
- Defined in:
- lib/datapackage/package.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
- #resources ⇒ Object
Instance Method Summary collapse
-
#base ⇒ Object
Returns the directory for a local file package or base url for a remote Returns nil for an in-memory object (because it has no base as yet).
-
#initialize(package = nil, schema = :base, opts = {}) ⇒ Package
constructor
Parse or create a data package.
-
#local? ⇒ Boolean
Is this a local package? Returns true if created from an in-memory object or a file/directory reference.
- #parse_package(package) ⇒ Object
- #property(property, default = nil) ⇒ Object
- #resource_exists?(location) ⇒ Boolean
- #to_json ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object
Constructor Details
#initialize(package = nil, schema = :base, opts = {}) ⇒ Package
Parse or create a data package
Supports reading data from JSON file, directory, and a URL
- package
-
Hash or a String
- schema
-
Hash, Symbol or String
- opts
-
Options used to customize reading and parsing
15 16 17 18 19 20 21 22 23 |
# File 'lib/datapackage/package.rb', line 15 def initialize(package = nil, schema = :base, opts = {}) @opts = opts @schema = DataPackage::Schema.new(schema || :base) @dead_resources = [] self.merge! parse_package(package) define_properties! load_resources! end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/datapackage/package.rb', line 5 def errors @errors end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
5 6 7 |
# File 'lib/datapackage/package.rb', line 5 def opts @opts end |
#resources ⇒ Object
57 58 59 60 |
# File 'lib/datapackage/package.rb', line 57 def resources update_resources! @resources end |
Instance Method Details
#base ⇒ Object
Returns the directory for a local file package or base url for a remote Returns nil for an in-memory object (because it has no base as yet)
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/datapackage/package.rb', line 38 def base # user can override base return @opts[:base] if @opts[:base] return '' unless @location # work out base directory or uri if local? return File.dirname(@location) else return @location.split('/')[0..-2].join('/') end end |
#local? ⇒ Boolean
Is this a local package? Returns true if created from an in-memory object or a file/directory reference
51 52 53 54 55 |
# File 'lib/datapackage/package.rb', line 51 def local? return @local if @local return !@location.start_with?('http') if @location true end |
#parse_package(package) ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/datapackage/package.rb', line 25 def parse_package(package) # TODO: base directory/url if package.nil? {} elsif package.class == Hash package else read_package(package) end end |
#property(property, default = nil) ⇒ Object
62 63 64 |
# File 'lib/datapackage/package.rb', line 62 def property(property, default = nil) self[property] || default end |
#resource_exists?(location) ⇒ Boolean
76 77 78 |
# File 'lib/datapackage/package.rb', line 76 def resource_exists?(location) @dead_resources.include?(location) end |
#to_json ⇒ Object
80 81 82 |
# File 'lib/datapackage/package.rb', line 80 def to_json self.to_json end |
#valid? ⇒ Boolean
66 67 68 69 |
# File 'lib/datapackage/package.rb', line 66 def valid? validate @valid end |
#validate ⇒ Object
71 72 73 74 |
# File 'lib/datapackage/package.rb', line 71 def validate @errors = @schema.validation_errors(self) @valid = @schema.valid?(self) end |