Class: KnowledgeClass

Inherits:
Object
  • Object
show all
Defined in:
lib/cirrocumulus/facts.rb

Defined Under Namespace

Classes: KnowledgeClassDescription

Constant Summary collapse

@@classes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ KnowledgeClass

Returns a new instance of KnowledgeClass.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cirrocumulus/facts.rb', line 105

def initialize(options = {})
  @values = {}

  description = @@classes[self.class.name]
  if description.primary_key && options.has_key?(description.primary_key.to_sym)
    @values[description.primary_key] = options[description.primary_key.to_sym]
  end

  description.properties.each do |prop|
    next if prop == description.primary_key
    next unless options.has_key?(prop.to_sym)
    @values[prop] = options[prop.to_sym]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cirrocumulus/facts.rb', line 162

def method_missing(meth, *args, &block)
  description = @@classes[self.class.name]
  description.properties.each do |prop|
    if meth.to_s == "#{prop}="
      return values[prop] = args[0]
    elsif meth.to_s == prop
      return values[prop]
    end
  end

  super
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



103
104
105
# File 'lib/cirrocumulus/facts.rb', line 103

def values
  @values
end

Class Method Details

.from_fact(fact) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cirrocumulus/facts.rb', line 88

def from_fact(fact)
  description = @@classes[self.name]
  return nil if fact[0] != description.name.to_sym

  instance = self.new
  instance.values[description.primary_key] = fact[1]
  description.properties.each_with_index do |k, idx|
    next if idx == 0
    instance.values[k] = fact[1 + idx*2]
  end

  instance
end

.id(id_name) ⇒ Object



63
64
65
66
67
# File 'lib/cirrocumulus/facts.rb', line 63

def id(id_name)
  @@classes[self.name] = KnowledgeClassDescription.new(self) if @@classes[self.name].nil?
  @@classes[self.name].primary_key = id_name.to_s
  @@classes[self.name].properties << id_name.to_s
end

.klass(class_name) ⇒ Object



58
59
60
61
# File 'lib/cirrocumulus/facts.rb', line 58

def klass(class_name)
  @@classes[self.name] = KnowledgeClassDescription.new(self) if @@classes[self.name].nil?
  @@classes[self.name].name = class_name.to_s
end

.property(property_name) ⇒ Object



69
70
71
72
# File 'lib/cirrocumulus/facts.rb', line 69

def property(property_name)
  @@classes[self.name] = KnowledgeClassDescription.new(self) if @@classes[self.name].nil?
  @@classes[self.name].properties << property_name.to_s
end

.to_templateObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cirrocumulus/facts.rb', line 74

def to_template
  description = @@classes[self.name]
  fact = [description.name.to_sym]
  fact << description.primary_key.upcase.to_sym if description.primary_key
  description.properties.each do |k|
    next if k == description.primary_key && description.primary_key

    fact << k.to_sym
    fact << k.upcase.to_sym
  end

  fact
end

Instance Method Details

#to_factObject



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/cirrocumulus/facts.rb', line 148

def to_fact
  description = @@classes[self.class.name]
  fact = [description.name.to_sym]
  fact << values[description.primary_key]
  description.properties.each do |k|
    next if k == description.primary_key

    fact << k.to_sym
    fact << values[k]
  end

  fact
end

#to_paramsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cirrocumulus/facts.rb', line 134

def to_params
  description = @@classes[self.class.name]
  fact = [description.name.to_sym]
  fact << (values.has_key?(description.primary_key) ? values[description.primary_key] : description.primary_key.upcase.to_sym) if description.primary_key
  description.properties.each do |k|
    next if k == description.primary_key && description.primary_key

    fact << k.to_sym
    fact << (values.has_key?(k) ? values[k] : k.upcase.to_sym)
  end

  fact
end

#to_templateObject



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cirrocumulus/facts.rb', line 120

def to_template
  description = @@classes[self.class.name]
  fact = [description.name.to_sym]
  fact << values[description.primary_key] if description.primary_key
  description.properties.each do |k|
    next if k == description.primary_key

    fact << k.to_sym
    fact << values[k]
  end

  fact
end