dm-is-checksumed 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +3 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +4 -0
- data/Gemfile +142 -0
- data/LICENSE.txt +20 -0
- data/README.md +71 -0
- data/Rakefile +33 -0
- data/dm-is-checksumed.gemspec +15 -0
- data/gemspec.yml +19 -0
- data/lib/dm-is-checksumed.rb +4 -0
- data/lib/dm-is-checksumed/is/checksumed.rb +143 -0
- data/spec/integration/checksumed_spec.rb +40 -0
- data/spec/models/url.rb +16 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/unit/checksumed_spec.rb +54 -0
- metadata +105 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title "dm-is-checksumed Documentation" --protected
|
data/ChangeLog.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# If you're working on more than one datamapper gem at a time, then it's
|
2
|
+
# recommended to create a local Gemfile and use this instead of the git
|
3
|
+
# sources. This will make sure that you are developing against your
|
4
|
+
# other local datamapper sources that you currently work on. Gemfile.local
|
5
|
+
# will behave identically to the standard Gemfile apart from the fact that
|
6
|
+
# it fetches the datamapper gems from local paths. This means that you can
|
7
|
+
# use the same environment variables, like ADAPTER(S) or PLUGIN(S) when
|
8
|
+
# running
|
9
|
+
# bundle commands. Gemfile.local is added to .gitignore, so you don't need
|
10
|
+
# to worry about accidentally checking local development paths into git.
|
11
|
+
# In order to create a local Gemfile, all you need to do is run:
|
12
|
+
#
|
13
|
+
# bundle exec rake local_gemfile
|
14
|
+
#
|
15
|
+
# This will give you a Gemfile.local file that points to your local clones
|
16
|
+
# of the various datamapper gems. It's assumed that all datamapper repo
|
17
|
+
# clones reside in the same directory. You can use the Gemfile.local like
|
18
|
+
# so for running any bundle command:
|
19
|
+
#
|
20
|
+
# BUNDLE_GEMFILE=Gemfile.local bundle foo
|
21
|
+
#
|
22
|
+
# You can also specify which adapter(s) should be part of the bundle by
|
23
|
+
# setting an environment variable. This of course also works when using the
|
24
|
+
# Gemfile.local
|
25
|
+
#
|
26
|
+
# bundle foo # dm-sqlite-adapter
|
27
|
+
#
|
28
|
+
# ADAPTER=mysql bundle foo # dm-mysql-adapter
|
29
|
+
#
|
30
|
+
# ADAPTERS=sqlite,mysql bundle foo # dm-sqlite-adapter, dm-mysql-adapter
|
31
|
+
#
|
32
|
+
# Of course you can also use the ADAPTER(S) variable when using the
|
33
|
+
# Gemfile.local and running specs against selected adapters.
|
34
|
+
#
|
35
|
+
# For easily working with adapters supported on your machine, it's
|
36
|
+
# recommended that you first install all adapters that you are planning to
|
37
|
+
# use or work on by doing something like
|
38
|
+
#
|
39
|
+
# ADAPTERS=sqlite,mysql,postgres bundle install
|
40
|
+
#
|
41
|
+
# This will clone the various repositories and make them available to
|
42
|
+
# bundler. Once you have them installed you can easily switch between
|
43
|
+
# adapters for the various development tasks. Running something like
|
44
|
+
#
|
45
|
+
# ADAPTER=mysql bundle exec rake spec
|
46
|
+
#
|
47
|
+
# will make sure that the dm-mysql-adapter is part of the bundle, and will
|
48
|
+
# be used when running the specs.
|
49
|
+
#
|
50
|
+
# You can also specify which plugin(s) should be part of the bundle by
|
51
|
+
# setting an environment variable. This also works when using the
|
52
|
+
# Gemfile.local
|
53
|
+
#
|
54
|
+
# bundle foo # dm-migrations
|
55
|
+
#
|
56
|
+
# PLUGINS=dm-validations bundle foo # dm-migrations,
|
57
|
+
# # dm-validations
|
58
|
+
#
|
59
|
+
# PLUGINS=dm-validations,dm-types bundle foo # dm-migrations,
|
60
|
+
# # dm-validations,
|
61
|
+
# # dm-types
|
62
|
+
#
|
63
|
+
# Of course you can combine the PLUGIN(S) and ADAPTER(S) env vars to run
|
64
|
+
# specs for certain adapter/plugin combinations.
|
65
|
+
#
|
66
|
+
# Finally, to speed up running specs and other tasks, it's recommended to
|
67
|
+
# run
|
68
|
+
#
|
69
|
+
# bundle lock
|
70
|
+
#
|
71
|
+
# after running 'bundle install' for the first time. This will make
|
72
|
+
# 'bundle exec' run a lot faster compared to the unlocked version. With an
|
73
|
+
# unlocked bundle you would typically just run 'bundle install' from time
|
74
|
+
# to time to fetch the latest sources from upstream. When you locked your
|
75
|
+
# bundle, you need to run
|
76
|
+
#
|
77
|
+
# bundle install --relock
|
78
|
+
#
|
79
|
+
# to make sure to fetch the latest updates and then lock the bundle again.
|
80
|
+
# Gemfile.lock is added to the .gitignore file, so you don't need to worry
|
81
|
+
# about accidentally checking it into version control.
|
82
|
+
|
83
|
+
source :rubygems
|
84
|
+
|
85
|
+
DATAMAPPER = 'http://github.com/datamapper'
|
86
|
+
DM_VERSION = '~> 1.0'
|
87
|
+
DO_VERSION = '~> 0.10.2'
|
88
|
+
DM_DO_ADAPTERS = %w[sqlite postgres mysql oracle sqlserver]
|
89
|
+
RAILS = 'http://github.com/rails/rails.git'
|
90
|
+
|
91
|
+
if ENV['EXTLIB']
|
92
|
+
gem 'extlib', '~> 0.9.15', :git => "#{DATAMAPPER}/extlib.git",
|
93
|
+
:require => nil
|
94
|
+
else
|
95
|
+
gem 'activesupport', '~> 3.0.4', :require => nil
|
96
|
+
gem 'i18n', '~> 0.5.0'
|
97
|
+
end
|
98
|
+
|
99
|
+
gem 'dm-core', DM_VERSION, :git => "#{DATAMAPPER}/dm-core.git"
|
100
|
+
|
101
|
+
group :development do
|
102
|
+
gem 'rake', '~> 0.8.7'
|
103
|
+
|
104
|
+
gem 'ore-tasks', '~> 0.4'
|
105
|
+
gem 'rspec', '~> 2.4'
|
106
|
+
|
107
|
+
gem 'kramdown', '~> 0.12.0'
|
108
|
+
gem 'yard', '~> 0.6.0'
|
109
|
+
end
|
110
|
+
|
111
|
+
group :datamapper do
|
112
|
+
# We need this because we want to pin these dependencies to their git
|
113
|
+
# master sources
|
114
|
+
|
115
|
+
adapters = ENV['ADAPTER'] || ENV['ADAPTERS']
|
116
|
+
adapters = adapters.to_s.tr(',', ' ').split.uniq - %w[ in_memory ]
|
117
|
+
|
118
|
+
if (do_adapters = DM_DO_ADAPTERS & adapters).any?
|
119
|
+
options = {}
|
120
|
+
options[:git] = "#{DATAMAPPER}/do.git" if ENV['DO_GIT'] == 'true'
|
121
|
+
|
122
|
+
gem 'data_objects', DO_VERSION, options.dup
|
123
|
+
|
124
|
+
do_adapters.each do |adapter|
|
125
|
+
adapter = 'sqlite3' if adapter == 'sqlite'
|
126
|
+
gem "do_#{adapter}", DO_VERSION, options.dup
|
127
|
+
end
|
128
|
+
|
129
|
+
gem 'dm-do-adapter', DM_VERSION, :git => "#{DATAMAPPER}/dm-do-adapter.git"
|
130
|
+
end
|
131
|
+
|
132
|
+
adapters.each do |adapter|
|
133
|
+
gem "dm-#{adapter}-adapter", DM_VERSION, :git => "#{DATAMAPPER}/dm-#{adapter}-adapter.git"
|
134
|
+
end
|
135
|
+
|
136
|
+
plugins = ENV['PLUGINS'] || ENV['PLUGIN']
|
137
|
+
plugins = plugins.to_s.tr(',', ' ').split.push('dm-migrations').uniq
|
138
|
+
|
139
|
+
plugins.each do |plugin|
|
140
|
+
gem plugin, DM_VERSION, :git => "#{DATAMAPPER}/#{plugin}.git"
|
141
|
+
end
|
142
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Hal Brodigan
|
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.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# dm-is-checksumed
|
2
|
+
|
3
|
+
* [Source](http://github.com/postmodern/dm-is-checksumed)
|
4
|
+
* [Issues](http://github.com/postmodern/dm-is-checksumed/issues)
|
5
|
+
* [Documentation](http://rubydoc.info/gems/dm-is-checksumed/frames)
|
6
|
+
* [Email](mailto:postmodern.mod3 at gmail.com)
|
7
|
+
|
8
|
+
## Description
|
9
|
+
|
10
|
+
A DataMapper plugin which adds checksum properties to a Model, referencing
|
11
|
+
other properties.
|
12
|
+
|
13
|
+
## Features
|
14
|
+
|
15
|
+
* Uses sha256
|
16
|
+
* Allows unique and non-unique checksums.
|
17
|
+
* Automatically substitutes in checksums in queries to avoid expensive
|
18
|
+
queries.
|
19
|
+
* Works with `first_or_create` and `first_or_new`.
|
20
|
+
|
21
|
+
## Examples
|
22
|
+
|
23
|
+
require 'dm-is-checksumed'
|
24
|
+
|
25
|
+
class Url
|
26
|
+
|
27
|
+
include DataMapper::Resource
|
28
|
+
|
29
|
+
is :checksumed
|
30
|
+
|
31
|
+
property :id, Serial
|
32
|
+
|
33
|
+
# The property to be checksumed
|
34
|
+
property :url, Text
|
35
|
+
|
36
|
+
# Defines the `url_checksum` property, referencing `url`
|
37
|
+
checksum :url
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Checksums are automatically calculated
|
42
|
+
url = Url.create(:url => 'http://example.com/extremely/long/url....')
|
43
|
+
url.url_checksum
|
44
|
+
# => "b74d7288b86817dea55bd044d3cedc013e83518b461cd6f202be85e33d95715b"
|
45
|
+
|
46
|
+
# Checksums are automatically substituted into queries
|
47
|
+
url = Url.first(:url => 'http://example.com/same/huge/url')
|
48
|
+
# ~ (0.000116) SELECT "id", "url_checksum" FROM "urls" WHERE "url_checksum" = 'b74d7288b86817dea55bd044d3cedc013e83518b461cd6f202be85e33d95715b' ORDER BY "id" LIMIT 1
|
49
|
+
|
50
|
+
# Works with first_or_create and first_or_new
|
51
|
+
url = Url.first_or_create(:url => 'http://example.com/same/huge/url')
|
52
|
+
url.url_checksum
|
53
|
+
# => "b74d7288b86817dea55bd044d3cedc013e83518b461cd6f202be85e33d95715b"
|
54
|
+
|
55
|
+
url = Url.first_or_create(:url => 'http://example.com/different/url')
|
56
|
+
url.url_checksum
|
57
|
+
# => "09a27797a33153b005871b61675a920fa6bedbb034a3b283bbe5803a1d31ac42"
|
58
|
+
|
59
|
+
## Requirements
|
60
|
+
|
61
|
+
* [dm-core](http://github.com/datamapper/dm-core) ~> 1.0
|
62
|
+
|
63
|
+
## Install
|
64
|
+
|
65
|
+
$ gem install dm-is-checksumed
|
66
|
+
|
67
|
+
## Copyright
|
68
|
+
|
69
|
+
Copyright (c) 2011 Hal Brodigan
|
70
|
+
|
71
|
+
See {file:LICENSE.txt} for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError => e
|
6
|
+
STDERR.puts e.message
|
7
|
+
STDERR.puts "Run `gem install bundler` to install Bundler."
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
Bundler.setup(:development)
|
13
|
+
rescue Bundler::BundlerError => e
|
14
|
+
STDERR.puts e.message
|
15
|
+
STDERR.puts "Run `bundle install` to install missing gems."
|
16
|
+
exit e.status_code
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake'
|
20
|
+
|
21
|
+
require 'ore/tasks'
|
22
|
+
Ore::Tasks.new
|
23
|
+
|
24
|
+
require 'rspec/core/rake_task'
|
25
|
+
RSpec::Core::RakeTask.new
|
26
|
+
|
27
|
+
task :test => :spec
|
28
|
+
task :default => :spec
|
29
|
+
|
30
|
+
require 'yard'
|
31
|
+
YARD::Rake::YardocTask.new
|
32
|
+
task :doc => :yard
|
33
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
begin
|
4
|
+
Ore::Specification.new do |gemspec|
|
5
|
+
# custom logic here
|
6
|
+
end
|
7
|
+
rescue NameError
|
8
|
+
begin
|
9
|
+
require 'ore/specification'
|
10
|
+
retry
|
11
|
+
rescue LoadError
|
12
|
+
STDERR.puts "The '#{__FILE__}' file requires Ore."
|
13
|
+
STDERR.puts "Run `gem install ore-core` to install Ore."
|
14
|
+
end
|
15
|
+
end
|
data/gemspec.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
name: dm-is-checksumed
|
2
|
+
version: 0.1.0
|
3
|
+
summary: Adds checksum properties to DataMapper Models.
|
4
|
+
description:
|
5
|
+
A DataMapper plugin which adds checksum properties to a Model,
|
6
|
+
referencing other properties.
|
7
|
+
|
8
|
+
license: MIT
|
9
|
+
authors: Postmodern
|
10
|
+
email: postmodern.mod3@gmail.com
|
11
|
+
homepage: http://github.com/postmodern/dm-is-checksumed
|
12
|
+
has_yard: true
|
13
|
+
|
14
|
+
dependencies:
|
15
|
+
dm-core: ~> 1.0
|
16
|
+
|
17
|
+
development_dependencies:
|
18
|
+
yard: ~> 0.6.0
|
19
|
+
bundler: ~> 1.0.0
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'dm-core'
|
2
|
+
require 'digest/sha2'
|
3
|
+
|
4
|
+
module DataMapper
|
5
|
+
module Is
|
6
|
+
module Checksumed
|
7
|
+
#
|
8
|
+
# Fired when your plugin gets included into a Model.
|
9
|
+
#
|
10
|
+
def is_checksumed(options={})
|
11
|
+
extend DataMapper::Is::Checksumed::ClassMethods
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
#
|
16
|
+
# The properties which have accompanying checksums.
|
17
|
+
#
|
18
|
+
# @return [Set<Symbol>]
|
19
|
+
# The property names.
|
20
|
+
#
|
21
|
+
def checksumed_properties
|
22
|
+
@checksumed_properties ||= Set[]
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Determines if a checksum property was defined for another
|
27
|
+
# property.
|
28
|
+
#
|
29
|
+
# @param [Symbol, String] name
|
30
|
+
# The name of the property.
|
31
|
+
#
|
32
|
+
# @return [Boolean]
|
33
|
+
# Specifies whether the property has an checksum property.
|
34
|
+
#
|
35
|
+
def checksumed?(name)
|
36
|
+
checksumed_properties.include?(name.to_sym)
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Calculates the SHA256 checksum of the given data.
|
41
|
+
#
|
42
|
+
# @param [#to_s] data
|
43
|
+
# The data to checksum.
|
44
|
+
#
|
45
|
+
# @return [String]
|
46
|
+
# The SHA256 hex-digest of the data.
|
47
|
+
#
|
48
|
+
def calculate_checksum(data)
|
49
|
+
Digest::SHA256.hexdigest(data.to_s)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Substitutes any checksumed properties with the checksums of their
|
54
|
+
# values.
|
55
|
+
#
|
56
|
+
# @param [DataMapper::Undefined, DataMapper::Query, Hash] query
|
57
|
+
# The query.
|
58
|
+
#
|
59
|
+
# @return [DataMapper::Resource, nil]
|
60
|
+
# The matching resource.
|
61
|
+
#
|
62
|
+
def first(query=Undefined)
|
63
|
+
super(checksum_query(query))
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# Substitutes any checksumed properties with the checksums of their
|
68
|
+
# values.
|
69
|
+
#
|
70
|
+
# @param [DataMapper::Undefined, DataMapper::Query, Hash] query
|
71
|
+
# The query.
|
72
|
+
#
|
73
|
+
# @return [DataMapper::Collection]
|
74
|
+
# The matching resources.
|
75
|
+
#
|
76
|
+
def all(query=Undefined)
|
77
|
+
super(checksum_query(query))
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
# Filters a query, replacing the checksumed properties, with their
|
82
|
+
# accompanying checksums.
|
83
|
+
#
|
84
|
+
# @param [Hash, DataMapper::Undefined] query
|
85
|
+
# The query to filter.
|
86
|
+
#
|
87
|
+
# @return [Hash, DataMapper::Undefined]
|
88
|
+
# The filtered query.
|
89
|
+
#
|
90
|
+
def checksum_query(query)
|
91
|
+
return query unless query.kind_of?(Hash)
|
92
|
+
|
93
|
+
new_query = {}
|
94
|
+
|
95
|
+
query.each do |name,value|
|
96
|
+
if (name.respond_to?(:to_sym) && checksumed_properties.include?(name.to_sym))
|
97
|
+
new_query[:"#{name}_checksum"] = calculate_checksum(value)
|
98
|
+
else
|
99
|
+
new_query[name] = value
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
return new_query
|
104
|
+
end
|
105
|
+
|
106
|
+
protected
|
107
|
+
|
108
|
+
#
|
109
|
+
# Defines a checksum property for another property.
|
110
|
+
#
|
111
|
+
# @param [Symbol] name
|
112
|
+
# The name of the property to checksum.
|
113
|
+
#
|
114
|
+
# @param [Hash] options
|
115
|
+
# Additional options.
|
116
|
+
#
|
117
|
+
# @option options [Boolean] :unique (true)
|
118
|
+
# Specifies whether the checksums are to be unique, or if
|
119
|
+
# duplicates are allowed.
|
120
|
+
#
|
121
|
+
def checksum(name,options={})
|
122
|
+
# build the checksum property options
|
123
|
+
property_options = {
|
124
|
+
:length => 64,
|
125
|
+
:required => true,
|
126
|
+
:default => lambda { |resource,property|
|
127
|
+
calculate_checksum(resource.attribute_get(name))
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
if options.fetch(:unique,true)
|
132
|
+
property_options[:unique] = true
|
133
|
+
else
|
134
|
+
property_options[:index] = true
|
135
|
+
end
|
136
|
+
|
137
|
+
property :"#{name}_checksum", String, property_options
|
138
|
+
checksumed_properties << name
|
139
|
+
end
|
140
|
+
end # ClassMethods
|
141
|
+
end # Checksumed
|
142
|
+
end # Is
|
143
|
+
end # DataMapper
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'models/url'
|
3
|
+
|
4
|
+
describe DataMapper::Is::Checksumed do
|
5
|
+
subject { Url }
|
6
|
+
|
7
|
+
before(:all) { subject.auto_migrate! }
|
8
|
+
|
9
|
+
let(:url) { 'http://AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' }
|
10
|
+
let(:url_checksum) { '7aea54fe73fda6bca28f47ea57f1af1a6bc7b28ef323a3b85763131baad46e8e' }
|
11
|
+
|
12
|
+
it "should calculate the checksums of properties" do
|
13
|
+
resource = subject.new(:url => url)
|
14
|
+
|
15
|
+
resource.url_checksum.should == url_checksum
|
16
|
+
end
|
17
|
+
|
18
|
+
context "queries" do
|
19
|
+
before(:all) do
|
20
|
+
subject.create(:url => 'http://foo/')
|
21
|
+
subject.create(:url => url)
|
22
|
+
subject.create(:url => 'http://bar/')
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should find the first resource with a matching checksum" do
|
26
|
+
resource = subject.first(:url => url)
|
27
|
+
|
28
|
+
resource.url_checksum.should == url_checksum
|
29
|
+
resource.url.should == url
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should find all resources with a matching checksum" do
|
33
|
+
resources = subject.all(:url => url)
|
34
|
+
|
35
|
+
resources.length.should == 1
|
36
|
+
resources[0].url_checksum.should == url_checksum
|
37
|
+
resources[0].url.should == url
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/models/url.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'dm-core/spec/setup'
|
3
|
+
require 'dm-core/spec/lib/adapter_helpers'
|
4
|
+
|
5
|
+
require 'dm-is-checksumed'
|
6
|
+
|
7
|
+
DataMapper::Logger.new(STDERR, :debug) if ENV['DEBUG']
|
8
|
+
DataMapper::Spec.setup
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.extend(DataMapper::Spec::Adapters::Helpers)
|
12
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'models/url'
|
3
|
+
|
4
|
+
describe DataMapper::Is::Checksumed do
|
5
|
+
subject { Url }
|
6
|
+
|
7
|
+
before(:all) { subject.auto_migrate! }
|
8
|
+
|
9
|
+
it "should record which properties are checksumed" do
|
10
|
+
subject.checksumed_properties.should include(:url)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should add accompanying checksum properties" do
|
14
|
+
subject.properties.should be_named(:url_checksum)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should make the checksum properties unique by default" do
|
18
|
+
subject.properties[:url_checksum].should be_unique
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "checksum" do
|
22
|
+
let(:expected) { '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824' }
|
23
|
+
|
24
|
+
it "should calculate SHA256 checksums" do
|
25
|
+
subject.calculate_checksum('hello').should == expected
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should convert non-Strings to Strings" do
|
29
|
+
subject.calculate_checksum(:hello).should == expected
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "checksum_query" do
|
34
|
+
let(:url) { 'http://AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' }
|
35
|
+
let(:url_checksum) { '7aea54fe73fda6bca28f47ea57f1af1a6bc7b28ef323a3b85763131baad46e8e' }
|
36
|
+
|
37
|
+
it "should not modify Undefined queries" do
|
38
|
+
subject.checksum_query(DataMapper::Undefined).should == DataMapper::Undefined
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should replace checksumed properties with their checksums" do
|
42
|
+
new_query = subject.checksum_query({:url => url})
|
43
|
+
|
44
|
+
new_query.should_not have_key(:url)
|
45
|
+
new_query[:url_checksum].should == url_checksum
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not replace un-checksumed properties" do
|
49
|
+
new_query = subject.checksum_query({:id => 2, :url => url})
|
50
|
+
|
51
|
+
new_query[:id].should == 2
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-is-checksumed
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Postmodern
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-17 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: "1.0"
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: yard
|
28
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.6.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.0.0
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *id003
|
48
|
+
description: A DataMapper plugin which adds checksum properties to a Model, referencing other properties.
|
49
|
+
email:
|
50
|
+
- postmodern.mod3@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- README.md
|
57
|
+
- ChangeLog.md
|
58
|
+
- LICENSE.txt
|
59
|
+
files:
|
60
|
+
- .document
|
61
|
+
- .rspec
|
62
|
+
- .yardopts
|
63
|
+
- ChangeLog.md
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- dm-is-checksumed.gemspec
|
69
|
+
- gemspec.yml
|
70
|
+
- lib/dm-is-checksumed.rb
|
71
|
+
- lib/dm-is-checksumed/is/checksumed.rb
|
72
|
+
- spec/integration/checksumed_spec.rb
|
73
|
+
- spec/models/url.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/unit/checksumed_spec.rb
|
76
|
+
homepage: http://github.com/postmodern/dm-is-checksumed
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 1.3.6
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: dm-is-checksumed
|
99
|
+
rubygems_version: 1.7.2
|
100
|
+
signing_key:
|
101
|
+
specification_version: 3
|
102
|
+
summary: Adds checksum properties to DataMapper Models.
|
103
|
+
test_files:
|
104
|
+
- spec/integration/checksumed_spec.rb
|
105
|
+
- spec/unit/checksumed_spec.rb
|