wolf_core 1.0.129 → 1.1.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: 91129e0763fe824d498dfced45a91b9549410bd42137525075401fab8da28894
4
- data.tar.gz: 11a1b4ed69b47e7a14c517ba879858a0bf55dff22beba9a7d231ec9eb89d1414
3
+ metadata.gz: db722de611536ac8908fc92df46aef74cdf6ac06388d6067a7d7decac2cb22c1
4
+ data.tar.gz: 81f5e2823ba6e0df5d3f9b190c56a4145d5c0213aaefadb1e756390ceeb0351e
5
5
  SHA512:
6
- metadata.gz: a19b37f2e8c9a2c953c29daec2a538853522c7c8a6eaa2015f52fb6b2d124e1a5800bc2495973601923c73c5d86dd440985b70f8a9fc1cb589e196ce183cb0fc
7
- data.tar.gz: 8673f95e1d39b74e42240c4e23764124e6f1a233d3fc8abc15c3a48cf6ce60a4f5a37e79033d4948a095a1a0398353dfcb51fcb487309c8fda6071db1fe393d0
6
+ metadata.gz: e0658df5828cf194bc6ea8509aee4c53511cfe0f2ed22c25100d60b66fbf9f833d2d267c44be5925a8d440c65749886126acf23deca689742dfdd382948e1187
7
+ data.tar.gz: 77d205bc4db37a4805b508d5586eec01218266dfcb501157f26fb3921daa4fedc5774b61ff0812c95606ce17e09dce787344e211fd25ea4808c73389ab7739fe
@@ -4,12 +4,14 @@ module WolfCore
4
4
  class InstanceApplicationSerializer
5
5
  attr_reader :options, :object
6
6
 
7
- def initialize(options: nil)
7
+ def initialize(object: nil, collection: nil, options: nil)
8
+ @object = object
9
+ @collection = collection
8
10
  @options = options || {}
9
11
  end
10
12
 
11
13
  def self.attributes(*attrs)
12
- _attributes.concat(attrs.map(&:to_sym))
14
+ _attributes.concat(attrs.map(&:to_sym)).uniq!
13
15
  end
14
16
 
15
17
  def self.has_many(relationship, serializer:) # rubocop:disable Naming/PredicateName
@@ -28,12 +30,14 @@ module WolfCore
28
30
  @_relationships ||= {}
29
31
  end
30
32
 
31
- def serialize_all(collection:, options: nil)
33
+ def serialize_all(collection: nil, options: nil)
32
34
  Result.try do
35
+ @collection = collection if collection
36
+
33
37
  options ||= {}
34
38
  @options = @options.merge(options)
35
39
 
36
- results = collection.map do |item|
40
+ results = @collection.map do |item|
37
41
  result = serialize(object: item, options: @options)
38
42
  result.raise_error
39
43
  result
@@ -42,33 +46,33 @@ module WolfCore
42
46
  end
43
47
  end
44
48
 
45
- def serialize(object:, options: nil)
49
+ def serialize(object: nil, options: nil)
46
50
  Result.try do
47
- @object = object
51
+ @object = object if object
48
52
  options ||= {}
49
53
  @options = @options.merge(options)
50
54
 
51
- serialized_object = as_json(object: object).with_indifferent_access
55
+ serialized_object = as_json(object: @object).with_indifferent_access
52
56
  Result.success(data: { serialized_object: serialized_object })
53
57
  end
54
58
  end
55
59
 
56
60
  private
57
61
 
58
- def serialized_attributes(object:)
62
+ def serialized_attributes
59
63
  self.class._attributes.each_with_object({}) do |attribute, hash|
60
64
  hash[attribute] = if respond_to?(attribute, true)
61
65
  send(attribute)
62
- elsif object.respond_to?(attribute)
63
- object.public_send(attribute)
66
+ elsif @object.respond_to?(attribute)
67
+ @object.public_send(attribute)
64
68
  end
65
69
  end
66
70
  end
67
71
 
68
- def serialized_relationships(object:)
72
+ def serialized_relationships
69
73
  self.class._relationships.each_with_object({}) do |(key, serializer_class), hash|
70
74
  serializer = serializer_class.new(options: @options)
71
- related_object = object.public_send(key)
75
+ related_object = @object.public_send(key)
72
76
  hash[key] = if related_object.is_a?(Enumerable)
73
77
  serializer.serialize_all(collection: related_object)
74
78
  else
@@ -77,9 +81,9 @@ module WolfCore
77
81
  end
