Module: ActionService::Exporting::ClassMethods

Defined in:
lib/action_service/exporting.rb

Instance Method Summary collapse

Instance Method Details

#add_export_definition_callback(&block) ⇒ Object

:nodoc:



111
112
113
# File 'lib/action_service/exporting.rb', line 111

def add_export_definition_callback(&block) # :nodoc:
  write_inheritable_array("export_definition_callbacks", [block])
end

#export(name, options = {}) ⇒ Object

Declares the named method to be available for remote execution.

Example:

class CalculatorService < ActionService::Base
  def add(a, b)
    a + b
  end

  export :add, :expects => [Integer, Integer], :returns => [Integer]
end

You will need to provide type annotation options if the method will be receiving arguments, or returning values that will be used by remote callers.

A type annotation is an Array, containing as elements one or more of:

  • A Class object for the desired type.

  • An Array containing a Class object. Indicates that the argument at that position is to be an Array containing values of type Class.

  • A Hash containing the argument name as the key, and one of the above as value.

Valid annotations:

:expects

The arguments that will be received by the method

:returns

The type of the method return value. May contain only one element.

:expects_and_returns

Shortcut for specifying :expects and :returns with the same signature.

The public name of the method (that is, the name that must be used by remote callers) will be a camelized version of name. Camelization is performed using Rails inflector camelization rules.

Method name camelization can be turned off for a class by using the export_name_mangling class option:

class MyService < ActionService::Base
  export_name_mangling false
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/action_service/exporting.rb', line 55

def export(name, options={})
  validate_options([:expects, :returns, :expects_and_returns], options.keys)
  if options[:expects_and_returns]
    expects = options[:expects_and_returns]
    returns = options[:expects_and_returns]
  else
    expects = options[:expects]
    returns = options[:returns]
  end
  if expects
    expects.each do |klass| 
      klass = klass.values.shift if klass.is_a?(Hash)
      klass = klass[0] if klass.is_a?(Array)
      if klass.ancestors.include?(ActiveRecord::Base)
        raise(ActionServiceError, "ActiveRecord model classes not allowed in :expects")
      end
    end
  end
  name = name.to_sym
  public_name = public_export_name(name)
  info = { :expects => expects, :returns => returns }
  write_inheritable_hash("action_service_exports", name => info)
  write_inheritable_hash("action_service_public_exports", public_name => name)
  call_export_definition_callbacks(self, name, info)
end

#exportsObject

A Hash of method name to export information



107
108
109
# File 'lib/action_service/exporting.rb', line 107

def exports
  read_inheritable_attribute("action_service_exports") || {}
end

#has_export?(name) ⇒ Boolean

Whether the given name is exported by this service

Returns:

  • (Boolean)


82
83
84
# File 'lib/action_service/exporting.rb', line 82

def has_export?(name)
  exports.has_key?(name)
end

#has_public_export?(name) ⇒ Boolean

Whether the given public method name (mangled) is exported by this service

Returns:

  • (Boolean)


88
89
90
# File 'lib/action_service/exporting.rb', line 88

def has_public_export?(name)
  public_exports.has_key?(name)
end

#internal_export_name(public_name) ⇒ Object

The internal method name for the given public method name



102
103
104
# File 'lib/action_service/exporting.rb', line 102

def internal_export_name(public_name)
  public_exports[public_name]
end

#public_export_name(export_name) ⇒ Object

The mangled public method name for the given method name



93
94
95
96
97
98
99
# File 'lib/action_service/exporting.rb', line 93

def public_export_name(export_name)
  if export_name_mangling
    Inflector.camelize(export_name.to_s)
  else
    export_name.to_s
  end
end