dolly 3.1.0 → 3.1.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: 2bd1cf887f25cacb3b25be56354cf74c1771cd021514cb8bf733e31aa7198feb
4
- data.tar.gz: cd464f54583a1f417fb58e27f504ccce8ddd33f39f1f48eea0f3b66487313445
3
+ metadata.gz: b4a7f73673dc9600d2451b6ae0dd1ed5ca8c792f671f1a115405b751610b9077
4
+ data.tar.gz: 7765f1a58d5515aebce77a79fdde6e0aad8e8928867e39c45a26ac5321ef7cb9
5
5
  SHA512:
6
- metadata.gz: 5c2bddaf348f5028c10ea464ea3265a9653d741508a103d3bd4d3e83757261ead6de60d630db9a8fa512ba22e13cd36043debc8e8d7be457aa63079018a3d580
7
- data.tar.gz: f1c2ede8149132f5d6941fbbe0b4eb1ada18ed060eb76ccd8b4eeffec556590b2b8c8044331d6726de10a0758120a6595b07f6da5afdd86da95be655344ceb8b
6
+ metadata.gz: 9430a6c2bc2b951442751b3192778a7d73985d6cb3fa0864f5a9529695d20fec13cd4b451d8232f06dfc0a27cc6897acae734eb979c52b63a60d81aa7c5061c6
7
+ data.tar.gz: a46dd510d441efffe1b7998e64b5806fba6fee10470dc3c38d78a4be49bd18d48b50ad9d84b630ef0c04895934214821162a46e3bc3888a71c02171623bffdb6
@@ -6,10 +6,12 @@ require 'dolly/request_header'
6
6
  require 'dolly/exceptions'
7
7
  require 'dolly/configuration'
8
8
  require 'refinements/string_refinements'
9
+ require 'dolly/framework_helper'
9
10
 
10
11
  module Dolly
11
12
  class Connection
12
13
  include Dolly::Configuration
14
+ include Dolly::FrameworkHelper
13
15
  attr_reader :db, :app_env
14
16
 
15
17
  DEFAULT_HEADER = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
@@ -92,7 +94,12 @@ module Dolly
92
94
  raise Dolly::ResourceNotFound if res.status.to_i == 404
93
95
  raise Dolly::ServerError.new(res.status.to_i) if (400..600).include? res.status.to_i
94
96
  return res.header_str if method == :head
95
- Oj.load(res.body_str, symbol_keys: true)
97
+
98
+ data = Oj.load(res.body_str, symbol_keys: true)
99
+ return data unless rails?
100
+ data.with_indifferent_access
101
+ rescue Oj::ParseError
102
+ res.body_str
96
103
  end
97
104
 
98
105
  def values_to_json hash
@@ -15,6 +15,7 @@ require 'dolly/timestamp'
15
15
  require 'dolly/query_arguments'
16
16
  require 'dolly/document_creation'
17
17
  require 'dolly/class_methods_delegation'
18
+ require 'dolly/framework_helper'
18
19
  require 'refinements/string_refinements'
19
20
 
20
21
  module Dolly
@@ -35,6 +36,7 @@ module Dolly
35
36
  include Attachment
36
37
  include QueryArguments
37
38
  include ClassMethodsDelegation
39
+ include FrameworkHelper
38
40
 
39
41
  attr_writer :doc
40
42
 
@@ -46,7 +48,7 @@ module Dolly
46
48
  protected
47
49
 
48
50
  def doc
49
- @doc ||= {}
51
+ @doc ||= doc_for_framework
50
52
  end
51
53
 
52
54
  def init_ancestor_properties
@@ -59,5 +61,10 @@ module Dolly
59
61
  end
60
62
  end
61
63
  end
64
+
65
+ def doc_for_framework
66
+ return {} unless rails?
67
+ {}.with_indifferent_access
68
+ end
62
69
  end
63
70
  end
@@ -1,16 +1,23 @@
1
1
  require 'dolly/properties'
2
+ require 'dolly/framework_helper'
2
3
 
3
4
  module Dolly
4
5
  module DocumentCreation
5
6
  include Properties
