Class: DataSource

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

Overview

<summary>

  This helper classes process the request after recieve from the DSRequest.
  The CRUD methods(add, remove, update, fetch) were supported.      
</summary>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, model) ⇒ DataSource

Returns a new instance of DataSource.



11
12
13
14
# File 'lib/DataSource.rb', line 11

def initialize(path, model)        
  #@data_source = self.get_data(path)
  @model = model
end

Instance Attribute Details

#data_sourceObject

Returns the value of attribute data_source.



8
9
10
# File 'lib/DataSource.rb', line 8

def data_source
  @data_source
end

Instance Method Details

#execute(request) ⇒ Object

<summary> process the request </summary>



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/DataSource.rb', line 43

def execute(request)
  operation_type = request.operationType      
    case operation_type
     when 'fetch' 
       @result = fetch(request)
     when 'add'         
       @result = add(request)
     when 'remove'
       @result = remove(request)
     when 'update'
       @result = update(request)
    end
  return @result
end

#get_data(path) ⇒ Object

<summary> get the DataSource contents from the path and parse to JSON format </summary>



18
19
20
21
22
23
24
25
26
# File 'lib/DataSource.rb', line 18

def get_data(path)
  ds_content = File.read(path)
  #remove the isc tag and the end tag
  ds_content['isc.RestDataSource.create('] = ''
  ds_content[');'] = ''
  #remove tab, newline tag \n \r \t etc
  result = ds_content.gsub('/\r|\n|\t|>>|\/\//', '')
  return JSON.parse(result)
end

#get_field(field_name) ⇒ Object

<summary> get the field content by the filed name </summary>



30
31
32
33
34
35
36
37
38
39
# File 'lib/DataSource.rb', line 30

def get_field(field_name)
  fields = @data_source['fields']
  
  fields.each do | f |
    if f['name'] == filed_name
      return f
    end
  end     
  return nil
end