emerald_odm 1.2.1 → 1.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 801a45bf7c2940c3547f9260d255c47b2b87fc8becad126899ea1e8c078aaf00
4
- data.tar.gz: de2e365a5c4e60dc4c79766ff624f8300e84245d09c6937169e7fa98c23771f3
3
+ metadata.gz: 58be3d671b46d8703d0308a6921e515a6b68e38da855a766c25bc01c9435c4c4
4
+ data.tar.gz: 8d6aa735ce20df9d627408d79d527774bd9dab8b40df5c6477053a4503b15c3d
5
5
  SHA512:
6
- metadata.gz: 40df24b7d653f85b7707d212ef97d661f196e3f3b96cd7dc6a7c735cd74bd44acc2708dc2700e894288f5fc9c12a4d818c7896315e353758b44381797f639f85
7
- data.tar.gz: '08e0a9504e115b9d296689b4467147b34f2a74cf8e9d3d7d4ea20788e504de2be38d65f72bd83b835fd893a6adcc89df6197dc7352f2e2a5f6965f50c437ffb2'
6
+ metadata.gz: 380ad74efa2cdd7eeca559c3a1fcd66fa3546b2abfa3812d4ea6e82621d90c73bc019551506a4b5aae6a01a8ef8fb3e8b6381cb8213bb450e289b2487eddf440
7
+ data.tar.gz: 01047b0d65a1c6067c24d967f6b300bbda7b7550915da8c96138da875ea23919caad7f59e596a7aa1fed9d02c1c1a98aedb8f55acddc9ddeebc7f03e2c8a5243
data/emerald_odm.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.email = ['mikael.santilio@gmail.com']
12
12
 
13
13
  spec.summary = 'MongoDB ODM'
14
- spec.description = 'Simple MongoDB ODM'
14
+ spec.description = 'A simple MongoDB ODM without Active Record'
15
15
  spec.homepage = 'https://github.com/MikaelSantilio/emerald-odm'
16
16
  spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
17
17
 
@@ -1,3 +1,3 @@
1
1
  module EmeraldODM
2
- VERSION = '1.2.1'
2
+ VERSION = '1.3.1'
3
3
  end
data/lib/emerald_odm.rb CHANGED
@@ -25,20 +25,20 @@ module EmeraldODM
25
25
  end
26
26
 
27
27
  class AttrInitializer
28
- attr_reader :document
28
+ attr_reader :_document
29
29
 
30
30
  def initialize(document)
31
31
  self.class.fields.each do |field|
32
32
  document_value = document[field]
33
33
  send("#{field}=", document_value) unless document_value.nil?
34
34
  end
35
- instance_variable_set('@document', document)
35
+ instance_variable_set('@_document', document)
36
36
  end
37
37
 
38
38
  # @return [Array]
39
39
  def self.fields
40
40
  public_instance_methods = self.public_instance_methods(false).grep(/=$/)
41
- rejected_attr_names = %w[document]
41
+ rejected_attr_names = %w[_document]
42
42
  fields = []
43
43
  public_instance_methods.each do |attr|
44
44
  attr_name = attr.to_s.gsub('=', '')
@@ -51,6 +51,41 @@ module EmeraldODM
51
51
  def nil?
52
52
  document.nil? || document.empty?
53
53
  end
54
+
55
+ def self.array_to_h(array)
56
+ array.map do |item|
57
+ if item.is_a?(AttrInitializer)
58
+ item.to_h
59
+ elsif item.is_a?(Array)
60
+ array_to_h(item)
61
+ else
62
+ item
63
+ end
64
+ end
65
+ end
66
+
67
+ def to_h
68
+ hash = {}
69
+ known_fields = self.class.fields
70
+ unknown_fields = document.keys - known_fields
71
+ unknown_fields.reject! { |field| field.start_with?('_') }
72
+ known_fields.each do |field|
73
+ value = send(field)
74
+ if value.is_a?(AttrInitializer)
75
+ hash[field] = value.to_h
76
+ elsif value.is_a?(Array)
77
+ hash[field] = self.class.array_to_h(value)
78
+ else
79
+ hash[field] = value
80
+ end
81
+ end
82
+ unknown_fields.each do |field|
83
+ value = document[field]
84
+ hash[field] = value
85
+ end
86
+
87
+ hash
88
+ end
54
89
  end
55
90
 
56
91
  class Collection < AttrInitializer
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: emerald_odm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - mikael
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-28 00:00:00.000000000 Z
11
+ date: 2023-02-03 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: '0'
111
- description: Simple MongoDB ODM
111
+ description: A simple MongoDB ODM without Active Record
112
112
  email:
113
113
  - mikael.santilio@gmail.com
114
114
  executables: []
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  requirements: []
147
147
  rubyforge_project:
148
- rubygems_version: 2.7.6
148
+ rubygems_version: 2.7.7
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: MongoDB ODM