7
+ include FrameworkHelper
6
8
 
7
9
  def from_doc(doc)
8
10
  attributes = property_clean_doc(doc)
9
- new(attributes).tap { |model| model.doc = doc }
11
+
12
+ new(attributes).tap do |model|
13
+ model.send(:doc).merge!(doc)
14
+ end
10
15
  end
11
16
 
12
17
  def from_json(json)
13
- from_doc(Oj.load(json, symbol_keys: true))
18
+ raw_data = Oj.load(json, symbol_keys: true)
19
+ data = rails? ? data.with_indifferent_access : raw_data
20
+ from_doc(data)
14
21
  end
15
22
 
16
23
  def create(attributes)
@@ -0,0 +1,7 @@
1
+ module Dolly
2
+ module FrameworkHelper
3
+ def rails?
4
+ defined?(ActiveSupport::HashWithIndifferentAccess)
5
+ end
6
+ end
7
+ end
@@ -30,7 +30,7 @@ module Dolly
30
30
  end
31
31
 
32
32
  def property_clean_doc(doc)
33
- doc.reject { |key, _value| !property_keys.include?(key) }
33
+ doc.reject { |key, _value| property_keys.exclude?(key.to_sym) }
34
34
  end
35
35
  end
36
36
  end
@@ -16,10 +16,15 @@ module Dolly
16
16
  def cast_value(value)
17
17
  return set_default if value.nil?
18
18
  return value unless class_name
19
- return self_klass.new(value) unless respond_to?(klass_sym)
19
+ return custom_class(value) unless respond_to?(klass_sym)
20
20
  send(klass_sym, value)
21
21
  end
22
22
 
23
+ def custom_class(value)
24
+ value = value.is_a?(Hash) ? value.symbolize_keys : value
25
+ self_klass.new(value)
26
+ end
27
+
23
28
  def boolean?
24
29
  [TrueClass, FalseClass].include?(class_name)
25
30
  end
@@ -32,6 +37,14 @@ module Dolly
32
37
  value.to_h
33
38
  end
34
39
 
40
+ def hash_with_indifferent_access_value(value)
41
+ if defined?(Rails)
42
+ value.to_h.with_indifferent_access
43
+ else
44
+ value.to_h
45
+ end
46
+ end
47
+
35
48
  def integer_value(value)
36
49
  value.to_i
37
50
  end
@@ -70,7 +83,13 @@ module Dolly
70
83
  end
71
84
 
72
85
  def klass_sym
73
- :"#{self_klass.name.underscore}_value"
86
+ klass_name = self_klass.
87
+ name.
88
+ split('::').
89
+ last.
90
+ underscore
91
+
92
+ :"#{klass_name}_value"
74
93
  end
75
94
 
76
95
  def self_klass
@@ -5,7 +5,7 @@ module Dolly
5
5
  assign_rev_properties(attributes)
6
6
 
7
7
  lambda do |property|
8
- name = property.key.to_sym
8
+ name = property.key
9
9
  next unless doc[name].nil?
10
10
  write_attribute(name, attributes[name])
11
11
  end
@@ -19,9 +19,9 @@ module Dolly
19
19
  end
20
20
 
21
21
  def write_attribute(key, value)
22
- value = set_property_value(key, value)
23
- instance_variable_set(:"@#{key}", value)
24
- update_doc(key, value)
22
+ casted_value = set_property_value(key, value)
23
+ instance_variable_set(:"@#{key}", casted_value)
24
+ update_doc(key, casted_value)
25
25
  end
26
26
 
27
27
  def valid_property?(name)
data/lib/dolly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "3.1.0"
2
+ VERSION = "3.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolly
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - javierg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-11 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -129,6 +129,7 @@ files:
129
129
  - lib/dolly/document_state.rb
130
130
  - lib/dolly/document_type.rb
131
131
  - lib/dolly/exceptions.rb
132
+ - lib/dolly/framework_helper.rb
132
133
  - lib/dolly/identity_properties.rb
133
134
  - lib/dolly/mango.rb
134
135
  - lib/dolly/mango_index.rb