Class: ExtDirect::Api

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

Overview

Author:

  • Mehmet Celik

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#exposed_apiObject (readonly)

Returns the value of attribute exposed_api.



7
8
9
# File 'lib/ext_direct/api.rb', line 7

def exposed_api
  @exposed_api
end

Class Method Details

.expose(class_to_expose, options = {}) ⇒ Hash

Expose a class

Parameters:

  • class (Class)

    that needs to be exposed

  • instructions (Hash)

    to how to expose the class. For now just the ‘:only’ key is accepted.

Returns:

  • (Hash)

    returns a list of exposed classes

Author:

  • Mehmet Celik



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ext_direct/api.rb', line 15

def self.expose(class_to_expose, options = {})
  @exposed_api = {} if @exposed_api.nil?
  
  methods = []
  
  if options.include?(:only)
    raw_methods = options[:only] || []
  else
    raw_methods = class_to_expose.instance_methods(false) - (options[:except] || [])
  end
  
  raw_methods.uniq!
  raw_methods.each do |m|
    name = m
    len = class_to_expose.instance_method(m).parameters.size
    methods << {:name => name, :len => len}
  end
        
  @exposed_api.store(class_to_expose.name, methods)
end

.expose_all(class_dir) ⇒ Object

Expose all classes in a directory(conviniance method)

Parameters:

  • Directory (String)

    where all classes are stored

Author:

  • Mehmet Celik



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ext_direct/api.rb', line 40

def self.expose_all(class_dir)
  @exposed_api = {}
 
  Dir.glob("#{class_dir}/**/*.rb").each do |r|
    rr = r.split("#{class_dir}/")[1].gsub('.rb','')     
    puts "#{class_dir}/#{rr}"
    require "#{class_dir}/#{rr}"
 
    klass = self.class.const_get(rr.split('_').map{|c| c.capitalize}.join(''))
    self.expose klass
  end      
end

.to_jsonString

Return all exposed classes as a JSON String

Returns:

  • (String)

    exposed classes as JSON String

Author:

  • Mehmet Celik



57
58
59
60
# File 'lib/ext_direct/api.rb', line 57

def self.to_json      
  api = self.to_raw
  "REMOTING_API = #{api.to_json};"
end

.to_rawHash

Return a Hash of all exposed classes

Returns:

  • (Hash)

    exposed classes

Author:

  • Mehmet Celik



66
67
68
69
70
# File 'lib/ext_direct/api.rb', line 66

def self.to_raw
  api = {:url => '/router', 
         :type => 'remoting',
         :actions => @exposed_api}      
end