Module: 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
51 52 53 |
# File 'lib/emery/dataclass.rb', line 51 def json_attributes @json_attributes end |
#jsoner_deserialize(json_value) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/emery/dataclass.rb', line 71 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
80 81 82 83 84 85 86 |
# File 'lib/emery/dataclass.rb', line 80 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
55 56 57 58 59 60 61 |
# File 'lib/emery/dataclass.rb', line 55 def val(name, type) if @json_attributes == nil @json_attributes = {} end @json_attributes[name] = type attr_reader name end |
#var(name, type) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/emery/dataclass.rb', line 63 def var(name, type) if @json_attributes == nil @json_attributes = {} end @json_attributes[name] = type attr_accessor name end |