78
82
  end
79
83
 
80
- def as_json(object:)
81
- attributes = serialized_attributes(object: object)
82
- relationships = serialized_relationships(object: object)
84
+ def as_json
85
+ attributes = serialized_attributes
86
+ relationships = serialized_relationships
83
87
  attributes.merge(relationships)
84
88
  end
85
89
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module WolfCore
4
+ class NoSqlDbInstanceDataSource
5
+ def self.init(region: "us-east-1")
6
+ return @instance if @instance
7
+
8
+ dynamodb_client = Aws::DynamoDB::Client.new(region: region)
9
+ @instance = new(client: dynamodb_client)
10
+ end
11
+
12
+ class << self
13
+ attr_reader :instance
14
+ end
15
+
16
+ def initialize(client:)
17
+ @client = client
18
+ end
19
+
20
+ def put_item(table_name:, item:)
21
+ Result.try do
22
+ response = @client.put_item({
23
+ table_name: table_name,
24
+ item: item
25
+ })
26
+ Result.success(data: { response: response })
27
+ end
28
+ end
29
+
30
+ def get_item(table_name:, key:)
31
+ Result.try do
32
+ get_params = {
33
+ table_name: table_name,
34
+ key: key
35
+ }
36
+ item = @client.get_item(get_params).item
37
+ Result.success(data: { item: item })
38
+ end
39
+ end
40
+
41
+ def get_item_by_index(table_name:, key_condition_expression:, expression_attribute_values:, index_name: nil)
42
+ Result.try do
43
+ query(
44
+ table_name: table_name,
45
+ key_condition_expression: key_condition_expression,
46
+ expression_attribute_values: expression_attribute_values,
47
+ index_name: index_name,
48
+ limit: 1
49
+ ).fold do |data|
50
+ items = data.items
51
+ Result.success(data: { item: items.first })
52
+ end
53
+ end
54
+ end
55
+
56
+ def query(table_name:, key_condition_expression:, expression_attribute_values:, index_name: nil, limit: nil)
57
+ Result.try do
58
+ query_params = {
59
+ table_name: table_name,
60
+ key_condition_expression: key_condition_expression,
61
+ expression_attribute_values: expression_attribute_values
62
+ }
63
+ query_params[:index_name] = index_name if index_name
64
+ query_params[:limit] = limit if limit
65
+
66
+ items = @client.query(query_params).items
67
+ Result.success(data: { items: items })
68
+ end
69
+ end
70
+ end
71
+ end
@@ -129,9 +129,12 @@ module WolfCore
129
129
  address
130
130
  end
131
131
 
132
- def split_name(full_name, name_range: nil, lastname_range: nil)
133
- words = full_name.split
132
+ def split_name(full_name, name_range: nil, lastname_range: nil, if_one_word: nil)
133
+ if_one_word = {} unless if_one_word.is_a?(Hash)
134
+ if_one_word.merge!(lastname: "-") if if_one_word[:lastname].nil?
135
+ words = full_name.strip.split
134
136
 
137
+ return { first_name: words[0], last_name: if_one_word[:lastname] } if words.length == 1
135
138
  return { first_name: words[0], last_name: words[1] } if words.length == 2
136
139
 
137
140
  name_range ||= 0..-2
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.129"
4
+ VERSION = "1.1.1"
5
5
  end
data/lib/wolf_core.rb CHANGED
@@ -18,4 +18,5 @@ require 'wolf_core/utils/file_utils'
18
18
  WolfCore::FileUtils.require_relative_folder(__dir__, 'wolf_core')
19
19
  WolfCore::LambdaFunctionDataSource.init
20
20
  WolfCore::InMemoryStorageDataSource.init
21
- WolfCore::NoSqlDbDataSource.init
21
+ WolfCore::NoSqlDbDataSource.init
22
+ WolfCore::NoSqlDbInstanceDataSource.init
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.129
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo
@@ -146,6 +146,7 @@ files:
146
146
  - lib/wolf_core/infrastructure/lambda_function_data_source.rb
147
147
  - lib/wolf_core/infrastructure/lambda_function_operations.rb
148
148
  - lib/wolf_core/infrastructure/no_sql_db_data_source.rb
149
+ - lib/wolf_core/infrastructure/no_sql_db_instance_data_source.rb
149
150
  - lib/wolf_core/infrastructure/no_sql_db_operations.rb
150
151
  - lib/wolf_core/utils/array_utils.rb
151
152
  - lib/wolf_core/utils/async_utils.rb