gearbox 0.1.0 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/Gemfile +5 -3
  2. data/Rakefile +1 -0
  3. data/VERSION +1 -1
  4. data/bin/gearbox +9 -0
  5. data/gearbox.gemspec +134 -0
  6. data/lib/gearbox.rb +124 -5
  7. data/lib/gearbox/attribute.rb +128 -0
  8. data/lib/gearbox/mixins/active_model_implementation.rb +27 -0
  9. data/lib/gearbox/mixins/resource.rb +20 -4
  10. data/lib/gearbox/mixins/semantic_accessors.rb +128 -89
  11. data/lib/gearbox/mixins/subject_methods.rb +88 -0
  12. data/lib/gearbox/rdf_collection.rb +22 -8
  13. data/lib/gearbox/types.rb +9 -8
  14. data/lib/gearbox/vocabulary.rb +149 -0
  15. data/lib/pry_utilities.rb +71 -0
  16. data/scratch/4s.rb +335 -0
  17. data/scratch/DEVELOPMENT_NOTES.md +85 -0
  18. data/scratch/actionable.md +34 -0
  19. data/scratch/ccrdf.html-rdfa.nq +100 -0
  20. data/scratch/foo.rb +17 -0
  21. data/scratch/index.rdf +7932 -0
  22. data/scratch/j2.rb +10 -0
  23. data/scratch/junk.rb +16 -0
  24. data/scratch/out.rb +67 -0
  25. data/spec/gearbox/attribute_spec.rb +455 -0
  26. data/spec/gearbox/mixins/active_model_implementation_spec.rb +18 -0
  27. data/spec/gearbox/mixins/ad_hoc_properties_spec.rb +44 -44
  28. data/spec/gearbox/mixins/resource_spec.rb +47 -8
  29. data/spec/gearbox/mixins/semantic_accessors_spec.rb +72 -43
  30. data/spec/gearbox/mixins/subject_methods_spec.rb +126 -0
  31. data/spec/gearbox/rdf_collection_spec.rb +28 -2
  32. data/spec/gearbox_spec.rb +6 -2
  33. data/spec/spec_helper.rb +1 -0
  34. metadata +150 -42
  35. data/Gemfile.lock +0 -138
  36. data/lib/examples/audience.rb +0 -24
  37. data/lib/examples/person.rb +0 -29
  38. data/lib/examples/reference.rb +0 -38
  39. data/lib/examples/theme.rb +0 -8
  40. data/spec/examples/audience_spec.rb +0 -28
  41. data/spec/examples/person_spec.rb +0 -45
  42. data/spec/examples/reference_spec.rb +0 -43
  43. data/spec/examples/theme_spec.rb +0 -137
data/Gemfile CHANGED
@@ -1,8 +1,10 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
3
  gem 'linkeddata'
4
- gem 'rspec'
5
- gem 'uuid'
4
+ gem 'sparql'
5
+ gem 'equivalent-xml'
6
+ gem 'activemodel'
7
+ gem "pry"
6
8
 
7
9
  if RUBY_PLATFORM =~ /darwin/i
8
10
  gem 'autotest-fsevent'
@@ -18,4 +20,4 @@ group :development do
18
20
  gem 'guard-markdown'
19
21
  gem "ruby-debug19"
20
22
  gem 'guard-minitest', :git => 'git://github.com/aspiers/guard-minitest.git'
21
- end
23
+ end
data/Rakefile CHANGED
@@ -21,6 +21,7 @@ Jeweler::Tasks.new do |gem|
21
21
  gem.description = %Q{A SPARQL-driven modeling toolset for semantic models.}
22
22
  gem.email = "davidlamontrichards@gmail.com"
23
23
  gem.authors = ["David Richards"]
24
+ gem.executables = %w(gearbox)
24
25
  # dependencies defined in Gemfile
25
26
  end
