class_kit 0.4.0 → 0.5.0

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
- SHA1:
3
- metadata.gz: 4c4c2e5ad372e1f531c744504badf4e475b4da8e
4
- data.tar.gz: 2db318e1138a6917be292c7111c02c0422d30156
2
+ SHA256:
3
+ metadata.gz: b81e243b2cf31eeaf9d1121ee76d9538662c5cb93e85a445492e35c4a40d8f81
4
+ data.tar.gz: e1ac5e6d7cbed16a01205991e8c78e154b4f126f6c4dbbde3a620678466bacd5
5
5
  SHA512:
6
- metadata.gz: 8f595e18e28fa3f59f2763e8df1f5dc6d3a7ec04f662187bc5ef60d8d9b676a7a7c0d05039ef9a182f22b350512d2cc7bf47f95538801996f8ca357185be14bc
7
- data.tar.gz: a169b775d07a1ae826ead253d4740058419da04cd204bb4100479fb0b07f47705f824a8dd0daaae2f4bbc11e2bc359ad17e4e513471df84dfee8af091436e472
6
+ metadata.gz: 9023dda17de237312d0bd6968ce16360bb6eedb10eaf375d47fff0d7ec1206a760e2557557f2ae338536e2e667ee1242ed4aba3842e2bcd41bad142e455f3109
7
+ data.tar.gz: ab848b6dbba992c5379a5fe935705f95737377e912e09a6e3a83e015e7f276fa51e098c133c147a5fc746bdb080488178780c46b8faffe00b309eb32a18096ae
@@ -1,15 +1,29 @@
1
1
  module ClassKit
2
- def attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, default: nil, auto_init: false,
3
- meta: {})
4
-
2
+ def attr_accessor_type(
3
+ name,
4
+ type: nil,
5
+ collection_type: nil,
6
+ allow_nil: true,
7
+ default: nil,
8
+ auto_init: false,
9
+ alias_name: nil,
10
+ meta: {})
5
11
  unless instance_variable_defined?(:@class_kit_attributes)
6
12
  instance_variable_set(:@class_kit_attributes, {})
7
13
  end
8
14
 
9
15
  attributes = instance_variable_get(:@class_kit_attributes)
10
16
 
11
- attributes[name] = { name: name, type: type, collection_type: collection_type, allow_nil: allow_nil,
12
- default: default, auto_init: auto_init, meta: meta }
17
+ attributes[name] = {
18
+ name: name,
19
+ type: type,
20
+ collection_type: collection_type,
21
+ allow_nil: allow_nil,
22
+ default: default,
23
+ auto_init: auto_init,
24
+ alias: alias_name,
25
+ meta: meta
26
+ }
13
27
 
14
28
  class_eval do
15
29
  define_method name do
@@ -16,17 +16,17 @@ module ClassKit
16
16
  "Class: #{klass} does not implement ClassKit.")
17
17
  end
18
18
 
19
- #This method is called to convert a ClassKit object into a Hash.
20
- def to_hash(object)
19
+ # This method is called to convert a ClassKit object into a Hash.
20
+ def to_hash(object, use_alias = false)
21
21
  validate_class_kit(object.class)
22
22
 
23
23
  hash = {}
24
24
 
25
25
  attributes = @attribute_helper.get_attributes(object.class)
26
26
  attributes.each do |attribute|
27
- key = attribute[:name]
27
+ key = use_alias ? (attribute[:alias] || attribute[:name]) : attribute[:name]
28
28
  type = attribute[:type]
29
- value = object.public_send(key)
29
+ value = object.public_send(attribute[:name])
30
30
  if value != nil
31
31
  hash[key] = if is_class_kit?(type)
32
32
  to_hash(value)
@@ -47,18 +47,18 @@ module ClassKit
47
47
  hash
48
48
  end
49
49
 
50
- #This method is called to convert a Hash into a ClassKit object.
51
- def from_hash(hash:, klass:)
50
+ # This method is called to convert a Hash into a ClassKit object.
51
+ def from_hash(hash:, klass:, use_alias: false)
52
52
  validate_class_kit(klass)
53
53
 
54
54
  @hash_helper.indifferent!(hash)
55
55
  entity = klass.new
56
56
  attributes = @attribute_helper.get_attributes(klass)
57
57
  attributes.each do |attribute|
58
- key = attribute[:name]
58
+ key = use_alias ? (attribute[:alias] || attribute[:name]) : attribute[:name]
59
59
  type = attribute[:type]
60
60
 
61
- #if the hash value is nil skip it
61
+ # if the hash value is nil skip it
62
62
  next if hash[key].nil?
63
63
 
64
64
  value = if is_class_kit?(type)
@@ -79,22 +79,22 @@ module ClassKit
79
79
  hash[key]
80
80
  end
81
81
 
82
- entity.public_send(:"#{key}=", value)
82
+ entity.public_send(:"#{attribute[:name]}=", value)
83
83
  end
84
84
 
85
85
  entity
86
86
  end
87
87
 
88
- #This method is called to convert a ClassKit object into JSON.
89
- def to_json(object)
90
- hash = to_hash(object)
88
+ # This method is called to convert a ClassKit object into JSON.
89
+ def to_json(object, use_alias = false)
90
+ hash = to_hash(object, use_alias)
91
91
  JSON.dump(hash)
92
92
  end
93
93
 
94
- #This method is called to convert JSON into a ClassKit object.
95
- def from_json(json:, klass:)
94
+ # This method is called to convert JSON into a ClassKit object.
95
+ def from_json(json:, klass:, use_alias: false)
96
96
  hash = JSON.load(json)
97
- from_hash(hash: hash, klass: klass)
97
+ from_hash(hash: hash, klass: klass, use_alias: use_alias)
98
98
  end
99
99
  end
100
100
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: class_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sage One
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-29 00:00:00.000000000 Z
11
+ date: 2018-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
131
  version: '0'
132
132
  requirements: []
133
133
  rubyforge_project:
134
- rubygems_version: 2.4.5
134
+ rubygems_version: 2.7.6
135
135
  signing_key:
136
136
  specification_version: 4
137
137
  summary: Toolkit for working with classes