mongoid-denormalize 0.3.0 → 0.3.1

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
  SHA256:
3
- metadata.gz: 601856686a5f670b81ae5704ecdfb107c251010190a2c0c4a1df87797a7480df
4
- data.tar.gz: e2258b9b466e37ee420e05f6f16c5eda0b1fd18a63d0fceca8961866a174f6f1
3
+ metadata.gz: 305306418eb9b932560215ef74e6aee0ea803444310dd9a36b82bc601026a268
4
+ data.tar.gz: 2ca4400f0e00dc5fa365637cd2002cd132b1c9e36f14f37f94e8716a5d4ae332
5
5
  SHA512:
6
- metadata.gz: 6ebddf92b6193919dc47464a2ab2ae9868b5c8a8b49ff2855d954fa226383d13ea5548e2848b845a4b0a04136d789588fda11798f2775a30b2306cae8fdaa059
7
- data.tar.gz: 84d082b7e3f53e312d42668791593287e88ecb8c135449109b14755113e0fa7e5d362d45962a4f8f8c8984776b53adb9d2c7b3eac7d2bd868a4a57c752dbb641
6
+ metadata.gz: 6f245dfef2f06c8d07f472225751257f6173787ab6ee4baacccec67caee4d9fe422acd732374055d52c38eb161d2ea7725078f4406a0c8c38b8a6126fb98bacc
7
+ data.tar.gz: 2255a8d098dbf6796aa0e3de5586ec38bc34a79c317098a928d5e757c2c9c6ec21e15e7917d38203a1c0ec4adbfa19337cc40df32bf0f4bb6a13252f62cb1ffc
data/.travis.yml CHANGED
@@ -4,6 +4,11 @@ rvm:
4
4
  - 2.3.4
5
5
  before_install: gem install bundler -v 1.15.4
6
6
 
7
+ gemfiles:
8
+ - Gemfile
9
+ - gemfiles/mongoid-5.0.gemfile
10
+ - gemfiles/mongoid-6.0.gemfile
11
+
7
12
  services:
8
13
  - mongodb
9
14
 
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
+ gemspec path: '../'
5
+
6
+ gem 'rake'
7
+ gem 'mongoid', "~> 5.0"
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-normalize-strings.gemspec
4
+ gemspec path: '../'
5
+
6
+ gem 'rake'
7
+ gem 'mongoid', "~> 6.0"
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Denormalize
3
- VERSION = "0.3.0"
3
+ VERSION = "0.3.1"
4
4
  end
5
5
  end
@@ -1,4 +1,4 @@
1
- require "mongoid-denormalize/version"
1
+ require 'mongoid-denormalize/version'
2
2
 
3
3
  module Mongoid
4
4
  module Denormalize
@@ -14,10 +14,10 @@ module Mongoid
14
14
  raise ArgumentError, 'Option :from is needed (e.g. denormalize :name, from: :user).'
15
15
  end
16
16
 
17
- fields = Mongoid::Denormalize.get_fields_with_names(fields, options)
17
+ fields = Mongoid::Denormalize.get_fields_with_names(self, fields, options)
18
18
 
19
19
  # Add fields to model
20
- fields.each { |field| field field[:as] }
20
+ fields.each { |field| field field[:as], type: field[:type] }
21
21
 
22
22
  # Add hooks
23
23
  Mongoid::Denormalize.add_hook_to_child(self, fields, options)
@@ -26,7 +26,9 @@ module Mongoid
26
26
  end
27
27
 
28
28
  # Check options and return name for each field
29
- def self.get_fields_with_names(fields, options)
29
+ def self.get_fields_with_names(child_class, fields, options)
30
+ parent = parent_class(child_class, options[:from].to_s)
31
+
30
32
  if options.include?(:as)
31
33
  options[:as] = [options[:as]] unless options[:as].is_a?(Array)
32
34
 
@@ -34,12 +36,18 @@ module Mongoid
34
36
  raise ArgumentError, 'When option :as is used you must pass a name for each field.'
35
37
  end
36
38
 
37
- return fields.map.with_index { |field, index| {name: field, as: options[:as][index]} }
39
+ return fields.map.with_index do |field, index|
40
+ {name: field, as: options[:as][index], type: field_type(parent, field)}
41
+ end
38
42
  elsif options.include?(:prefix)
39
- return fields.map { |field| {name: field, as: "#{options[:prefix]}_#{field}"} }
43
+ return fields.map do |field|
44
+ {name: field, as: "#{options[:prefix]}_#{field}", type: field_type(parent, field)}
45
+ end
40
46
  end
41
47
 
42
- fields.map { |field| {name: field, as: "#{options[:from]}_#{field}"} }
48
+ fields.map do |field|
49
+ {name: field, as: "#{options[:from]}_#{field}", type: field_type(parent, field)}
50
+ end
43
51
  end
44
52
 
45
53
  # Add hook to child class to denormalize fields when parent relation is changed
@@ -59,8 +67,7 @@ module Mongoid
59
67
  def self.add_hook_to_parent(child_class, fields, options)
60
68
  from = options[:from].to_s
61
69
 
62
- parent = (child_class.relations[from].class_name ||
63
- child_class.relations[from].name.capitalize).constantize
70
+ parent = parent_class(child_class, from)
64
71
 
65
72
  relation = parent.relations[child_class.relations[from].inverse_of.to_s] ||
66
73
  parent.relations[child_class.model_name.plural] ||
@@ -89,5 +96,16 @@ module Mongoid
89
96
  end
90
97
  end
91
98
  end
99
+
100
+ # Retrieve parent class
101
+ def self.parent_class(child_class, from)
102
+ (child_class.relations[from].class_name || child_class.relations[from].name.capitalize)
103
+ .constantize
104
+ end
105
+
106
+ # Retreive the type of a field from the given class
107
+ def self.field_type(klass, field)
108
+ klass.fields[field.to_s].options[:type]
109
+ end
92
110
  end
93
111
  end
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'bundler', '~> 1.15'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
27
27
  spec.add_development_dependency 'rspec', '~> 3.0'
28
- spec.add_dependency 'mongoid'
28
+ spec.add_dependency 'mongoid', ['>= 3.0', '< 7.0']
29
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-denormalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - rjurado01
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-19 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,20 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '3.0'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '7.0'
62
65
  type: :runtime
63
66
  prerelease: false
64
67
  version_requirements: !ruby/object:Gem::Requirement
65
68
  requirements:
66
69
  - - ">="
67
70
  - !ruby/object:Gem::Version
68
- version: '0'
71
+ version: '3.0'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '7.0'
69
75
  description: Helper module for denormalizing relations attributes in Mongoid models
70
76
  email:
71
77
  - rjurado01@gmail.com
@@ -82,6 +88,8 @@ files:
82
88
  - Rakefile
83
89
  - bin/console
84
90
  - bin/setup
91
+ - gemfiles/mongoid-5.0.gemfile
92
+ - gemfiles/mongoid-6.0.gemfile
85
93
  - lib/mongoid-denormalize.rb
86
94
  - lib/mongoid-denormalize/version.rb
87
95
  - mongoid-denormalize.gemspec
@@ -105,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
113
  version: '0'
106
114
  requirements: []
107
115
  rubyforge_project:
108
- rubygems_version: 2.7.3
116
+ rubygems_version: 2.7.6
109
117
  signing_key:
110
118
  specification_version: 4
111
119
  summary: Denormalize fields from relations