26
27
  Jeweler::RubygemsDotOrgTasks.new
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.10
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby -wKU
2
+ require 'yaml'
3
+
4
+ version = File.read(File.join(File.dirname(__FILE__), %w(.. VERSION)))
5
+ gearbox = File.join(File.dirname(__FILE__), %w(.. lib gearbox))
6
+ utilities = File.join(File.dirname(__FILE__), %w(.. lib pry_utilities))
7
+ libs = " -r #{gearbox} -r #{utilities}"
8
+ puts "Loading Gearbox version: #{version}"
9
+ exec "pry #{libs}"
@@ -0,0 +1,134 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gearbox"
8
+ s.version = "0.1.10"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["David Richards"]
12
+ s.date = "2012-04-03"
13
+ s.description = "A SPARQL-driven modeling toolset for semantic models."
14
+ s.email = "davidlamontrichards@gmail.com"
15
+ s.executables = ["gearbox"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.html",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Guardfile",
25
+ "LICENSE.txt",
26
+ "README.html",
27
+ "README.md",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "bin/gearbox",
31
+ "gearbox.gemspec",
32
+ "lib/gearbox.rb",
33
+ "lib/gearbox/attribute.rb",
34
+ "lib/gearbox/attribute_collection.rb",
35
+ "lib/gearbox/mixins/active_model_implementation.rb",
36
+ "lib/gearbox/mixins/ad_hoc_properties.rb",
37
+ "lib/gearbox/mixins/resource.rb",
38
+ "lib/gearbox/mixins/semantic_accessors.rb",
39
+ "lib/gearbox/mixins/subject_methods.rb",
40
+ "lib/gearbox/rdf_collection.rb",
41
+ "lib/gearbox/type.rb",
42
+ "lib/gearbox/types.rb",
43
+ "lib/gearbox/types/any.rb",
44
+ "lib/gearbox/types/boolean.rb",
45
+ "lib/gearbox/types/date.rb",
46
+ "lib/gearbox/types/decimal.rb",
47
+ "lib/gearbox/types/float.rb",
48
+ "lib/gearbox/types/integer.rb",
49
+ "lib/gearbox/types/native.rb",
50
+ "lib/gearbox/types/string.rb",
51
+ "lib/gearbox/types/uri.rb",
52
+ "lib/gearbox/vocabulary.rb",
53
+ "lib/pry_utilities.rb",
54
+ "scratch/4s.rb",
55
+ "scratch/DEVELOPMENT_NOTES.md",
56
+ "scratch/actionable.md",
57
+ "scratch/ccrdf.html-rdfa.nq",
58
+ "scratch/foo.rb",
59
+ "scratch/index.rdf",
60
+ "scratch/j2.rb",
61
+ "scratch/junk.rb",
62
+ "scratch/out.rb",
63
+ "spec/gearbox/attribute_collection_spec.rb",
64
+ "spec/gearbox/attribute_spec.rb",
65
+ "spec/gearbox/mixins/active_model_implementation_spec.rb",
66
+ "spec/gearbox/mixins/ad_hoc_properties_spec.rb",
67
+ "spec/gearbox/mixins/resource_spec.rb",
68
+ "spec/gearbox/mixins/semantic_accessors_spec.rb",
69
+ "spec/gearbox/mixins/subject_methods_spec.rb",
70
+ "spec/gearbox/rdf_collection_spec.rb",
71
+ "spec/gearbox_spec.rb",
72
+ "spec/spec_helper.rb"
73
+ ]
74
+ s.homepage = "http://github.com/davidrichards/gearbox"
75
+ s.licenses = ["MIT"]
76
+ s.require_paths = ["lib"]
77
+ s.rubygems_version = "1.8.18"
78
+ s.summary = "Flexible semantic models."
79
+
80
+ if s.respond_to? :specification_version then
81
+ s.specification_version = 3
82
+
83
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
84
+ s.add_runtime_dependency(%q<linkeddata>, [">= 0"])
85
+ s.add_runtime_dependency(%q<sparql>, [">= 0"])
86
+ s.add_runtime_dependency(%q<equivalent-xml>, [">= 0"])
87
+ s.add_runtime_dependency(%q<activemodel>, [">= 0"])
88
+ s.add_runtime_dependency(%q<pry>, [">= 0"])
89
+ s.add_runtime_dependency(%q<autotest-fsevent>, [">= 0"])
90
+ s.add_runtime_dependency(%q<autotest-growl>, [">= 0"])
91
+ s.add_development_dependency(%q<minitest>, [">= 0"])
92
+ s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
93
+ s.add_development_dependency(%q<bundler>, [">= 0"])
94
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
95
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
96
+ s.add_development_dependency(%q<guard-markdown>, [">= 0"])
97
+ s.add_development_dependency(%q<ruby-debug19>, [">= 0"])
98
+ s.add_development_dependency(%q<guard-minitest>, [">= 0"])
99
+ else
100
+ s.add_dependency(%q<linkeddata>, [">= 0"])
101
+ s.add_dependency(%q<sparql>, [">= 0"])
102
+ s.add_dependency(%q<equivalent-xml>, [">= 0"])
103
+ s.add_dependency(%q<activemodel>, [">= 0"])
104
+ s.add_dependency(%q<pry>, [">= 0"])
105
+ s.add_dependency(%q<autotest-fsevent>, [">= 0"])
106
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
107
+ s.add_dependency(%q<minitest>, [">= 0"])
108
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
109
+ s.add_dependency(%q<bundler>, [">= 0"])
110
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
111
+ s.add_dependency(%q<simplecov>, [">= 0"])
112
+ s.add_dependency(%q<guard-markdown>, [">= 0"])
113
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
114
+ s.add_dependency(%q<guard-minitest>, [">= 0"])
115
+ end
116
+ else
117
+ s.add_dependency(%q<linkeddata>, [">= 0"])
118
+ s.add_dependency(%q<sparql>, [">= 0"])
119
+ s.add_dependency(%q<equivalent-xml>, [">= 0"])
120
+ s.add_dependency(%q<activemodel>, [">= 0"])
121
+ s.add_dependency(%q<pry>, [">= 0"])
122
+ s.add_dependency(%q<autotest-fsevent>, [">= 0"])
123
+ s.add_dependency(%q<autotest-growl>, [">= 0"])
124
+ s.add_dependency(%q<minitest>, [">= 0"])
125
+ s.add_dependency(%q<yard>, ["~> 0.6.0"])
126
+ s.add_dependency(%q<bundler>, [">= 0"])
127
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
128
+ s.add_dependency(%q<simplecov>, [">= 0"])
129
+ s.add_dependency(%q<guard-markdown>, [">= 0"])
130
+ s.add_dependency(%q<ruby-debug19>, [">= 0"])
131
+ s.add_dependency(%q<guard-minitest>, [">= 0"])
132
+ end
133
+ end
134
+
@@ -1,12 +1,112 @@
1
1
  # ================
