grom 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34878dcc73dda4ed7d3145cc1bee503d8843d471
4
- data.tar.gz: 91303eb86ec021e9ef6d4ab62edb271c1d8a4b89
3
+ metadata.gz: 2d6ee092d9341867b4a7c60014c733292c40f33f
4
+ data.tar.gz: 167e881cfc3acc879bc51e0bf1255f2c1542792a
5
5
  SHA512:
6
- metadata.gz: 362e2f954adc564f22875118b2ee2901a968183a72ba900da4d7469153b1198c2f2793efa22c88f0a88bd7b25aa22f91ef58f983124dd5ad6298429114e620b9
7
- data.tar.gz: 74f6dd9f20628e1e3e766ca1874c487ea4368aabbf99c33bbe647f1ed488a379216368c75e364d995d4a7b13d4732ba701925ec3f5add0af94534367a977ef29
6
+ metadata.gz: eaaf26838e5222504705553b973434d8e8bafebe5f03dee38723c4ee4256cb89141949bffd14159a9b65cf5c7b269712df1f92737a6cc946197420f7bd3be70f
7
+ data.tar.gz: 5e6eb3ed1f02e9a0b72d23ab7b0a5b0cefb7a231f1d154dd4853234f94feff507335cdf38bcbd62d8dce1f81ba1ed9b42e7bb7bbf387f70d5c3aeb40065358d0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ */.idea/*
5
+ .idea/*
6
+ .DS_Store
7
+ Gemfile.lock
8
+ /coverage
9
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,57 @@
1
+ Metrics/LineLength:
2
+ Description: 'Limit lines to 120 characters.'
3
+ Max: 120
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Style/SpaceBeforeFirstArg:
9
+ Enabled: false
10
+
11
+ Style/BracesAroundHashParameters:
12
+ Enabled: false
13
+
14
+ Style/IndentHash:
15
+ EnforcedStyle: consistent
16
+
17
+ Style/AlignHash:
18
+ Severity: fatal
19
+ Enabled: true
20
+ EnforcedHashRocketStyle: table
21
+ EnforcedColonStyle: table
22
+
23
+ Style/AlignParameters:
24
+ EnforcedStyle: with_fixed_indentation
25
+
26
+ Style/StringLiterals:
27
+ EnforcedStyle: single_quotes
28
+
29
+ Style/CollectionMethods:
30
+ PreferredMethods:
31
+ collect: 'map'
32
+ collect!: 'map!'
33
+ inject: 'reduce'
34
+ detect: 'find'
35
+ find_all: 'select'
36
+
37
+ Style/DotPosition:
38
+ EnforcedStyle: leading
39
+
40
+ Style/DoubleNegation:
41
+ Enabled: false
42
+
43
+ Style/SpaceAroundOperators:
44
+ # When true, allows most uses of extra spacing if the intent is to align
45
+ # with an operator on the previous or next line, not counting empty lines
46
+ # or comment lines.
47
+ AllowForAlignment: true
48
+
49
+ Style/FrozenStringLiteralComment:
50
+ Enabled: false
51
+
52
+ AllCops:
53
+ Exclude:
54
+ - '*.gemspec'
55
+ - 'vendor/**/*'
56
+ - 'tmp/**/*'
57
+ - 'spec/**/*'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mattrayner1@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in grom.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2016-2017 United Kingdom Parliament
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Graph Object Mapper (Grom)
2
+ ALL QUERIES MUST CONTAIN TYPES
2
3
  Grom is a custom ORM that takes graph data and deserializes it into objects. It uses the [RDF.rb](https://github.com/ruby-rdf/rdf) library. It was created in the context
3
4
  of UK Parliament's new website.
4
5
 
data/Rakefile CHANGED
@@ -1,34 +1,6 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- require 'rdoc/task'
8
-
9
- RDoc::Task.new(:rdoc) do |rdoc|
10
- rdoc.rdoc_dir = 'rdoc'
11
- rdoc.title = 'Grom'
12
- rdoc.options << '--line-numbers'
13
- rdoc.rdoc_files.include('README.md')
14
- rdoc.rdoc_files.include('lib/**/*.rb')
15
- end
16
-
17
-
18
-
19
-
20
-
21
-
22
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
23
3
 
24
- require 'rake/testtask'
25
-
26
- Rake::TestTask.new(:test) do |t|
27
- t.libs << 'lib'
28
- t.libs << 'test'
29
- t.pattern = 'test/**/*_test.rb'
30
- t.verbose = false
31
- end
32
-
4
+ RSpec::Core::RakeTask.new(:spec)
33
5
 
34
- task default: :test
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ require 'grom'
4
+
5
+ # You can add fixtures and/or initialization code here to make experimenting
6
+ # with your gem easier. You can also use a different console, if you like.
7
+
8
+ # (If you use this, don't forget to add pry to your Gemfile!)
9
+ require 'pry'
10
+ Pry.start
11
+
12
+ require 'irb'
13
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/grom.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'grom/version'
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'grom'
9
+ spec.version = Grom::VERSION
10
+ spec.authors = ['Rebecca Appleyard', 'Giuseppe De Santis', 'Matt Rayner']
11
+ spec.email = ['rklappleyard@gmail.com', 'giusdesan@gmail.com', 'mattrayner1@gmail.com']
12
+ spec.homepage = 'https://github.com/ukparliament/grom'
13
+ spec.summary = 'Grom is a Graph Object Mapper'
14
+ spec.description = 'Grom is a Graph Object Mapper'
15
+ spec.license = 'Open Parliament License'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_dependency 'rdf'
25
+ spec.add_dependency 'activesupport'
26
+
27
+ # the below are test dependencies
28
+ spec.add_development_dependency 'bundler', '~> 1.13'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'rubocop'
32
+ spec.add_development_dependency 'pry'
33
+ spec.add_development_dependency 'pry-nav'
34
+ spec.add_development_dependency 'simplecov'
35
+ end
@@ -0,0 +1,65 @@
1
+ module Grom
2
+ class Builder
3
+ attr_reader :objects
4
+
5
+ def initialize(reader)
6
+ @reader = reader
7
+
8
+ build_objects
9
+ end
10
+
11
+ def build_objects
12
+ build_objects_by_subject
13
+ link_objects
14
+
15
+ @objects
16
+ end
17
+
18
+ def initialize_objects_hashes
19
+ @objects, @objects_by_subject = [], {}
20
+ end
21
+
22
+ def build_objects_by_subject
23
+ initialize_objects_hashes
24
+ @reader.subjects_by_type.each do |_type, subjects|
25
+ subjects.each do |subject|
26
+ begin
27
+ object = Grom::Node.new(@reader.statements_by_subject[subject])
28
+ @objects_by_subject[subject] = object
29
+ @objects << object
30
+ rescue NoMethodError
31
+ p 'No statements passed to Grom::Node.new'
32
+ super
33
+ end
34
+ end
35
+ end
36
+
37
+ self
38
+ end
39
+
40
+ def link_objects
41
+ @reader.connections_by_subject.each do |subject, connections|
42
+ current_node = @objects_by_subject[subject]
43
+ connections.each do |connection_subject|
44
+ begin
45
+ connection_node = @objects_by_subject[connection_subject]
46
+ current_node_type = Grom::Helper.get_id(current_node.type)
47
+ connector_name_symbol = Grom::Helper.pluralize_instance_variable_symbol(current_node_type)
48
+
49
+ connected_object_array = connection_node.instance_variable_get(connector_name_symbol)
50
+ connected_object_array = [] if connected_object_array.nil?
51
+ connected_object_array << current_node
52
+
53
+ connection_node.instance_variable_set(connector_name_symbol, connected_object_array)
54
+ rescue NoMethodError
55
+ p 'No type for current node'
56
+ super
57
+ end
58
+ end
59
+ end
60
+
61
+ self
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,21 @@
1
+ module Grom
2
+ module Helper
3
+ def self.pluralize_instance_variable_symbol(string)
4
+ string = ActiveSupport::Inflector.underscore(string)
5
+ string = ActiveSupport::Inflector.pluralize(string).downcase
6
+
7
+ "@#{string}".to_sym
8
+ end
9
+
10
+ def self.lazy_array_insert(hash, key, value)
11
+ hash[key] ||= []
12
+ hash[key] << value
13
+ end
14
+
15
+ def self.get_id(uri)
16
+ return nil if uri.to_s['/'].nil?
17
+
18
+ uri == RDF.type.to_s ? 'type' : uri.to_s.split('/').last
19
+ end
20
+ end
21
+ end
data/lib/grom/node.rb ADDED
@@ -0,0 +1,29 @@
1
+ module Grom
2
+ class Node
3
+ attr_reader :statements
4
+
5
+ def initialize(statements)
6
+ @statements = statements
7
+
8
+ populate
9
+ end
10
+
11
+ def method_missing(method, *params, &block)
12
+ instance_variable_get("@#{method}".to_sym) || super
13
+ end
14
+
15
+ def respond_to_missing?(method, include_private = false)
16
+ instance_variable_get("@#{method}".to_sym) || super
17
+ end
18
+
19
+ private
20
+
21
+ def populate
22
+ @statements.each do |statement|
23
+ attribute_name = Grom::Helper.get_id(statement.predicate)
24
+ attribute_value = statement.object.to_s
25
+ instance_variable_set("@#{attribute_name}".to_sym, attribute_value)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,43 @@
1
+ require 'pry'
2
+
3
+ module Grom
4
+ class Reader
5
+ attr_reader :data, :statements_by_subject, :subjects_by_type, :connections_by_subject, :objects
6
+
7
+ def initialize(data)
8
+ @data = data
9
+
10
+ read_data
11
+
12
+ @objects = Grom::Builder.new(self).objects
13
+ end
14
+
15
+ def read_data
16
+ # Reset all our hashes just in case
17
+ @statements_by_subject = {}
18
+ @subjects_by_type = {}
19
+ @connections_by_subject = {}
20
+
21
+ RDF::NTriples::Reader.new(@data) do |reader|
22
+ reader.each_statement do |statement|
23
+ subject = statement.subject.to_s
24
+
25
+ # TODO: Use Ruby key value syntax in below method.
26
+ Grom::Helper.lazy_array_insert(@statements_by_subject, subject, statement)
27
+
28
+ predicate = statement.predicate.to_s
29
+
30
+ if predicate == RDF.type.to_s
31
+ Grom::Helper.lazy_array_insert(@subjects_by_type, Grom::Helper.get_id(statement.object), subject)
32
+ end
33
+
34
+ if (statement.object =~ URI.regexp) == 0 && predicate != RDF.type.to_s
35
+ Grom::Helper.lazy_array_insert(@connections_by_subject, subject, statement.object.to_s)
36
+ end
37
+ end
38
+ end
39
+
40
+ self
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Grom
2
+ VERSION = '0.2.0'.freeze
3
+ end
data/lib/grom.rb CHANGED
@@ -1,12 +1,11 @@
1
- require_relative '../lib/grom/graph_mapper'
2
- require_relative '../lib/grom/base'
3
- require_relative '../lib/grom/helpers'
4
- require 'active_support/core_ext/string/inflections'
1
+ require 'rdf'
2
+ require 'active_support/inflector'
5
3
 
6
- module Grom
7
- VERSION = '0.1.1'
4
+ require_relative 'grom/version'
5
+ require_relative 'grom/reader'
6
+ require_relative 'grom/node'
7
+ require_relative 'grom/helper'
8
+ require_relative 'grom/builder'
8
9
 
9
- def self.root
10
- File.dirname __dir__
11
- end
10
+ module Grom
12
11
  end
metadata CHANGED
@@ -1,36 +1,31 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rebecca Appleyard
8
8
  - Giuseppe De Santis
9
+ - Matt Rayner
9
10
  autorequire:
10
- bindir: bin
11
+ bindir: exe
11
12
  cert_chain: []
12
- date: 2016-12-20 00:00:00.000000000 Z
13
+ date: 2017-01-25 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
- name: rails
16
+ name: rdf
16
17
  requirement: !ruby/object:Gem::Requirement
17
18
  requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: 5.0.0
21
19
  - - ">="
22
20
  - !ruby/object:Gem::Version
23
- version: 5.0.0.1
21
+ version: '0'
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
27
25
  requirements:
28
- - - "~>"
29
- - !ruby/object:Gem::Version
30
- version: 5.0.0
31
26
  - - ">="
32
27
  - !ruby/object:Gem::Version
33
- version: 5.0.0.1
28
+ version: '0'
34
29
  - !ruby/object:Gem::Dependency
35
30
  name: activesupport
36
31
  requirement: !ruby/object:Gem::Requirement
@@ -46,13 +41,55 @@ dependencies:
46
41
  - !ruby/object:Gem::Version
47
42
  version: '0'
48
43
  - !ruby/object:Gem::Dependency
49
- name: rdf-turtle
44
+ name: bundler
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.13'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.13'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '10.0'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '10.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '3.0'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '3.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: rubocop
50
87
  requirement: !ruby/object:Gem::Requirement
51
88
  requirements:
52
89
  - - ">="
53
90
  - !ruby/object:Gem::Version
54
91
  version: '0'
55
- type: :runtime
92
+ type: :development
56
93
  prerelease: false
57
94
  version_requirements: !ruby/object:Gem::Requirement
58
95
  requirements:
@@ -60,7 +97,7 @@ dependencies:
60
97
  - !ruby/object:Gem::Version
61
98
  version: '0'
62
99
  - !ruby/object:Gem::Dependency
63
- name: rspec-rails
100
+ name: pry
64
101
  requirement: !ruby/object:Gem::Requirement
65
102
  requirements:
66
103
  - - ">="
@@ -74,7 +111,7 @@ dependencies:
74
111
  - !ruby/object:Gem::Version
75
112
  version: '0'
76
113
  - !ruby/object:Gem::Dependency
77
- name: webmock
114
+ name: pry-nav
78
115
  requirement: !ruby/object:Gem::Requirement
79
116
  requirements:
80
117
  - - ">="
@@ -101,25 +138,36 @@ dependencies:
101
138
  - - ">="
102
139
  - !ruby/object:Gem::Version
103
140
  version: '0'
104
- description: Grom is a Graph Object Mapper designed for UK Parliament
141
+ description: Grom is a Graph Object Mapper
105
142
  email:
106
143
  - rklappleyard@gmail.com
107
144
  - giusdesan@gmail.com
145
+ - mattrayner1@gmail.com
108
146
  executables: []
109
147
  extensions: []
110
148
  extra_rdoc_files: []
111
149
  files:
112
- - MIT-LICENSE
150
+ - ".gitignore"
151
+ - ".rspec"
152
+ - ".rubocop.yml"
153
+ - ".ruby-version"
154
+ - CODE_OF_CONDUCT.md
155
+ - Gemfile
156
+ - LICENSE
113
157
  - README.md
114
158
  - Rakefile
159
+ - bin/console
160
+ - bin/setup
161
+ - grom.gemspec
115
162
  - lib/grom.rb
116
- - lib/grom/base.rb
117
- - lib/grom/graph_mapper.rb
118
- - lib/grom/helpers.rb
119
- - lib/tasks/grom_tasks.rake
120
- homepage: ''
163
+ - lib/grom/builder.rb
164
+ - lib/grom/helper.rb
165
+ - lib/grom/node.rb
166
+ - lib/grom/reader.rb
167
+ - lib/grom/version.rb
168
+ homepage: https://github.com/ukparliament/grom
121
169
  licenses:
122
- - MIT
170
+ - Open Parliament License
123
171
  metadata: {}
124
172
  post_install_message:
125
173
  rdoc_options: []
@@ -137,8 +185,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
185
  version: '0'
138
186
  requirements: []
139
187
  rubyforge_project:
140
- rubygems_version: 2.6.7
188
+ rubygems_version: 2.5.1
141
189
  signing_key:
142
190
  specification_version: 4
143
- summary: ''
191
+ summary: Grom is a Graph Object Mapper
144
192
  test_files: []
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2016 Rebecca Appleyard
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/lib/grom/base.rb DELETED
@@ -1,94 +0,0 @@
1
- require 'grom'
2
- require 'uri'
3
- require_relative '../../lib/grom/helpers'
4
-
5
- module Grom
6
- class Base
7
- extend Grom::GraphMapper
8
- extend Grom::Helpers
9
-
10
- def initialize(attributes)
11
- attributes.each do |k, v|
12
- translated_key = self.class.property_translator[k]
13
- v = self.class.create_property_name(self.class.get_id(v)) if (v =~ URI::regexp) == 0
14
- unless v.nil? || translated_key.nil?
15
- instance_variable_set("@#{translated_key}", v)
16
- self.class.send(:attr_reader, translated_key)
17
- end
18
- end
19
- end
20
-
21
- def self.find(id)
22
- endpoint_url = "#{find_base_url_builder(self.name, id)}.ttl"
23
- ttl_data = get_ttl_data(endpoint_url)
24
- self.object_single_maker(ttl_data)
25
- end
26
-
27
- def self.all(*options)
28
- endpoint_url = "#{all_base_url_builder(self.name, *options)}.ttl"
29
- ttl_data = get_ttl_data(endpoint_url)
30
-
31
- self.object_array_maker(ttl_data)
32
- end
33
-
34
- def self.has_many(association)
35
- self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_many_query(self, *options); end")
36
- end
37
-
38
- def self.has_many_through(association, through_association)
39
- self.has_many(through_association[:via])
40
- self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_many_through_query(self, #{create_class_name(through_association[:via])}.new({}).class.name, *options); end")
41
- end
42
-
43
- def self.has_one(association)
44
- self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_one_query(self, *options); end")
45
- end
46
-
47
- def self.has_many_query(owner_object, *options)
48
- endpoint_url = associations_url_builder(owner_object, self.name, {optional: options })
49
- ttl_data = get_ttl_data(endpoint_url)
50
- self.object_array_maker(ttl_data)
51
- end
52
-
53
- def self.has_one_query(owner_object, *options)
54
- endpoint_url = associations_url_builder(owner_object, self.name, {optional: options, single: true })
55
- ttl_data = get_ttl_data(endpoint_url)
56
- self.object_single_maker(ttl_data)
57
- end
58
-
59
- def self.through_getter_setter(through_property_plural)
60
- self.class_eval("def #{through_property_plural}=(array); @#{through_property_plural} = array; end")
61
- self.class_eval("def #{through_property_plural}; @#{through_property_plural}; end")
62
- end
63
-
64
- def self.object_array_maker(ttl_data)
65
- create_hash_from_ttl(ttl_data).map do |hash|
66
- self.new(hash)
67
- end
68
- end
69
-
70
- def self.object_single_maker(ttl_data)
71
- self.object_array_maker(ttl_data).first
72
- end
73
-
74
- def self.has_many_through_query(owner_object, through_class, *options)
75
- through_property_plural = create_plural_property_name(through_class)
76
- endpoint_url = associations_url_builder(owner_object, self.name, {optional: options })
77
- self.through_getter_setter(through_property_plural)
78
- ttl_data = get_ttl_data(endpoint_url)
79
- self.map_hashes_to_objects(through_split_graph(ttl_data), through_property_plural)
80
- end
81
-
82
- def self.map_hashes_to_objects(hashes, through_property_plural)
83
- through_array = hashes[:through_class_hash].values
84
- hashes[:associated_class_hash].values.map do |hash|
85
- associated_object = self.new(hash)
86
- through_obj_array, through_array = through_array.partition do |t_hash|
87
- t_hash[:associated_object_id] == associated_object.id
88
- end
89
- associated_object.send((through_property_plural + '=').to_sym, through_obj_array)
90
- associated_object
91
- end
92
- end
93
- end
94
- end
@@ -1,56 +0,0 @@
1
- require 'grom'
2
- require 'rdf/turtle'
3
-
4
- module Grom
5
- module GraphMapper
6
-
7
- def get_ttl_data(uri)
8
- Net::HTTP.get(URI(uri))
9
- end
10
-
11
- def get_id(uri)
12
- uri == RDF.type.to_s ? 'type' : uri.to_s.split("/").last
13
- end
14
-
15
- def create_hash_from_ttl(ttl_data)
16
- hash = {}
17
- RDF::Turtle::Reader.new(ttl_data) do |reader|
18
- reader.each_statement do |statement|
19
- statement_mapper(statement, hash)
20
- end
21
- end
22
- hash.values
23
- end
24
-
25
- def statement_mapper(statement, hash)
26
- subject = get_id(statement.subject)
27
- hash[subject] ||= { :id => subject }
28
- hash[subject][get_id(statement.predicate).to_sym] = statement.object.to_s
29
- end
30
-
31
- def through_split_graph(ttl_data)
32
- associated_hash, through_hash = {}, {}
33
- RDF::Turtle::Reader.new(ttl_data) do |reader|
34
- reader.each_statement do |s|
35
- if (s.subject.to_s =~ URI::regexp) == 0
36
- subject = get_id(s.subject)
37
- associated_hash[subject] ||= { :id => subject }
38
- associated_hash[subject][get_id(s.predicate).to_sym] = s.object.to_s
39
- else
40
- through_hash[s.subject.to_s] ||= {}
41
- if get_id(s.predicate) == "connect"
42
- through_hash[s.subject.to_s][:associated_object_id] = get_id(s.object)
43
- elsif get_id(s.predicate) == "objectId"
44
- through_hash[s.subject.to_s][:id] = get_id(s.object)
45
- else
46
- through_hash[s.subject.to_s][get_id(s.predicate).to_sym] = s.object.to_s
47
- end
48
- end
49
- end
50
- end
51
-
52
- { associated_class_hash: associated_hash, through_class_hash: through_hash }
53
- end
54
-
55
- end
56
- end
data/lib/grom/helpers.rb DELETED
@@ -1,58 +0,0 @@
1
- require 'grom'
2
-
3
- module Grom
4
- module Helpers
5
-
6
- def associations_url_builder(owner_object, associated_class_name, options={})
7
- id = owner_object.id
8
- associated_class_name = options[:single].nil? ? create_plural_property_name(associated_class_name) : create_property_name(associated_class_name)
9
- endpoint = "#{find_base_url_builder(owner_object.class.name, id)}/#{associated_class_name}"
10
- if options[:optional].nil?
11
- endpoint += '.ttl'
12
- else
13
- options[:optional].each do |option|
14
- endpoint += "/#{option}" unless option.nil?
15
- end
16
- endpoint += '.ttl'
17
- end
18
- endpoint
19
- end
20
-
21
- def find_base_url_builder(class_name, id)
22
- "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}/#{id}"
23
- end
24
-
25
- def all_base_url_builder(class_name, *options)
26
- endpoint = "#{API_ENDPOINT}/#{create_plural_property_name(class_name)}"
27
- options.each do |option|
28
- endpoint += "/#{option}" unless option.nil?
29
- end
30
- endpoint
31
- end
32
-
33
- def create_class_name(plural_name)
34
- ActiveSupport::Inflector.camelize(ActiveSupport::Inflector.singularize(plural_name.to_s).capitalize)
35
- end
36
-
37
- def create_plural_property_name(class_name)
38
- ActiveSupport::Inflector.pluralize(create_property_name(class_name))
39
- end
40
-
41
- def create_property_name(class_name)
42
- ActiveSupport::Inflector.underscore(class_name).downcase
43
- end
44
-
45
- def order_list(arr, *parameters)
46
- rejected = []
47
- arr.delete_if{ |obj| rejected << obj if parameters.any?{ |param| obj.send(param).nil? } }
48
- sorted_arr = arr.sort_by do |obj|
49
- parameters.map{ |param| obj.send(param) }.select{ |p| not p.nil? }
50
- end
51
- rejected.concat(sorted_arr)
52
- end
53
-
54
- def order_list_by_through(arr, through_association, property)
55
- arr.map{ |obj| obj.send(through_association) }.flatten.sort{ |a, b| a[property] <=> b[property] }
56
- end
57
- end
58
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :grom do
3
- # # Task goes here
4
- # end