data_mapped 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +136 -2
- data/data_mapped.gemspec +4 -4
- data/lib/data_mapped/join.rb +8 -0
- data/lib/data_mapped/model.rb +8 -0
- data/lib/data_mapped/permanent.rb +9 -3
- data/lib/data_mapped/searchable.rb +13 -1
- data/lib/data_mapped/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 335c405bdd0df4ca6217569d675c938553473d37
|
4
|
+
data.tar.gz: 3e78dace180017e3c8424f036b2243f8a3973a13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26d26bf585825d9b62a7442118f72d13c34309c4c7447b21e4d6a6a99025247cf2b68393873cc68c170405e6290c1d9a39ed6215f0d58cf5c9f5b78409bac211
|
7
|
+
data.tar.gz: 12e8c7fbcc73ca1a5eae2df3a322c02b60eadd4fbad34c62a9d06710ae2df22013e245bb72a20306200c6beb33381d3dfd10d8a318eda0cbf11f5076a1febcfa
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
# DataMapped
|
2
2
|
|
3
|
-
|
3
|
+
Ruby mixins for [DataMapper](http://datamapper.org/).
|
4
|
+
[Don't repeat yourself](#normal-model),
|
5
|
+
[control your join tables](#join-model-for-a-join-table),
|
6
|
+
[prevent your resources from being deleted](#permanent-model-that-cant-be-destroyed),
|
7
|
+
and [full-text search](#searchable-model) across your resources.
|
8
|
+
|
9
|
+
|
4
10
|
|
5
11
|
## Installation
|
6
12
|
|
@@ -16,9 +22,137 @@ Or install it yourself as:
|
|
16
22
|
|
17
23
|
$ gem install data_mapped
|
18
24
|
|
25
|
+
|
26
|
+
|
19
27
|
## Usage
|
20
28
|
|
21
|
-
|
29
|
+
### Normal Model
|
30
|
+
|
31
|
+
This mixin sets up your class for DataMapper, and adds `@id`, `@created_at`,
|
32
|
+
and `@updated_at` properties that behave as expected.
|
33
|
+
|
34
|
+
**Rationale**: Don't repeat yourself. Don't manually add the same standard
|
35
|
+
properties to each of your models.
|
36
|
+
|
37
|
+
require 'data_mapped/model'
|
38
|
+
|
39
|
+
class Movie
|
40
|
+
include DataMapped::Model
|
41
|
+
property :title, String, required: true
|
42
|
+
property :description, Text
|
43
|
+
end
|
44
|
+
|
45
|
+
### Join Model (for a Join Table)
|
46
|
+
|
47
|
+
This mixin sets up your class for DataMapper, and adds `@created_at` and
|
48
|
+
`@updated_at` properties that behave as expected. This join model differs
|
49
|
+
from the [normal model](#normal-model) in that the join model doesn't add an
|
50
|
+
`@id` property, as it expects for your class to define a composite primary key
|
51
|
+
using `#belongs_to` relationships.
|
52
|
+
|
53
|
+
**Rationale**: Tightly control your many-to-many associations. This is a
|
54
|
+
helpful pattern to do something to a movie resource any time an actor is
|
55
|
+
associated with it.
|
56
|
+
|
57
|
+
require 'data_mapped/join'
|
58
|
+
require 'data_mapped/model'
|
59
|
+
|
60
|
+
class Movie
|
61
|
+
include DataMapped::Model
|
62
|
+
property :title, String, required: true
|
63
|
+
property :description, Text
|
64
|
+
has n, :actors, through: Resource
|
65
|
+
end
|
66
|
+
|
67
|
+
class Actor
|
68
|
+
include DataMapped::Model
|
69
|
+
property :name, String, required: true
|
70
|
+
has n, :movies, through: Resource
|
71
|
+
end
|
72
|
+
|
73
|
+
class ActorMovie
|
74
|
+
include DataMapped::Join
|
75
|
+
belongs_to :actor, key: true
|
76
|
+
belongs_to :movie, key: true
|
77
|
+
end
|
78
|
+
|
79
|
+
**Integration note**: Your join model class name must be a
|
80
|
+
[Pascal case](http://c2.com/cgi/wiki?PascalCase), alphabetized concatenation
|
81
|
+
of the names of the two models that you're joining. Internally, DataMapper
|
82
|
+
introspects your join model class name and uses it to determine the name of
|
83
|
+
the join table in your database.
|
84
|
+
|
85
|
+
### Permanent Model (that Can't be Destroyed)
|
86
|
+
|
87
|
+
This mixin prevents resources of your class from being destroyed (at least in
|
88
|
+
the typical fashion using
|
89
|
+
[`#destroy`](http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Resource#destroy-instance_method)).
|
90
|
+
It's meant to be used in conjunction with either the
|
91
|
+
[normal model](#normal-model) or the
|
92
|
+
[join model](#join-model-for-a-join-table) mixin.
|
93
|
+
|
94
|
+
**Rationale**: Prevent accidentally deleting resources that shouldn't be
|
95
|
+
deleted, if you have itchy fingers and play around too much in `irb` on
|
96
|
+
production like I do. Our use case is that movies should be created, read,
|
97
|
+
and updated, but never deleted.
|
98
|
+
|
99
|
+
require 'data_mapped/model'
|
100
|
+
require 'data_mapped/permanent'
|
101
|
+
|
102
|
+
class Movie
|
103
|
+
include DataMapped::Model
|
104
|
+
include DataMapped::Permanent
|
105
|
+
property :title, String, required: true
|
106
|
+
property :description, Text
|
107
|
+
end
|
108
|
+
|
109
|
+
**Integration note**: You can still delete permanent models with
|
110
|
+
[`#destroy!`](http://rubydoc.info/github/datamapper/dm-core/master/DataMapper/Resource#destroy%21-instance_method).
|
111
|
+
In general, make a practice out of staying away from DataMapper's unsafe bang
|
112
|
+
(!) methods. These
|
113
|
+
[bang methods don't run validators or callbacks](http://datamapper.org/docs/create_and_destroy.html).
|
114
|
+
:-(
|
115
|
+
|
116
|
+
### Searchable Model
|
117
|
+
|
118
|
+
This mixin duct tapes your class together with
|
119
|
+
[Sunspot](http://sunspot.github.io/) and
|
120
|
+
[Solr](https://lucene.apache.org/solr/), and makes your class's resources
|
121
|
+
indexed and searchable. It's meant to be used in conjunction with the
|
122
|
+
[normal model](#normal-model) mixin.
|
123
|
+
|
124
|
+
**Rationale**: Everyone loves full-text search, and this makes it easy to get
|
125
|
+
DataMapper to talk to Sunspot. Include this mixin, define your model, then
|
126
|
+
use
|
127
|
+
[Sunspot's powerful DSL](http://sunspot.github.io/docs/index.html#Indexing_In_Depth)
|
128
|
+
to define what should be indexed and how it should be weighted.
|
129
|
+
|
130
|
+
require 'data_mapped/model'
|
131
|
+
require 'data_mapped/searchable'
|
132
|
+
|
133
|
+
class Movie
|
134
|
+
include DataMapped::Model
|
135
|
+
include DataMapped::Searchable
|
136
|
+
property :title, String, required: true
|
137
|
+
property :description, Text
|
138
|
+
has n, :actors, through: Resource
|
139
|
+
|
140
|
+
searchable auto_index: true, auto_remove: true do
|
141
|
+
text :title, boost: 2.0
|
142
|
+
text :description
|
143
|
+
text :actor_names, boost: 1.5 do
|
144
|
+
self.actors.map(&:name).join(', ')
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
class Actor
|
150
|
+
include DataMapped::Model
|
151
|
+
property :name, String, required: true
|
152
|
+
has n, :movies, through: Resource
|
153
|
+
end
|
154
|
+
|
155
|
+
|
22
156
|
|
23
157
|
## Contributing
|
24
158
|
|
data/data_mapped.gemspec
CHANGED
@@ -6,11 +6,11 @@ require 'data_mapped/version'
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = "data_mapped"
|
8
8
|
spec.version = DataMapped::VERSION
|
9
|
-
spec.authors = ["
|
9
|
+
spec.authors = ["Rajiv Bakulesh Shah"]
|
10
10
|
spec.email = ["brainix@gmail.com"]
|
11
|
-
spec.description = %q{
|
12
|
-
spec.summary = %q{
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.description = %q{Mixins for DataMapper}
|
12
|
+
spec.summary = %q{Mixins for DataMapper}
|
13
|
+
spec.homepage = "https://github.com/brainix/data_mapped"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files`.split($/)
|
data/lib/data_mapped/join.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
#-----------------------------------------------------------------------------#
|
2
|
+
# join.rb #
|
3
|
+
# #
|
4
|
+
# Copyright (c) 2013, Rajiv Bakulesh Shah. #
|
5
|
+
#-----------------------------------------------------------------------------#
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
require 'data_mapper'
|
2
10
|
|
3
11
|
|
data/lib/data_mapped/model.rb
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
#-----------------------------------------------------------------------------#
|
2
|
+
# model.rb #
|
3
|
+
# #
|
4
|
+
# Copyright (c) 2013, Rajiv Bakulesh Shah. #
|
5
|
+
#-----------------------------------------------------------------------------#
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
require 'data_mapper'
|
2
10
|
|
3
11
|
|
@@ -1,3 +1,11 @@
|
|
1
|
+
#-----------------------------------------------------------------------------#
|
2
|
+
# permanent.rb #
|
3
|
+
# #
|
4
|
+
# Copyright (c) 2013, Rajiv Bakulesh Shah. #
|
5
|
+
#-----------------------------------------------------------------------------#
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
require 'data_mapper'
|
2
10
|
|
3
11
|
|
@@ -15,9 +23,7 @@ module DataMapped
|
|
15
23
|
DataMapper::Model.append_inclusions(InstanceMethods)
|
16
24
|
|
17
25
|
def self.included(other)
|
18
|
-
other.class_eval
|
19
|
-
before :destroy, :vomit_before_destruction
|
20
|
-
end
|
26
|
+
other.class_eval { before :destroy, :vomit_before_destruction }
|
21
27
|
end
|
22
28
|
|
23
29
|
end
|
@@ -1,3 +1,11 @@
|
|
1
|
+
#-----------------------------------------------------------------------------#
|
2
|
+
# searchable.rb #
|
3
|
+
# #
|
4
|
+
# Copyright (c) 2013, Rajiv Bakulesh Shah. #
|
5
|
+
#-----------------------------------------------------------------------------#
|
6
|
+
|
7
|
+
|
8
|
+
|
1
9
|
require 'active_support/all'
|
2
10
|
require 'data_mapper'
|
3
11
|
require 'sunspot'
|
@@ -25,7 +33,11 @@ module DataMapped
|
|
25
33
|
end
|
26
34
|
|
27
35
|
def self.included(other)
|
28
|
-
other.class_eval
|
36
|
+
other.class_eval do
|
37
|
+
alias_method(:new_record?, :new?)
|
38
|
+
after :save, :reindex
|
39
|
+
after :destroy, :reindex
|
40
|
+
end
|
29
41
|
other.extend Sunspot::Rails::Searchable::ActsAsMethods
|
30
42
|
Sunspot::Adapters::DataAccessor.register(DataAccessor, other)
|
31
43
|
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, other)
|
data/lib/data_mapped/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_mapped
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- Rajiv Bakulesh Shah
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -108,7 +108,7 @@ dependencies:
|
|
108
108
|
- - ~>
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: 1.3.3
|
111
|
-
description:
|
111
|
+
description: Mixins for DataMapper
|
112
112
|
email:
|
113
113
|
- brainix@gmail.com
|
114
114
|
executables: []
|
@@ -129,7 +129,7 @@ files:
|
|
129
129
|
- lib/data_mapped/permanent.rb
|
130
130
|
- lib/data_mapped/searchable.rb
|
131
131
|
- lib/data_mapped/version.rb
|
132
|
-
homepage:
|
132
|
+
homepage: https://github.com/brainix/data_mapped
|
133
133
|
licenses:
|
134
134
|
- MIT
|
135
135
|
metadata: {}
|
@@ -152,5 +152,5 @@ rubyforge_project:
|
|
152
152
|
rubygems_version: 2.0.3
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
|
-
summary:
|
155
|
+
summary: Mixins for DataMapper
|
156
156
|
test_files: []
|