2
2
  # = Dependencies =
3
3
  # ================
4
- require 'uuid'
5
4
  require 'linkeddata'
6
5
  require 'ostruct'
6
+ require 'active_model'
7
7
 
8
8
  module Gearbox
9
9
 
10
+ # =======================
11
+ # = Settings from Spira =
12
+ # =======================
13
+ ##
14
+ # The list of repositories available for Gearbox resources
15
+ #
16
+ # @see http://rdf.rubyforge.org/RDF/Repository.html
17
+ # @return [Hash{Symbol => RDF::Repository}]
18
+ # @private
19
+ def repositories
20
+ settings[:repositories] ||= {}
21
+ end
22
+ module_function :repositories
23
+
24
+ ##
25
+ # The list of all property types available for Gearbox resources
26
+ #
27
+ # @see Gearbox::Types
28
+ # @return [Hash{Symbol => Gearbox::Type}]
29
+ def types
30
+ settings[:types] ||= {}
31
+ end
32
+ module_function :types
33
+
34
+ ##
35
+ # A thread-local hash for storing settings. Used by Resource classes.
36
+ #
37
+ # @see Gearbox::Resource
38
+ # @see Gearbox.repositories
39
+ # @see Gearbox.types
40
+ def settings
41
+ Thread.current[:gearbox] ||= {}
42
+ end
43
+ module_function :settings
44
+
45
+ ##
46
+ # Add a repository to Gearbox's list of repositories.
47
+ #
48
+ # @overload add_repository(name, repo)
49
+ # @param [Symbol] name The name of this repository
50
+ # @param [RDF::Repository] repo An RDF::Repository
51
+ # @overload add_repository(name, klass, *args)
52
+ # @param [Symbol] name The name of this repository
53
+ # @param [RDF::Repository, Class] repo A Class that inherits from RDF::Repository
54
+ # @param [*Object] The list of arguments to instantiate the class
55
+ # @example Adding an ntriples file as a repository
56
+ # Gearbox.add_repository(:default, RDF::Repository.load('http://datagraph.org/jhacker/foaf.nt'))
57
+ # @example Adding an empty repository to be instantiated on use
58
+ # Gearbox.add_repository(:default, RDF::Repository)
59
+ # @return [Void]
60
+ # @see RDF::Repository
61
+ def add_repository(name, klass, *args)
62
+ repositories[name] = case klass
63
+ when Class
64
+ promise { klass.new(*args) }
65
+ else
66
+ klass
67
+ end
68
+ if (name == :default) && settings[:repositories][name].nil?
69
+ warn "WARNING: Adding nil default repository"
70
+ end
71
+ end
72
+ alias_method :add_repository!, :add_repository
73
+ module_function :add_repository, :add_repository!
74
+
75
+ ##
76
+ # The RDF::Repository for the named repository
77
+ #
78
+ # @param [Symbol] name The name of the repository
79
+ # @return [RDF::Repository]
80
+ # @see RDF::Repository
81
+ def repository(name)
82
+ repositories[name]
83
+ end
84
+ module_function :repository
85
+
86
+ ##
87
+ # Clear all repositories from Gearbox's knowledge. Use it if you want, but
88
+ # it's really here for testing.
89
+ #
90
+ # @return [Void]
91
+ # @private
92
+ def clear_repositories!
93
+ settings[:repositories] = {}
94
+ end
95
+ module_function :clear_repositories!
96
+
97
+ ##
98
+ # Alias a property type to another. This allows a range of options to be
99
+ # specified for a property type which all reference one Gearbox::Type
100
+ #
101
+ # @param [Any] new The new symbol or reference
102
+ # @param [Any] original The type the new symbol should refer to
103
+ # @return [Void]
104
+ # @private
105
+ def type_alias(new, original)
106
+ types[new] = original
107
+ end
108
+ module_function :type_alias
109
+
10
110
  # ========================
