module DataClass

Public Class Methods

included(base) click to toggle source
# File lib/emery/dataclass.rb, line 46
def self.included(base)
  base.extend ClassMethods
end
new(params) click to toggle source
# File lib/emery/dataclass.rb, line 2
def initialize(params)
  self.class.json_attributes.each do |attr, attr_type|
    attr_value = params[attr]
    self.instance_variable_set("@#{attr}", T.check_var(attr, attr_type, attr_value))
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/emery/dataclass.rb, line 9
def ==(other)
  begin
    T.check(self.class, other)
    self.class.json_attributes.keys.each do |attr|
      if self.instance_variable_get("@#{attr}") != other.instance_variable_get("@#{attr}")
        return false
      end
    end
    return true
  rescue
    return false
  end
end
copy(params) click to toggle source
# File lib/emery/dataclass.rb, line 23
def copy(params)
  params.each do |attr, attr_value|
    if !self.class.json_attributes.key?(attr)
      raise TypeError.new("Non existing attribute #{attr}")
    end
  end
  new_params =
    self.class.json_attributes.map do |attr, attr_type|
      attr_value =
        if params.key?(attr)
          params[attr]
        else
          self.instance_variable_get("@#{attr}")
        end
      [attr, attr_value]
    end.to_h
  return self.class.new(new_params)
end
to_json() click to toggle source
# File lib/emery/dataclass.rb, line 42
def to_json
  return Jsoner.serialize(self.class, self)
end