Module: Emery::DataClass::ClassMethods
- Defined in:
- lib/emery/dataclass.rb
Instance Method Summary collapse
- #json_attributes ⇒ Object
- #jsoner_deserialize(json_value) ⇒ Object
- #jsoner_serialize(value) ⇒ Object
- #val(name, type) ⇒ Object
- #var(name, type) ⇒ Object
Instance Method Details
#json_attributes ⇒ Object
48 49 50 |
# File 'lib/emery/dataclass.rb', line 48 def json_attributes @json_attributes end |
#jsoner_deserialize(json_value) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/emery/dataclass.rb', line 68 def jsoner_deserialize(json_value) T.check(T.hash(String, NilableUntyped), json_value) parameters = @json_attributes.map do |attr, attr_type| attr_value = json_value[attr.to_s] [attr, Jsoner.deserialize(attr_type, attr_value)] end return self.new parameters.to_h end |
#jsoner_serialize(value) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/emery/dataclass.rb', line 77 def jsoner_serialize(value) T.check(self, value) attrs = @json_attributes.map do |attr, attr_type| [attr, Jsoner.serialize(attr_type, value.send(attr))] end return attrs.to_h end |
#val(name, type) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/emery/dataclass.rb', line 52 def val(name, type) if @json_attributes == nil @json_attributes = {} end @json_attributes[name] = type attr_reader name end |
#var(name, type) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/emery/dataclass.rb', line 60 def var(name, type) if @json_attributes == nil @json_attributes = {} end @json_attributes[name] = type attr_accessor name end |