11
111
  # = Helper Utility: path =
12
112
  # ========================
@@ -20,21 +120,40 @@ module Gearbox
20
120
  # =======================
21
121
  # = Loading the Library =
22
122
  # =======================
123
+ require path('type')
124
+ require path('types')
125
+ autoload :Attribute, path('attribute')
23
126
  autoload :AttributeCollection, path('attribute_collection')
24
127
  autoload :RDFCollection, path('rdf_collection')
25
128
 
26
129
  autoload :AdHocProperties, path('mixins/ad_hoc_properties')
130
+ autoload :ActiveModelImplementation, path('mixins/active_model_implementation')
131
+ autoload :AttributeMethods, path('mixins/attribute_methods')
27
132
  autoload :Resource, path('mixins/resource')
28
133
  autoload :SemanticAccessors, path('mixins/semantic_accessors')
134
+ autoload :SubjectMethods, path('mixins/subject_methods')
29
135
 
136
+ end
137
+
138
+ module Example
139
+ # ========================
140
+ # = Helper Utility: path =
141
+ # ========================
142
+ # @private
143
+ def path(path)
144
+ File.expand_path("../examples/#{path}", __FILE__)
145
+ end
146
+ private :path
147
+ module_function :path
148
+
30
149
  # ============
31
150
  # = Examples =
32
151
  # ============
33
152
 
34
153
  # Will separate these after things get to a solid 0.1 state.
35
- autoload :Audience, path('../examples/audience')
36
- autoload :Person, path('../examples/person')
37
- autoload :Reference, path('../examples/reference')
38
- autoload :Theme, path('../examples/theme')
154
+ autoload :Audience, path('audience')
155
+ autoload :Person, path('person')
156
+ autoload :Reference, path('reference')
157
+ autoload :Theme, path('theme')
39
158
 
40
159
  end
