Class: ModelRdf

Inherits:
Object
  • Object
show all
Defined in:
lib/data_models/model_rdf.rb

Constant Summary collapse

@@privacy =

Attributes declarations

["Hidden","Public","Authenticated"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

Read the document of attributes relations with rdf properties



19
20
21
# File 'lib/data_models/model_rdf.rb', line 19

def initialize
  @model_rdf = YAML::load(File.open("#{Rails.root}/config/easy_data/rdf_info.yaml"))
end

Instance Attribute Details

#model_rdfObject

Returns the value of attribute model_rdf.



10
11
12
# File 'lib/data_models/model_rdf.rb', line 10

def model_rdf
  @model_rdf
end

Instance Method Details

#add_model(model, attributes) ⇒ Object



120
121
122
# File 'lib/data_models/model_rdf.rb', line 120

def add_model(model,attributes)
    self.model_rdf[model] = attributes
end

#attribute(model, attribute) ⇒ String

RDFa: Return attribute rdf info

Parameters:

  • model's (String)

    name

  • model's (Strign)

    attribute

Returns:

  • (String)

    RDF information about model’s attribute to insert in HTML tag.



150
151
152
153
# File 'lib/data_models/model_rdf.rb', line 150

def attribute(model,attribute)
   att_rdf = self.model_rdf[model]['attributes'][attribute]
   " property='#{att_rdf[:namespace]}:#{att_rdf[:property]}'"
end

#delete_model(model) ⇒ Object



124
125
126
# File 'lib/data_models/model_rdf.rb', line 124

def delete_model(model)
    self.model_rdf.delete(model)
end

#get_associations_model(model) ⇒ Array

Return all models which they are assocciations of current model

Parameters:

  • current (String)

    model name

Returns:

  • (Array)

    list of models which the are assocciations with current model.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/data_models/model_rdf.rb', line 90

def get_associations_model(model)
   associations = self.model_rdf[model]['associations'].keys
   models = DataModels.load_models
   hash_associations = {}

   associations.each do |assc|
     if models.include?assc.camelize
       hash_associations[assc] = assc 
     elsif eval(model+'.reflections[:'+assc+'].class_name')
       hash_associations[assc] = eval(model+'.reflections[:'+assc+'].class_name')
     else
       hash_associations[assc] = eval(model+'.reflections[:'+assc+'].options[:class_name]')
     end
   end

   hash_associations
end

#get_associations_tag(element) ⇒ Hash

Return all association’s information of a model’s instance

Parameters:

  • Model's (Object)

    instance

Returns:

  • (Hash)

    RDF’s association informations about the object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/data_models/model_rdf.rb', line 292

def get_associations_tag(element)
   
   associations = get_attributes_model(element.class.to_s)
   class_element = element.class
   if associations.nil?
     associations = get_attributes_model(element.class.base_class.to_s)
     class_element = element.class.base_class
   end
   properties = {}       
  
   class_element.reflections.each do |ref,value|
     
     rel = Array(eval "element.#{ref}")||Array()
     
     if exist_info_assoc(rel,associations["associations"][ref.to_s]) && can_see?(associations["associations"][ref.to_s][:privacy]) && !rel.empty?
       
       properties.merge!({"#{associations['associations'][ref.to_s][:namespace]}:#{associations['associations'][ref.to_s][:property]}" => {:model => rel.first.class ,:id => rel.collect{|obj| obj.id}}
      })
     end
   end     
    
   properties
end

#get_attributes_model(model) ⇒ Hash

Return attributes of model stored in the configuration yaml file

Parameters:

  • model's (String)

    name

Returns:

  • (Hash)

    Return all information about the model and his data.



83
84
85
# File 'lib/data_models/model_rdf.rb', line 83

def get_attributes_model(model)
   self.model_rdf[model] 
end

#get_header(attributes) ⇒ Array

Get all namespace used to build the response

Parameters:

  • list (Array)

    of attributes

Returns:

  • (Array)

    list of namespaces used to describes them.



246
247
248
249
250
251
252
253
254
255
256
# File 'lib/data_models/model_rdf.rb', line 246

def get_header(attributes)
   headers = {}
   
   (attributes["attributes"].merge(attributes["associations"])).each do |att,properties|
     if properties != "no publication" && properties[:namespace] != 'not defined' && properties[:privacy]=="Public"
        headers["xmlns:#{properties[:namespace]}"] = (eval "EasyData::RDF::#{properties[:namespace].upcase}.get_uri") #EasyData.get_uri_namespace(properties[:namespace])
     end
   end    
  
   headers
end

#get_model_rdf(query, model, host) ⇒ Hash

Build a response to user’s request

Parameters:

  • Response's (Array)

    data

  • model's (String)

    name

  • current (String)

    host

Returns:

  • (Hash)

    Response to be render in rdf file.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/data_models/model_rdf.rb', line 217

def get_model_rdf(query,model,host)
   
   if public?model
    request = {:body => "",:header => {"xmlns:rdf"=>"http://www.w3.org/1999/02/22-rdf-syntax-ns#"}}
    elements = {}
    models = []
    query.each do |element|
       elements[element.id] = {'description' => "#{host}/s/#{element.class.to_s}/#{element.id}",
                               'attributes' => get_properties_tag(element),
                               'associations' => get_associations_tag(element)
                              }
    
       models << element.class.to_s
    end     
 
    attributes = {}
    request[:body] = elements
    models.each do |mod|
      attributes = get_attributes_model(mod) || get_attributes_model((eval mod).base_class.to_s)
      request[:header].merge!(get_header(attributes))
    end
    request
   else
    {}
   end
end

#get_modelsHash

Return datas stored in the RDF informations yaml file

Returns:

  • (Hash)

    return RDF informations



25
26
27
# File 'lib/data_models/model_rdf.rb', line 25

def get_models
   self.model_rdf
end

#get_not_hidden_modelsHash

Return data stored in the RDF informations yaml file about publicated models.

Returns:

  • (Hash)

    return RDF information about not hidden rdf information’s models.



42
43
44
45
46
47
48
49
50
# File 'lib/data_models/model_rdf.rb', line 42

def get_not_hidden_models
  models = []
  self.load_models.each do |mod|
    unless self.hidden?(mod) 
      models << mod
    end
  end
  models
end

#get_prefix(model) ⇒ String

RDFa: return a string with all prefix used to describes attributes

Parameters:

  • model's (String)

    name

Returns:

  • (String)

    list of prefix to be used in RDFa information



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/data_models/model_rdf.rb', line 168

def get_prefix(model)
   prefix = []
   data_model = get_attributes_model(model)
   data_model["attributes"].each do |att,info|
    if info[:namespace] && info[:namespace] != 'not defined'
     puts info[:namespace]
     prefix << "xmls:#{info[:namespace]}=#{(eval "EasyData::RDF::#{info[:namespace].upcase}.get_uri")} "
    end
   end
   data_model["associations"].each do |assoc,info|
    if info[:namespace] && info[:namespace] != 'not defined'
     prefix << "xmls:#{info[:namespace]}=#{(eval "EasyData::RDF::#{info[:namespace].upcase}.get_uri")} "
    end
   end

   if data_model[:namespace] && data_model[:namespace] != 'not defined'
     prefix << "xmls:#{data_model[:namespace]}=#{(eval "EasyData::RDF::#{data_model[:namespace].upcase}.get_uri")}"
   end

   prefix.uniq.join(" ")
end

#get_properties_tag(element) ⇒ Hash

Return all attribute’s information of a model’s instance

Parameters:

  • Model's (Object)

    instance

Returns:

  • (Hash)

    RDF’s attributes informations about the object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/data_models/model_rdf.rb', line 261

def get_properties_tag(element)

  attributes = get_attributes_model(element.class.to_s)

  # If element's class is a polimorphic class, we used base class.
  if attributes.nil?
    attributes = get_attributes_model(element.class.base_class.to_s)
  end 

  properties = {}
  if element.attributes.respond_to? :each 
   element.attributes.each do |att|
     #conditions to methods to check if can be show
    begin        
     
     if can_see?(attributes["attributes"][att.first][:privacy]) && exist_info_att(attributes["attributes"][att.first],att.second)
       properties["#{attributes['attributes'][att.first][:namespace]}:#{attributes["attributes"][att.first][:property]}"] = att.second
     end
     rescue
       puts attributes["attributes"]         
     end
   end  
  end

  properties
   
end

#get_public_modelsHash

Return data stored in the RDF informations yaml file about publicated models

Returns:

  • (Hash)

    return RDF information about public access information of publicated models



31
32
33
34
35
36
37
38
# File 'lib/data_models/model_rdf.rb', line 31

def get_public_models
   models = []
   self.load_models.each do |mod|
     if self.public?(mod) 
       models << mod
     end
   end
end

#get_rdf_info_model(model) ⇒ Hash

Model RDF informations and privacy

Parameters:

  • model's (String)

    name

Returns:

  • (Hash)

    hash with RDF and privacy information about the model



76
77
78
# File 'lib/data_models/model_rdf.rb', line 76

def get_rdf_info_model(model)
   {:namespace => self.model_rdf[model][:namespace],:property => self.model_rdf[model][:property]}
end

#hidden?(model) ⇒ Boolean

Check if the model is hidden

Parameters:

  • model's (Symbol)

    name

Returns:

  • (Boolean)

    true or false



69
70
71
# File 'lib/data_models/model_rdf.rb', line 69

def hidden?(model)
   self.model_rdf[model][:privacy] == "Hidden"
end

#model(model) ⇒ String

RDFa: Return attributes of model RDF info

Parameters:

  • model's (String)

    name

Returns:

  • (String)

    model’s rdf information to insert in HTML tag



111
112
113
114
115
116
117
118
# File 'lib/data_models/model_rdf.rb', line 111

def model(model)
  info = get_rdf_info_model(model)
  if info[:property] && info[:property]!= "not defined"
   " typeof='#{info[:namespace]}:#{info[:property]}'"
  else
   " "
  end
end

#privacy(index) ⇒ String

Convert index privacy to string

Parameters:

  • Index (Integer)

    of privacy

Returns:

  • (String)

    Privacy label



193
194
195
# File 'lib/data_models/model_rdf.rb', line 193

def privacy(index)
  @@privacy[index]
end

#private?(model) ⇒ Boolean

Check if the model is private

Parameters:

  • model's (Symbol)

    name

Returns:

  • (Boolean)

    true or false



62
63
64
# File 'lib/data_models/model_rdf.rb', line 62

def private?(model)
   self.model_rdf[model][:privacy] == "Private"
end

#public?(model) ⇒ Boolean

Check if the model is public

Parameters:

  • model's (Symbol)

    name

Returns:

  • (Boolean)

    true or false



55
56
57
# File 'lib/data_models/model_rdf.rb', line 55

def public?(model)
   self.model_rdf[model][:privacy] == "Public"
end

#refresh_informationObject



206
207
# File 'lib/data_models/model_rdf.rb', line 206

def refresh_information
end

#saveObject

Save changes in rdf_info.yaml (configuration’s yaml file) return [Boolean] true or false if operations has finished correctly



199
200
201
202
203
# File 'lib/data_models/model_rdf.rb', line 199

def save
  file = File.open("#{Rails.root}/config/easy_data/rdf_info.yaml",'w')
  file.puts YAML::dump(self.model_rdf)
  file.close
end

#update_associations_model(model, association, param, value) ⇒ Boolean

update associations with rdf properties.

Parameters:

  • model's (String)

    name

  • model's (String)

    attribute

  • association's (String)

    property to be updated

  • new (Strign)

    value

Returns:

  • (Boolean)

    true or false if the operation has finished correctly.



161
162
163
# File 'lib/data_models/model_rdf.rb', line 161

def update_associations_model(model,association,param,value)
   self.model_rdf[model]['associations'][association][param.to_sym] = value
end

#update_attributes_model(model, attribute, param, value) ⇒ Boolean

update attributes with rdf properties.

Parameters:

  • model's (String)

    name

  • model's (String)

    attribute

  • attribute's (String)

    property to be updated

  • new (Strign)

    value

Returns:

  • (Boolean)

    true or false if the operation has finished correctly.



142
143
144
# File 'lib/data_models/model_rdf.rb', line 142

def update_attributes_model(model,attribute,param,value)
   self.model_rdf[model]['attributes'][attribute][param.to_sym] = value
end

#update_model(model, param, value) ⇒ Boolean

Update model rdf info

Parameters:

  • model's (String)

    name

  • model's (String)

    property to be updated

  • new (Strign)

    value

Returns:

  • (Boolean)

    true or false if the operation has finished correctly.



132
133
134
# File 'lib/data_models/model_rdf.rb', line 132

def update_model(model,param,value)
   self.model_rdf[model][param.to_sym] = value
end