@@ -49,6 +49,26 @@ Or install it yourself as:
4949
5050ClassKit entities can be created by implementing the ` extend ClassKit ` extend into the entity class and then using the ` attr_accessor_type ` method to register attributes as above inplace of the standard ruby ` attr_accessor ` method.
5151
52+ ClassKit entity attributes can use a name alias in order to parse to/from hashes/json with different key names, see the example below:
53+
54+ class Address
55+ extend ClassKit
56+
57+ attr_accessor_type :line1, type: String, alias_name: :l1
58+ attr_accessor_type :line2, alias_name: :l2
59+ attr_accessor_type :postcode, alias_name: :pc
60+ end
61+
62+ { "l1": "23 the street", "l2": "the town", "pc": "ne1 4rt" }
63+
64+ To use alias names you must specify ` use_alias = true ` for the following helper methods:
65+
66+ helper.to_hash(entity, true)
67+ helper.from_hash(hash: hash_object, klass: entity_klass, use_alias: true)
68+
69+ helper.to_json(entity, true)
70+ helper.from_json(json: json_string, klass: entity_klass, use_alias: true)
71+
5272### attr_accessor_type
5373
5474This method is used to add typed attributes to a class.
@@ -111,12 +131,13 @@ Example:
111131
112132 helper.is_class_kit?(obj)
113133
114- #### #to_hash(object)
134+ #### #to_hash(object, use_alias )
115135
116136This method is called to convert a ClassKit entity into a ` Hash ` .
117137
118138[ Params]
119- - ` object ` This is the ClassKit entity to convert.
139+ - ` object ` [ Required] This is the ClassKit entity to convert.
140+ - ` use_alias ` [ Optional] [ Default=false] This is used to specify if attribute alias names should be used.
120141
121142[ Return]
122143
@@ -126,14 +147,15 @@ Example:
126147
127148 hash = helper.to_hash(obj)
128149
129- #### #from_hash(hash:,klass:)
150+ #### #from_hash(hash:,klass:, use_alias: )
130151
131152This method is called to convert a ` Hash ` into a ClassKit entity.
132153
133154[ Params]
134- - ` hash: ` This is the ` Hash ` to convert.
135- - ` klass: ` This is the class of the ClassKit entity you want to convert the ` Hash ` into.
136- (NOTE: It should be the fully qualified class name including modules)
155+ - ` hash: ` [ Required] This is the ` Hash ` to convert.
156+ - ` klass: ` [ Required] This is the class of the ClassKit entity you want to convert the ` Hash ` into.
157+ > NOTE: It should be the fully qualified class name including modules
158+ - ` use_alias ` [ Optional] [ Default=false] This is used to specify if attribute alias names should be used.
137159
138160[ Return]
139161
@@ -143,12 +165,13 @@ Example:
143165
144166 entity = helper.from_hash(hash: hsh, klass: Contact)
145167
146- #### #to_json(object)
168+ #### #to_json(object, use_alias )
147169
148170This method is called to convert a ClassKit entity into JSON.
149171
150172[ Params]
151173- ` object ` This is the ClassKit entity to convert.
174+ - ` use_alias ` [ Optional] [ Default=false] This is used to specify if attribute alias names should be used.
152175
153176[ Return]
154177
@@ -158,14 +181,15 @@ Example:
158181
159182 json_string = helper.to_json(obj)
160183
161- #### #from_json(json:, klass:)
184+ #### #from_json(json:, klass:, use_alias: )
162185
163186This method is called to convert a JSON string into a ClassKit entity.
164187
165188[ Params]
166189- ` json: ` This is the JSON string to convert.
167190- ` klass: ` This is the class of the ClassKit entity you want to convert the JSON into.
168- (NOTE: It should be the fully qualified class name including modules)
191+ > NOTE: It should be the fully qualified class name including modules
192+ - ` use_alias ` [ Optional] [ Default=false] This is used to specify if attribute alias names should be used.
169193
170194[ Return]
171195
0 commit comments