class Mumukit::Platform::Model

Public Class Methods

accessors() click to toggle source
# File lib/mumukit/platform/model.rb, line 39
def self.accessors
  self.readers + self.attributes.map { |it| "#{it}=".to_sym }
end
define_attr_readers(readers, bool_readers) click to toggle source

Define the attribute readers for the model, given the normal accessor names and the boolean accessor names

# File lib/mumukit/platform/model.rb, line 45
def self.define_attr_readers(readers, bool_readers)
  attr_reader(*readers)
  bool_readers.each { |it| define_method("#{it}?") { !!send(it) } }
end
define_attr_writers(writers, bool_writers) click to toggle source

Define the attribute writers for the model, given the normal accessor names and the boolean accessor names

# File lib/mumukit/platform/model.rb, line 52
def self.define_attr_writers(writers, bool_writers)
  attr_writer(*writers)
  bool_writers.each { |it| define_method("#{it}=") { |value| instance_variable_set("@#{it}", value.to_boolean) } }
end
dump(obj) click to toggle source

Serializes model

# File lib/mumukit/platform/model.rb, line 60
def self.dump(obj)
  obj.to_json
end
load(json) click to toggle source

Deserializes model

# File lib/mumukit/platform/model.rb, line 65
def self.load(json)
  json ? new(JSON.parse(json)) : new
end
model_attr_accessor(*readers) click to toggle source

Accessors

# File lib/mumukit/platform/model.rb, line 17
def self.model_attr_accessor(*readers)
  bools, raws = readers.partition { |it| it.to_s.end_with? '?' }
  raw_bools = bools.map { |it| it.to_s[0..-2].to_sym }
  attributes = raws + raw_bools

  self.readers += readers
  self.attributes += raws + raw_bools

  define_attr_readers attributes, raw_bools
  define_attr_writers raws, raw_bools
end
parse(hash) click to toggle source

Parses model from an event. Only allowed attributes are accepted

# File lib/mumukit/platform/model.rb, line 31
def self.parse(hash)
  hash ? new(hash.slice(*self.attributes)) : new
end

Public Instance Methods

as_json(options = {}) click to toggle source
Calls superclass method
# File lib/mumukit/platform/model.rb, line 35
def as_json(options = {})
  super(options).slice(*self.class.attributes.map(&:to_s))
end
empty?() click to toggle source
# File lib/mumukit/platform/model.rb, line 11
def empty?
  as_json.empty?
end