acts_as_rdf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .idea
2
+ pkg
3
+ doc
4
+ .yardoc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,57 @@
1
+ = Acts As RDF
2
+
3
+ This acts_as extension provides the capabilities for serving RDF.
4
+
5
+
6
+ == Install
7
+
8
+ === Rails 3
9
+
10
+ Specify it in your Rails Gemfile:
11
+
12
+ gem "acts_as_rdf"
13
+
14
+ Then install it:
15
+
16
+ bundle install
17
+
18
+ === Rails 2
19
+
20
+ Specify it in your Rails config:
21
+
22
+ config.gem "acts_as_rdf", "~>0.1"
23
+
24
+ Then install it:
25
+
26
+ rake gems:install
27
+
28
+
29
+ == Example
30
+
31
+ class Item < ActiveRecord::Base
32
+ acts_as_rdf
33
+ end
34
+
35
+ item.to_rdf
36
+ item.to_ttl
37
+ item.to_ntriples
38
+
39
+ == Bugs and Feedback
40
+
41
+ If you discover any bugs or want to drop a line, feel free to create an issue on GitHub:
42
+ http://github.com/rails/acts_as_rdf/issues
43
+
44
+ === Note on Patches/Pull Requests
45
+
46
+ * Fork the project.
47
+ * Make your feature addition or bug fix.
48
+ * Add tests for it. This is important so I don't break it in a
49
+ future version unintentionally.
50
+ * Commit, do not mess with rakefile, version, or history.
51
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
52
+ * Send me a pull request. Bonus points for topic branches.
53
+
54
+
55
+ == Credits
56
+
57
+ Copyright (c) 2010 Andrew Sodt, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_rdf"
8
+ gem.summary = %Q{Gem version of acts_as_rdf Rails plugin}
9
+ gem.description = %Q{Gem version of acts_as_rdf Rails plugin}
10
+ gem.email = "andrews@alleninstitute.org"
11
+ gem.homepage = "http://github.com/rails/acts_as_rdf"
12
+ gem.authors = ["Andrew Sodt"]
13
+ gem.add_dependency "activerecord", ">= 3.0.0"
14
+ gem.add_dependency "rdf", ">= 0.2.3"
15
+ gem.add_dependency "rdf-raptor", ">= 0.4.0"
16
+ gem.add_development_dependency "yard"
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'yard'
48
+ YARD::Rake::YardocTask.new do |t|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+ t.options += ['--title', "acts_as_rdf #{version} Documentation"]
51
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,125 @@
1
+ module ActsAsRdf
2
+
3
+ require "uri"
4
+ require 'rdf/raptor'
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ # This +acts_as+ extension provides the capabilities for rendering rdf
11
+ #
12
+ module ClassMethods
13
+
14
+ def acts_as_rdf(options = {})
15
+ configuration = { :prefixes => {
16
+ 'rdf' => RDF,
17
+ 'rdfs' => RDF::RDFS,
18
+ 'xsd' => 'http://www.w3.org/2001/XMLSchema#',
19
+ 'owl' => RDF::OWL
20
+ }
21
+ }
22
+ configuration.update(options) if options.is_a?(Hash)
23
+
24
+ class_eval <<-EOV
25
+ include ActsAsRdf::InstanceMethods
26
+
27
+ def acts_as_rdf_class
28
+ self.class
29
+ end
30
+
31
+ def get_prefixes
32
+ '#{configuration[:prefixes]}'
33
+ end
34
+
35
+ EOV
36
+ end
37
+ end
38
+
39
+ # All the methods available to a record that has had <tt>acts_as_rdf</tt> specified.
40
+ module InstanceMethods
41
+
42
+ def to_rdf
43
+ graph = to_graph()
44
+ return to_output(graph, :rdfxml)
45
+ end
46
+
47
+ def to_ttl
48
+ graph = to_graph()
49
+ return to_output(graph, :turtle)
50
+ end
51
+
52
+ def to_ntriples
53
+ graph = to_graph()
54
+ return to_output(graph, :ntriples)
55
+ end
56
+
57
+ protected
58
+
59
+ def to_graph(graph = RDF::Graph.new)
60
+
61
+ if 'Array'.eql?(self.class.name) then
62
+ self.each do |element|
63
+ graph = self.to_graph(graph)
64
+ end
65
+ else
66
+ graph << [to_uri(self), RDF.type, to_uri(self.class)]
67
+
68
+ self.attributes.each do |attr, value|
69
+ graph << [to_uri(self), to_uri(attr), RDF::Literal.new(value)]
70
+ if ('name'.eql?(attr)) then
71
+ graph << [to_uri(self), RDF::RDFS.label, RDF::Literal.new(value)]
72
+ end
73
+ end
74
+
75
+ socs = self.class.reflect_on_all_associations
76
+ if (socs != nil) then
77
+ socs.each do |soc|
78
+ if self.respond_to? soc.name then
79
+ association_objects = self.send soc.name # needs to handle plural case
80
+ if association_objects != nil then
81
+ if 'Array'.eql?(association_objects.class.name) then
82
+ association_objects.each do |obj|
83
+ graph << [to_uri(self), to_uri(soc.class_name), to_uri(obj)]
84
+ end
85
+ else
86
+ graph << [to_uri(self), to_uri(soc.class_name), to_uri(association_objects)]
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+ return graph
95
+ end
96
+
97
+ def to_output(graph, format)
98
+ base_uri = RDF::URI.new(get_host())
99
+
100
+ return RDF::Writer.for(format).buffer(:base_uri => base_uri, :default_namespace => base_uri, :prefixes => get_prefixes()) { |writer|
101
+ graph.each_statement do |statement|
102
+ writer << statement
103
+ end
104
+ }
105
+ end
106
+
107
+ def to_uri(thing)
108
+ if (thing.class.name != nil)
109
+ if ('String'.eql?(thing.class.name) || 'Class'.eql?(thing.class.name)) then
110
+ return RDF::URI.new("#{get_host()}/#{thing.to_s}")
111
+ end
112
+ return RDF::URI.new("#{get_host()}/#{thing.class.name.pluralize}/#{thing.id}")
113
+ end
114
+
115
+ return RDF::URI.new("#{get_host()}##{thing.to_s}")
116
+ end
117
+
118
+ def get_host
119
+ "http://#{self.class.respond_to?('request') ? "#{request.env['HTTP_HOST']}" : "localhost"}"
120
+ end
121
+
122
+ end
123
+ end
124
+
125
+ ActiveRecord::Base.class_eval { include ActsAsRdf }
data/test/helper.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord', '>= 3.0.0'
5
+ require 'active_record'
6
+
7
+ require "#{File.dirname(__FILE__)}/../lib/acts_as_rdf"
8
+
9
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
+
11
+ def setup_db
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :mixins do |t|
14
+ t.column :name, :string
15
+ t.column :created_at, :datetime
16
+ t.column :updated_at, :datetime
17
+ end
18
+
19
+ create_table :mixin_relations do |t|
20
+ t.column :name, :string
21
+ t.column :mixin_id, :integer
22
+ t.column :created_at, :datetime
23
+ t.column :updated_at, :datetime
24
+ end
25
+ end
26
+ end
27
+
28
+ def teardown_db
29
+ ActiveRecord::Base.connection.tables.each do |table|
30
+ ActiveRecord::Base.connection.drop_table(table)
31
+ end
32
+ end
33
+
34
+ class Mixin < ActiveRecord::Base
35
+ acts_as_rdf
36
+ has_many :mixin_relations
37
+
38
+ def self.table_name() "mixins" end
39
+ end
40
+
41
+ class MixinRelation < Mixin
42
+ acts_as_rdf
43
+
44
+ belongs_to :mixin
45
+
46
+ def self.table_name() "mixin_relations" end
47
+ end
@@ -0,0 +1,42 @@
1
+ require 'test/helper'
2
+
3
+ class RdfTest < Test::Unit::TestCase
4
+ def setup
5
+ setup_db
6
+ Mixin.create! :name => "hello"
7
+ MixinRelation.create! :name => "hello relation 1", :mixin_id => 1
8
+ MixinRelation.create! :name => "hello relation 2", :mixin_id => 1
9
+ end
10
+
11
+ def teardown
12
+ teardown_db
13
+ end
14
+
15
+ def test_to_rdf
16
+ Mixin.find(:all).each do |o|
17
+ puts o.to_rdf
18
+ end
19
+ MixinRelation.find(:all).each do |o|
20
+ puts o.to_rdf
21
+ end
22
+ end
23
+
24
+ def test_to_turtle
25
+ Mixin.find(:all).each do |o|
26
+ puts o.to_ttl
27
+ end
28
+ MixinRelation.find(:all).each do |o|
29
+ puts o.to_ttl
30
+ end
31
+ end
32
+
33
+ def test_to_ntriples
34
+ Mixin.find(:all).each do |o|
35
+ puts o.to_ntriples
36
+ end
37
+ MixinRelation.find(:all).each do |o|
38
+ puts o.to_ntriples
39
+ end
40
+ end
41
+
42
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_rdf
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Andrew Sodt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-19 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rdf
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 17
46
+ segments:
47
+ - 0
48
+ - 2
49
+ - 3
50
+ version: 0.2.3
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rdf-raptor
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 15
62
+ segments:
63
+ - 0
64
+ - 4
65
+ - 0
66
+ version: 0.4.0
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: yard
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ type: :development
82
+ version_requirements: *id004
83
+ description: Gem version of acts_as_rdf Rails plugin
84
+ email: andrews@alleninstitute.org
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - README.rdoc
91
+ files:
92
+ - .gitignore
93
+ - MIT-LICENSE
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/acts_as_rdf.rb
98
+ - test/helper.rb
99
+ - test/test_to_rdf.rb
100
+ has_rdoc: true
101
+ homepage: http://github.com/rails/acts_as_rdf
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options:
106
+ - --charset=UTF-8
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.3.7
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Gem version of acts_as_rdf Rails plugin
134
+ test_files:
135
+ - test/helper.rb
136
+ - test/test_to_rdf.rb