mongoid-denormalize 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cecaa0d338d86451e2d2808712767f0e6862aed4
4
+ data.tar.gz: 53e285e91e43bd2cedce3430fcf49fed0f953a63
5
+ SHA512:
6
+ metadata.gz: 4de17b1f7116ea41d2593dafa284e11a3c41058a8b35ea8a3dcbee2cf73f23a3732028f50764dd2703ac485165842de14634eab8cb14fb89a485dd538a680e5f
7
+ data.tar.gz: 6e944fc4822da56b58d2e7a582750e2a980d67dad51a194cc1898eb644fe336792ae49b9385b2d60b7d2ff96d6a513d27123fa879875e75c730802e6a6771a59
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.4
5
+ before_install: gem install bundler -v 1.15.4
6
+
7
+ services:
8
+ - mongodb
9
+
10
+ addons:
11
+ apt:
12
+ sources:
13
+ - mongodb-3.4-precise
14
+ packages:
15
+ - mongodb-org-server
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mongoid-denormalize.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 rjurado
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Mongoid::Denormalize
2
+ ![Build Status](https://travis-ci.org/rjurado01/mongoid-denormalize.svg?branch=master)
3
+
4
+ Helper module for denormalizing association attributes in Mongoid models
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'mongoid-denormalize'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install mongoid-denormalize
21
+
22
+ ## Usage
23
+
24
+ In your model:
25
+
26
+ ```ruby
27
+ # Include the helper method
28
+ include Mongoid::Denormalize
29
+
30
+ # Define your denormalized fields
31
+ denormalize :name, :email, from: :user
32
+ ```
33
+
34
+ You need to add `inverse_of` to `belongs_to` side if `has_many` target name is diferent from model:
35
+
36
+ ```ruby
37
+ class Club
38
+ ...
39
+ has_many :members, class_name: 'User'
40
+ end
41
+
42
+ class User
43
+ ...
44
+ belogns_to club, inverse_of: :members
45
+
46
+ denormalize :name, from: :club
47
+ end
48
+ ```
49
+
50
+ ## Example
51
+
52
+ ```ruby
53
+ class User
54
+ include Mongoid::Document
55
+ include Mongoid::Denormalize
56
+
57
+ field :name
58
+
59
+ has_many :books
60
+ end
61
+
62
+ class Book
63
+ include Mongoid::Document
64
+ include Mongoid::Denormalize
65
+
66
+ field :title
67
+
68
+ belongs_to :author
69
+
70
+ denormalize :name, from: :author
71
+ end
72
+
73
+ >> user = User.create(name: 'User1')
74
+ >> book = Book.create(title: 'Title', author: user)
75
+ >> book.author_name
76
+ "User1"
77
+
78
+ >> user.update_attributes(name: 'User1.1')
79
+ >> book.reload.author_name
80
+ "User1.1"
81
+
82
+ >> new_user = User.create(name: 'User2')
83
+ >> book.update_attributes(author: new_user)
84
+ >> book.reload.author_name
85
+ "User2"
86
+ ```
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rjurado01/mongoid-denormalize.
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mongoid/denormalize"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
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
@@ -0,0 +1,47 @@
1
+ require "mongoid-denormalize/version"
2
+
3
+ module Mongoid
4
+ module Denormalize
5
+ def self.included(base)
6
+ base.extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def denormalize(*args)
11
+ options = args.pop
12
+
13
+ unless options.is_a?(Hash) && (from = options[:from]&.to_s)
14
+ raise ArgumentError, 'Option :from is needed (e.g. delegate :name, from: :user).'
15
+ end
16
+
17
+ # Add fields to model
18
+ args.each { |field| field "#{from}_#{field}" }
19
+
20
+ # Denormalize fields when model is saved and 'from' has changed
21
+ before_save do
22
+ if send(from) && send("#{from}_id_changed?")
23
+ args.each do |field|
24
+ send("#{from}_#{field}=", send(from).send(field))
25
+ end
26
+ end
27
+ end
28
+
29
+ from_class = (relations[from].class_name || relations[from].name.capitalize).constantize
30
+ model_class = model_name.name
31
+ inverse_of = relations[from].inverse_of || model_name.route_key.pluralize
32
+
33
+ # When 'from' is updated, update all childs
34
+ from_class.send(:after_update) do
35
+ attributes = {}
36
+ args.each { |field| attributes["#{from}_#{field}"] = send(field) }
37
+
38
+ unless relations[inverse_of.to_s]
39
+ raise "Option :inverse_of is needed for 'belongs_to :#{from}' into #{model_class}."
40
+ end
41
+
42
+ send(inverse_of).update_all('$set' => attributes)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Denormalize
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'mongoid-denormalize/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'mongoid-denormalize'
9
+ spec.version = Mongoid::Denormalize::VERSION
10
+ spec.authors = ['rjurado01']
11
+ spec.email = ['rjurado01@gmail.com']
12
+
13
+ spec.summary = 'Denormalize fields from relations'
14
+ spec.description = 'Helper module for denormalizing relations attributes in Mongoid models'
15
+ spec.homepage = 'https://github.com/rjurado01/mongoid-denormalize'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.15'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_dependency 'mongoid'
29
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-denormalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rjurado01
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Helper module for denormalizing relations attributes in Mongoid models
70
+ email:
71
+ - rjurado01@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/mongoid-denormalize.rb
86
+ - lib/mongoid-denormalize/version.rb
87
+ - mongoid-denormalize.gemspec
88
+ homepage: https://github.com/rjurado01/mongoid-denormalize
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.2
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Denormalize fields from relations
112
+ test_files: []