data_package 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/data_package.gemspec +0 -1
- data/lib/attr_helper/base.rb +118 -0
- data/lib/attr_helper/serialization.rb +2 -1
- data/lib/data_package/base.rb +1 -1
- data/lib/data_package/helpers.rb +21 -0
- data/lib/data_package/resource.rb +8 -1
- data/lib/data_package/version.rb +1 -1
- data/lib/data_package.rb +5 -4
- data/spec/klass_helper.rb +1 -1
- metadata +4 -17
- data/lib/attr_helper.rb +0 -115
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9bb5f38b73d7574924c6cc18fe28506566d61228
|
4
|
+
data.tar.gz: b6b2bcd9ab3f67cf3a07422b1f41211c4dfab8da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7322ef232c56454c1d87115f052b051d5744c09a82bb0bd8c96ac8ca61dbc6d6f4cb052cc293f293e4dce6f4dde7ae75bee8a4bfa369ccef6f5e9883ebdeb2a0
|
7
|
+
data.tar.gz: 4c9c3b153fe66838671028afc92126f819a4190e4e46720f1ae901631768f3aeb635c0b2e290d0daa8175b87a841e99d244f6b0791e502c3b96c7d06e8cb29d0
|
data/data_package.gemspec
CHANGED
@@ -22,7 +22,6 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_runtime_dependency 'rcsv'
|
23
23
|
spec.add_runtime_dependency 'yajl-ruby'
|
24
24
|
spec.add_runtime_dependency 'data_kit'
|
25
|
-
spec.add_runtime_dependency 'active_support'
|
26
25
|
|
27
26
|
# Development Dependencies
|
28
27
|
spec.add_development_dependency "bundler", "~> 1.3"
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'attr_helper/base_attr'
|
2
|
+
require 'attr_helper/required_attr'
|
3
|
+
require 'attr_helper/serialization'
|
4
|
+
|
5
|
+
module AttrHelper
|
6
|
+
module Base
|
7
|
+
def self.included(klass)
|
8
|
+
klass.send :extend, ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def inherited(klass)
|
13
|
+
super
|
14
|
+
|
15
|
+
# we might want to undef here so
|
16
|
+
# that child classes can override
|
17
|
+
|
18
|
+
unless required_attributes.empty?
|
19
|
+
add_required_attrs(klass)
|
20
|
+
end
|
21
|
+
|
22
|
+
unless optional_attributes.empty?
|
23
|
+
add_optional_attrs(klass)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def attr_optional(name, options = {})
|
28
|
+
optional_attributes << BaseAttr.new(name.to_sym, options)
|
29
|
+
attr_accessor name.to_sym
|
30
|
+
end
|
31
|
+
|
32
|
+
def optional_attributes
|
33
|
+
@optional_attributes ||= []
|
34
|
+
end
|
35
|
+
|
36
|
+
def attr_required(name, options = {})
|
37
|
+
required_attributes << RequiredAttr.new(name.to_sym, options)
|
38
|
+
attr_accessor name.to_sym
|
39
|
+
end
|
40
|
+
|
41
|
+
def required_attributes
|
42
|
+
@required_attributes ||= []
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def add_required_attrs(klass)
|
48
|
+
required_attributes.each do |attribute|
|
49
|
+
klass.attr_required attribute.name, {
|
50
|
+
:name => attribute.name,
|
51
|
+
:key => attribute.key,
|
52
|
+
:default => attribute.default,
|
53
|
+
:serialize => attribute.serialize,
|
54
|
+
:if => attribute.if_cond,
|
55
|
+
:unless => attribute.unless_cond
|
56
|
+
}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_optional_attrs(klass)
|
61
|
+
optional_attributes.each do |attribute|
|
62
|
+
klass.attr_optional attribute.name, {
|
63
|
+
:name => attribute.name,
|
64
|
+
:key => attribute.key,
|
65
|
+
:default => attribute.default,
|
66
|
+
:serialize => attribute.serialize
|
67
|
+
}
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def attributes
|
73
|
+
required_attributes + optional_attributes
|
74
|
+
end
|
75
|
+
|
76
|
+
def required_attributes
|
77
|
+
self.class.required_attributes.select do |attribute|
|
78
|
+
attribute.required?(self)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def optional_attributes
|
83
|
+
self.class.optional_attributes
|
84
|
+
end
|
85
|
+
|
86
|
+
def missing_attributes
|
87
|
+
required_attributes.select do |attribute|
|
88
|
+
value = send(attribute.name)
|
89
|
+
value.respond_to?(:empty?) ? value.empty? : value.nil?
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def attr_required?(name)
|
94
|
+
required_attributes.any?{|a| a.name == name}
|
95
|
+
end
|
96
|
+
|
97
|
+
def attr_missing?(name)
|
98
|
+
missing_attributes.any?{|a| a.name == name}
|
99
|
+
end
|
100
|
+
|
101
|
+
def attr_present?(name)
|
102
|
+
!attr_missing?(name)
|
103
|
+
end
|
104
|
+
|
105
|
+
def write_attribute(name, value)
|
106
|
+
self.instance_variable_set("@#{name}", value)
|
107
|
+
end
|
108
|
+
|
109
|
+
def write_attributes(attrs = {})
|
110
|
+
attrs = DataPackage::Helpers.symbolize_keys(attrs)
|
111
|
+
|
112
|
+
attributes.each do |attribute|
|
113
|
+
value = attrs[attribute.key.to_sym] || attribute.default
|
114
|
+
self.send("#{attribute.name}=", value) unless value.nil?
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
data/lib/data_package/base.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module DataPackage
|
2
|
+
module Helpers
|
3
|
+
class << self
|
4
|
+
# http://devblog.avdi.org/2009/07/14/recursively-symbolize-keys/
|
5
|
+
def symbolize_keys(hash)
|
6
|
+
hash.inject({}){|result, (key, value)|
|
7
|
+
new_key = case key
|
8
|
+
when String then key.to_sym
|
9
|
+
else key
|
10
|
+
end
|
11
|
+
new_value = case value
|
12
|
+
when Hash then symbolize_keys(value)
|
13
|
+
else value
|
14
|
+
end
|
15
|
+
result[new_key] = new_value
|
16
|
+
result
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -21,7 +21,6 @@ module DataPackage
|
|
21
21
|
|
22
22
|
attr_optional :size
|
23
23
|
attr_optional :hash
|
24
|
-
attr_optional :last_modified, :key => 'lastModified'
|
25
24
|
|
26
25
|
attr_accessor :base_path
|
27
26
|
|
@@ -38,6 +37,14 @@ module DataPackage
|
|
38
37
|
@dialect = Dialect.new(json)
|
39
38
|
end
|
40
39
|
|
40
|
+
#
|
41
|
+
# Future Note:
|
42
|
+
# For remote resources we should have a path *and* a url
|
43
|
+
# If the path is a file on disk then use it, otherwise assume
|
44
|
+
# that the path is a pointer to the destination of the URL data
|
45
|
+
# this makes it possible for scripts to rely on a file location
|
46
|
+
#
|
47
|
+
|
41
48
|
def each_row(&block)
|
42
49
|
case data_source_type
|
43
50
|
when :data
|
data/lib/data_package/version.rb
CHANGED
data/lib/data_package.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
-
require 'active_support/core_ext/hash'
|
2
|
-
|
3
1
|
require "data_package/version"
|
4
2
|
|
5
3
|
# Attribute Helpers
|
6
|
-
require 'attr_helper'
|
4
|
+
require 'attr_helper/base'
|
5
|
+
|
6
|
+
# Ruby Helpers
|
7
|
+
require 'data_package/helpers'
|
7
8
|
|
8
9
|
# Core Specification
|
9
10
|
require 'data_package/base'
|
@@ -14,4 +15,4 @@ require 'data_package/resource'
|
|
14
15
|
require 'data_package/dialect'
|
15
16
|
require 'data_package/person'
|
16
17
|
require 'data_package/source'
|
17
|
-
require 'data_package/license'
|
18
|
+
require 'data_package/license'
|
data/spec/klass_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_package
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mode Analytics
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rcsv
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: active_support
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - '>='
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - '>='
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: bundler
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,7 +123,7 @@ files:
|
|
137
123
|
- README.md
|
138
124
|
- Rakefile
|
139
125
|
- data_package.gemspec
|
140
|
-
- lib/attr_helper.rb
|
126
|
+
- lib/attr_helper/base.rb
|
141
127
|
- lib/attr_helper/base_attr.rb
|
142
128
|
- lib/attr_helper/required_attr.rb
|
143
129
|
- lib/attr_helper/serialization.rb
|
@@ -145,6 +131,7 @@ files:
|
|
145
131
|
- lib/data_package/base.rb
|
146
132
|
- lib/data_package/dialect.rb
|
147
133
|
- lib/data_package/field.rb
|
134
|
+
- lib/data_package/helpers.rb
|
148
135
|
- lib/data_package/license.rb
|
149
136
|
- lib/data_package/package.rb
|
150
137
|
- lib/data_package/person.rb
|
data/lib/attr_helper.rb
DELETED
@@ -1,115 +0,0 @@
|
|
1
|
-
require 'attr_helper/base_attr'
|
2
|
-
require 'attr_helper/required_attr'
|
3
|
-
require 'attr_helper/serialization'
|
4
|
-
|
5
|
-
module AttrHelper
|
6
|
-
def self.included(klass)
|
7
|
-
klass.send :extend, ClassMethods
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
def inherited(klass)
|
12
|
-
super
|
13
|
-
|
14
|
-
# we might want to undef here so
|
15
|
-
# that child classes can override
|
16
|
-
|
17
|
-
unless required_attributes.empty?
|
18
|
-
add_required_attrs(klass)
|
19
|
-
end
|
20
|
-
|
21
|
-
unless optional_attributes.empty?
|
22
|
-
add_optional_attrs(klass)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def attr_optional(name, options = {})
|
27
|
-
optional_attributes << BaseAttr.new(name.to_sym, options)
|
28
|
-
attr_accessor name.to_sym
|
29
|
-
end
|
30
|
-
|
31
|
-
def optional_attributes
|
32
|
-
@optional_attributes ||= []
|
33
|
-
end
|
34
|
-
|
35
|
-
def attr_required(name, options = {})
|
36
|
-
required_attributes << RequiredAttr.new(name.to_sym, options)
|
37
|
-
attr_accessor name.to_sym
|
38
|
-
end
|
39
|
-
|
40
|
-
def required_attributes
|
41
|
-
@required_attributes ||= []
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def add_required_attrs(klass)
|
47
|
-
required_attributes.each do |attribute|
|
48
|
-
klass.attr_required attribute.name, {
|
49
|
-
:name => attribute.name,
|
50
|
-
:key => attribute.key,
|
51
|
-
:default => attribute.default,
|
52
|
-
:serialize => attribute.serialize,
|
53
|
-
:if => attribute.if_cond,
|
54
|
-
:unless => attribute.unless_cond
|
55
|
-
}
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
def add_optional_attrs(klass)
|
60
|
-
optional_attributes.each do |attribute|
|
61
|
-
klass.attr_optional attribute.name, {
|
62
|
-
:name => attribute.name,
|
63
|
-
:key => attribute.key,
|
64
|
-
:default => attribute.default,
|
65
|
-
:serialize => attribute.serialize
|
66
|
-
}
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def attributes
|
72
|
-
required_attributes + optional_attributes
|
73
|
-
end
|
74
|
-
|
75
|
-
def required_attributes
|
76
|
-
self.class.required_attributes.select do |attribute|
|
77
|
-
attribute.required?(self)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
def optional_attributes
|
82
|
-
self.class.optional_attributes
|
83
|
-
end
|
84
|
-
|
85
|
-
def missing_attributes
|
86
|
-
required_attributes.select do |attribute|
|
87
|
-
value = send(attribute.name)
|
88
|
-
value.respond_to?(:empty?) ? value.empty? : value.nil?
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def attr_required?(name)
|
93
|
-
required_attributes.any?{|a| a.name == name}
|
94
|
-
end
|
95
|
-
|
96
|
-
def attr_missing?(name)
|
97
|
-
missing_attributes.any?{|a| a.name == name}
|
98
|
-
end
|
99
|
-
|
100
|
-
def attr_present?(name)
|
101
|
-
!attr_missing?(name)
|
102
|
-
end
|
103
|
-
|
104
|
-
def write_attribute(name, value)
|
105
|
-
self.instance_variable_set("@#{name}", value)
|
106
|
-
end
|
107
|
-
|
108
|
-
def write_attributes(attrs = {})
|
109
|
-
attrs = attrs.symbolize_keys
|
110
|
-
attributes.each do |attribute|
|
111
|
-
value = attrs[attribute.key.to_sym] || attribute.default
|
112
|
-
self.send("#{attribute.name}=", value) unless value.nil?
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|