Class: SakaiInfo::SakaiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/sakai_object.rb

Overview

this class forms the basis of all other Sakai object abstractions

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject (readonly)

most objects will have unique IDs (perhaps the rest should generate their own?)



17
18
19
# File 'lib/sakai-info/sakai_object.rb', line 17

def id
  @id
end

Class Method Details

.all_serializationsObject

support for CLI – returns an array of symbols that can be passed back to #serialize, #to_yaml, or #to_json should be reimplemented in all object classes



94
95
96
# File 'lib/sakai-info/sakai_object.rb', line 94

def self.all_serializations
  [:default]
end

.descendantsObject

keep track of descendants



99
100
101
# File 'lib/sakai-info/sakai_object.rb', line 99

def self.descendants
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Instance Method Details

#dbrow_only_serializationObject



53
54
55
56
57
58
59
# File 'lib/sakai-info/sakai_object.rb', line 53

def dbrow_only_serialization
  if not self.dbrow_serialization["dbrow"].nil?
    self.dbrow_serialization["dbrow"]
  else
    {}
  end
end

#dbrow_serializationObject



43
44
45
46
47
48
49
50
51
# File 'lib/sakai-info/sakai_object.rb', line 43

def dbrow_serialization
  if self.respond_to? :dbrow
    {
      "dbrow" => self.dbrow
    }
  else
    {}
  end
end

#default_serializationObject



61
62
63
# File 'lib/sakai-info/sakai_object.rb', line 61

def default_serialization
  object_type_serialization
end

#object_type_serializationObject



37
38
39
40
41
# File 'lib/sakai-info/sakai_object.rb', line 37

def object_type_serialization
  {
    "sakai_object_type" => self.class
  }
end

#serialize(*q) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sakai-info/sakai_object.rb', line 19

def serialize(*q)
  q.flatten!

  if q.length == 0
    q = [:default]
  end

  serialization = {}
  q.each do |sub|
    sub_method_name = (sub.to_s + "_serialization").to_sym
    if self.respond_to? sub_method_name
      serialization = serialization.merge(self.method(sub_method_name).call)
    end
  end

  serialization
end

#shell_serializationObject



69
70
71
# File 'lib/sakai-info/sakai_object.rb', line 69

def shell_serialization
  summary_serialization
end

#summary_serializationObject



65
66
67
# File 'lib/sakai-info/sakai_object.rb', line 65

def summary_serialization
  default_serialization
end

#to_csv(*fields) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/sakai-info/sakai_object.rb', line 81

def to_csv(*fields)
  values = []
  fields.each do |field|
    m = self.method(field.to_sym)
    next if m.nil?
    values << m.call.to_s
  end
  values.collect{|v|"\"#{v}\""}.join(",")
end

#to_json(*q) ⇒ Object



77
78
79
# File 'lib/sakai-info/sakai_object.rb', line 77

def to_json(*q)
  serialize(q).to_json
end

#to_yaml(*q) ⇒ Object



73
74
75
# File 'lib/sakai-info/sakai_object.rb', line 73

def to_yaml(*q)
  serialize(q).to_yaml
end