denormalize_mm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/mongo_mapper/denormalization.rb +48 -0
- metadata +47 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module Denormalization
|
3
|
+
def self.included(mod)
|
4
|
+
mod.extend(MongoMapper::Denormalization::ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def denormalize_field(association, field, options={})
|
9
|
+
denormalize(association, field, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def denormalize_association(dest, options={})
|
13
|
+
options = options.dup
|
14
|
+
source = options.delete(:from)
|
15
|
+
|
16
|
+
if !source
|
17
|
+
raise "denormalize_association must take a from (source) option"
|
18
|
+
end
|
19
|
+
|
20
|
+
denormalize(source, dest, {
|
21
|
+
:target_field => dest,
|
22
|
+
}.merge(options))
|
23
|
+
end
|
24
|
+
|
25
|
+
def denormalize(association, field, options={})
|
26
|
+
method_name = "denormalize_#{association}_#{field}"
|
27
|
+
|
28
|
+
validation_method = options[:on] || "before_validation"
|
29
|
+
source_field_code = options[:source_field] || "#{association}.#{field}"
|
30
|
+
target_field_code = options[:target_field] || "#{association}_#{field}"
|
31
|
+
|
32
|
+
self.class_eval <<-CODE, __FILE__, __LINE__
|
33
|
+
#{validation_method} :#{method_name}
|
34
|
+
|
35
|
+
def #{method_name}
|
36
|
+
if self.#{association}
|
37
|
+
self.#{target_field_code} = #{source_field_code}
|
38
|
+
end
|
39
|
+
|
40
|
+
true
|
41
|
+
end
|
42
|
+
|
43
|
+
private :#{method_name}
|
44
|
+
CODE
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: denormalize_mm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Scott Taylor
|
9
|
+
- Andrew Pariser
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2014-06-23 00:00:00.000000000 Z
|
14
|
+
dependencies: []
|
15
|
+
description: Helpers to denormalize fields easily on mongo mapper models
|
16
|
+
email: scott@railsnewbie.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/mongo_mapper/denormalization.rb
|
22
|
+
homepage: http://github.com/GoLearnUp/denormalize_mm
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.24
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Denormalize fields easily in mongo mapper
|
47
|
+
test_files: []
|