@@ -0,0 +1,128 @@
1
+ module Gearbox
2
+
3
+ class Attribute
4
+
5
+ # Options to figure out: more index options, routing, triple pattern
6
+ attr_accessor :predicate, :reverse, :index, :name, :repository, :value, :language, :datatype
7
+ attr_reader :subject_decorator
8
+
9
+ def initialize(opts={})
10
+ set(opts.delete(:value) || :_value_not_set)
11
+ assert_defaults
12
+ extract_from_statement(opts) if opts.has_key?(:statement)
13
+ assert_options(opts)
14
+ end
15
+
16
+ def subject_decorator=(value=nil, &block)
17
+ @subject_decorator = block_given? ? block : value
18
+ end
19
+
20
+ # TODO: I'm a little quesy about the relationship between the model class, the model, and the attribute.
21
+ # I'm going to have to rebuild the model DSL and see how this stuff gets built.
22
+ def subject
23
+ return nil unless subject_decorator
24
+ if subject_decorator.respond_to?(:call)
25
+ subject_decorator.call()
26
+ elsif respond_to?(subject_decorator)
27
+ self.send(subject_decorator)
28
+ else
29
+ nil
30
+ end
31
+ end
32
+
33
+ def to_value
34
+ # Skipping the serialization steps for a moment.
35
+ @value
36
+ end
37
+ alias :get :to_value
38
+
39
+ def literal(opts={:value => :_value_not_set})
40
+ value = opts.delete(:value)
41
+ value = to_value if value == :_value_not_set
42
+ opts = {:language => self.language, :datatype => self.datatype}.merge(opts)
43
+ value = case opts[:datatype]
44
+ when :boolean
45
+ RDF::Literal::Boolean.new(value)
46
+ when :date
47
+ RDF::Literal::Date.new(value)
48
+ when :date_time
49
+ RDF::Literal::DateTime.new(value)
50
+ when :decimal
51
+ RDF::Literal::Decimal.new(value)
52
+ when :double
53
+ RDF::Literal::Double.new(value)
54
+ when :time
55
+ RDF::Literal::Time.new(value)
56
+ when :token
57
+ RDF::Literal::Token.new(value)
58
+ when :xml
59
+ RDF::Literal::XML.new(value)
60
+ else
61
+ value
62
+ end
63
+
64
+ RDF::Literal.new(value, opts)
65
+ end
66
+
67
+ def set(value=:_value_not_set, opts={})
68
+ opts = {:value => value}.merge(opts)
69
+ @value = literal(opts).object
70
+ end
71
+
72
+ def to_rdf(model, opts={})
73
+ opts[:value] = :_value_not_set unless opts.has_key?(:value)
74
+ opts = {
75
+ :reverse => self.reverse,
76
+ :predicate => self.predicate,
77
+ :value => self.literal(opts)
78
+ }.merge(opts)
79
+
80
+ subject = self.subject
81
+ subject ||= model.subject
82
+ subject = RDF::URI.new(subject)
83
+ predicate = opts[:predicate]
84
+ object = opts[:value]
85
+
86
+ return opts[:reverse] ? RDF::Statement(object, predicate, subject)
87
+ : RDF::Statement(subject, predicate, object)
88
+ end
89
+
90
+ private
91
+
92
+ def assert_defaults
93
+ self.index = false
94
+ self.reverse = false
95
+ end
96
+
97
+ def assert_options(opts)
98
+ opts.each do |accessor, value|
99
+ send("#{accessor}=", value) if respond_to?("#{accessor}=")
100
+ end
101
+ end
102
+
103
+ def extract_from_statement(opts)
104
+ statement = opts[:statement]
105
+ object = statement.object
106
+
107
+ self.reverse = can_infer_reverse_from_model?(opts[:model], object)
108
+ object = statement.subject if self.reverse
109
+
110
+ self.predicate = statement.predicate
111
+ self.set extract_from_literal(object)
112
+ end
113
+
114
+ def extract_from_literal(literal)
115
+ return literal unless literal.is_a?(RDF::Literal)
116
+ self.language = literal.language
117
+ self.datatype = literal.datatype
118
+ literal.object
119
+ end
120
+
121
+ def can_infer_reverse_from_model?(model, object)
122
+ return false unless model
123
+ model.subject.to_s == object.to_s
124
+ end
125
+ end
126
+
127
+ end
128
+