datory 1.0.0.rc9 → 1.0.0.rc10

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: 581be089a2e76a9d744e511869da46033d47904a1103343ec447d3d508a41ab8
4
- data.tar.gz: d9a70d1d761b4370fb7022401b8a830ab84fbe713c4fd33de4bb84daa73f4101
3
+ metadata.gz: 1e6931d46c3dd6929ceb0e0954c116eda8a1af5e8e04126adbec8a12a9505afd
4
+ data.tar.gz: cc93ae958f1d03eac21840ef6219bead651fbf78d00bb8a09f9f3adbf431fe69
5
5
  SHA512:
6
- metadata.gz: 59cccc7dd3abea33e60cc44be7ee1f9406dccff627ff560cdd76c1e5cff66ec6d255df87d666637745efe9e13630c5f668ff9b6454472deb6a16653740401ca2
7
- data.tar.gz: dc3d890966553c776ea4b5a0269c75bc8690f9b591d0f18538d81bade8b8facd9670a750626d096edb359313964c66aa10b2318ebe1517d10e591a7c796ae691
6
+ metadata.gz: 4f93c7d0bfb210d5b7f71fe0334f9e5d92aaa4d2d915ea7b1c9e116734ac46e863cb59405a95c8ba30c355dd712b830d03db6901bd9a740cc179bf4722c25cc3
7
+ data.tar.gz: d94430f29e119f6e64601c4f3c9258a4b9e8f4e9168edf416e7f6c12628d510158a639ecd1e101e38e79eb7c7439ad84422c2a0f5ae22b977b8eee6e4109c1bc
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ class Descriptor
6
+ def self.describe(...)
7
+ new.describe(...)
8
+ end
9
+
10
+ def describe(collection_of_attributes:) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
11
+ headings = []
12
+ rows = []
13
+
14
+ headings << "Attribute"
15
+ headings << "From"
16
+ headings << "To"
17
+ headings << "As"
18
+
19
+ collection_of_attributes.each do |attribute|
20
+ row = []
21
+
22
+ row << attribute.name
23
+
24
+ from_type = attribute.options.fetch(:from)
25
+
26
+ row << from_type
27
+ row << attribute.options.fetch(:to, attribute.name)
28
+ row << attribute.options.fetch(:as, attribute.options.fetch(:include, from_type))
29
+
30
+ rows << row
31
+ end
32
+
33
+ Datory::Console.print_table(headings.uniq, rows)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ module Serialization
6
+ class Model
7
+ def self.prepare(...)
8
+ new.prepare(...)
9
+ end
10
+
11
+ def prepare(data) # rubocop:disable Metrics/MethodLength
12
+ if data.is_a?(Array)
13
+ data.map do |item|
14
+ if item.is_a?(Hash)
15
+ build(item)
16
+ else
17
+ item
18
+ end
19
+ end
20
+ elsif data.is_a?(Hash)
21
+ build(data)
22
+ else
23
+ data
24
+ end
25
+ end
26
+
27
+ def build(attributes = {}) # rubocop:disable Metrics/MethodLength
28
+ attributes.each do |key, value|
29
+ self.class.send(:attr_accessor, key)
30
+
31
+ instance_variable_set(:"@#{key}", value)
32
+
33
+ if value.is_a?(Array)
34
+ value.map! { |item| Datory::Attributes::Serialization::Model.prepare(item) }
35
+ instance_variable_set(:"@#{key}", value)
36
+ elsif value.is_a?(Hash)
37
+ instance_variable_set(:"@#{key}", Datory::Attributes::Serialization::Model.prepare(value))
38
+ else
39
+ instance_variable_set(:"@#{key}", value)
40
+ end
41
+ end
42
+
43
+ self
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -39,7 +39,7 @@ module Datory
39
39
  output_formatter.call(value: value)
40
40
  elsif [Date, Time, DateTime].include?(value.class)
41
41
  value.to_s
42
- elsif ActiveSupport::Duration == value.class
42
+ elsif value.instance_of?(ActiveSupport::Duration)
43
43
  value.iso8601
44
44
  else
45
45
  value
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "terminal-table"
4
+
5
+ module Datory
6
+ class Console
7
+ def self.print_table(headings, rows)
8
+ table = Terminal::Table.new(
9
+ headings: headings,
10
+ rows: rows,
11
+ style: { border_x: "~", border_i: "~" }
12
+ )
13
+
14
+ new(table.to_s).print
15
+ end
16
+
17
+ def initialize(text)
18
+ @text = text
19
+ end
20
+
21
+ def print
22
+ puts @text
23
+ end
24
+ end
25
+ end
@@ -4,6 +4,8 @@ module Datory
4
4
  module Context
5
5
  module Callable
6
6
  def serialize(model)
7
+ model = Datory::Attributes::Serialization::Model.prepare(model)
8
+
7
9
  Datory::Attributes::Serialization::Serializator.serialize(
8
10
  model: model,
9
11
  collection_of_attributes: collection_of_attributes
@@ -21,6 +23,16 @@ module Datory
21
23
  end
22
24
  end
23
25
 
26
+ def to_model(**attributes)
27
+ context = send(:new)
28
+
29
+ attributes.each do |attribute_name, attribute_value|
30
+ context.define_singleton_method(attribute_name) { attribute_value }
31
+ end
32
+
33
+ context
34
+ end
35
+
24
36
  # def build!(attributes = {})
25
37
  # context = send(:new)
26
38
  #
@@ -33,6 +45,12 @@ module Datory
33
45
  _build!(context, **attributes)
34
46
  end
35
47
 
48
+ def describe
49
+ Datory::Attributes::Descriptor.describe(
50
+ collection_of_attributes: collection_of_attributes
51
+ )
52
+ end
53
+
36
54
  private
37
55
 
38
56
  def _build!(context, **attributes)
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc9"
8
+ PRE = "rc10"
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datory
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc9
4
+ version: 1.0.0.rc10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-24 00:00:00.000000000 Z
11
+ date: 2024-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - '='
59
59
  - !ruby/object:Gem::Version
60
60
  version: 2.5.0.rc6
61
+ - !ruby/object:Gem::Dependency
62
+ name: terminal-table
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: zeitwerk
63
77
  requirement: !ruby/object:Gem::Requirement
@@ -198,12 +212,15 @@ files:
198
212
  - lib/datory.rb
199
213
  - lib/datory/attributes/attribute.rb
200
214
  - lib/datory/attributes/collection.rb
215
+ - lib/datory/attributes/descriptor.rb
201
216
  - lib/datory/attributes/dsl.rb
217
+ - lib/datory/attributes/serialization/model.rb
202
218
  - lib/datory/attributes/serialization/serializator.rb
203
219
  - lib/datory/attributes/tools/service_builder.rb
204
220
  - lib/datory/attributes/tools/service_factory.rb
205
221
  - lib/datory/attributes/workspace.rb
206
222
  - lib/datory/base.rb
223
+ - lib/datory/console.rb
207
224
  - lib/datory/context/callable.rb
208
225
  - lib/datory/context/dsl.rb
209
226
  - lib/datory/context/workspace.rb