Module: DataClass::ClassMethods

Defined in:
lib/emery/dataclass.rb

Instance Method Summary collapse

Instance Method Details

#json_attributesObject



47
48
49
# File 'lib/emery/dataclass.rb', line 47

def json_attributes
  @json_attributes
end

#jsoner_deserialize(json_value) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/emery/dataclass.rb', line 67

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



76
77
78
79
80
81
82
# File 'lib/emery/dataclass.rb', line 76

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



51
52
53
54
55
56
57
# File 'lib/emery/dataclass.rb', line 51

def val(name, type)
  if @json_attributes == nil
    @json_attributes = {}
  end
  @json_attributes[name] = type
  attr_reader name
end

#var(name, type) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/emery/dataclass.rb', line 59

def var(name, type)
  if @json_attributes == nil
    @json_attributes = {}
  end
  @json_attributes[name] = type
  attr_accessor name
end