Class: RestfulObjects::ActionDescription

Inherits:
Object
  • Object
show all
Includes:
LinkGenerator
Defined in:
lib/restful_objects/action_description.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LinkGenerator

#generate_rel, #generate_repr_type, #link_to, #underscore_to_hyphen_string

Constructor Details

#initialize(id, domain_type, options) ⇒ ActionDescription

Returns a new instance of ActionDescription.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/restful_objects/action_description.rb', line 8

def initialize(id, domain_type, options)
  @id              = id
  @domain_type     = domain_type
  @friendly_name   = options[:friendly_name]   || id
  @description     = options[:description]     || id
  @member_order    = options[:member_order]    || 0
  @disabled_reason = options[:disabled_reason] || ''

  case options[:return_type]
    when NilClass
      @result_type      = :void
      @kind_result_type = :void
    when Symbol
      if options[:return_type] == :void
        @result_type      = :void
        @kind_result_type = :void
      else
        raise "result type for scalar '#{options[:return_type]}' unssuported" unless [:string, :int, :bool, :decimal, :date, :blob].include?(options[:return_type])
        @result_type      = options[:return_type]
        @kind_result_type = :scalar
      end
    when Hash
      options[:return_type]
      if options[:return_type][:object]
        @result_type      = options[:return_type][:object]
        @kind_result_type = :object
      elsif options[:return_type][:proto_object]
        @result_type      = options[:return_type][:proto_object]
        @kind_result_type = :proto_object
      elsif options[:return_type][:list]
        @result_type      = options[:return_type][:list]
        @kind_result_type = :list
      else
        raise 'invalid return_type: object, proto_object or list key expected'
      end
      unless @result_type.is_a?(Class) or @result_type.is_a?(String)
        raise 'return_type object, proto_object or list value should be a class or a string' 
      end
    else
      raise 'invalid return_type: symbol or hash expected'
  end

  @parameters = ParameterDescriptionList.new
  options[:parameters].each { |name, definition| @parameters.add(name, definition) } if options[:parameters]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/restful_objects/action_description.rb', line 6

def description
  @description
end

#disabled_reasonObject

Returns the value of attribute disabled_reason.



6
7
8
# File 'lib/restful_objects/action_description.rb', line 6

def disabled_reason
  @disabled_reason
end

#friendly_nameObject

Returns the value of attribute friendly_name.



6
7
8
# File 'lib/restful_objects/action_description.rb', line 6

def friendly_name
  @friendly_name
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/restful_objects/action_description.rb', line 5

def id
  @id
end

#kind_result_typeObject (readonly)

Returns the value of attribute kind_result_type.



5
6
7
# File 'lib/restful_objects/action_description.rb', line 5

def kind_result_type
  @kind_result_type
end

#member_orderObject

Returns the value of attribute member_order.



6
7
8
# File 'lib/restful_objects/action_description.rb', line 6

def member_order
  @member_order
end

#parametersObject (readonly)

Returns the value of attribute parameters.



5
6
7
# File 'lib/restful_objects/action_description.rb', line 5

def parameters
  @parameters
end

#result_typeObject (readonly)

Returns the value of attribute result_type.



5
6
7
# File 'lib/restful_objects/action_description.rb', line 5

def result_type
  @result_type
end

Instance Method Details

#get_representationObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/restful_objects/action_description.rb', line 54

def get_representation
  representation = {
    'id' => @id,
    'hasParams' => has_params,
    'memberOrder' => @member_order,
    'parameters' => parameters_list,
    'links' => [
      link_to(:self, "/domain-types/#{@domain_type}/actions/#{@id}", :action_description),
      link_to(:up, "/domain-types/#{@domain_type}", :domain_type),
      link_to(:return_type, "/domain-types/#{result_type}", :domain_type)
    ],
    'extensions' => {}
  }

  representation['friendlyName'] = friendly_name if friendly_name
  representation['description'] = description if description

  representation.to_json
end

#has_paramsObject



82
83
84
# File 'lib/restful_objects/action_description.rb', line 82

def has_params
  not @parameters.empty?
end

#metadataObject



74
75
76
77
78
79
80
# File 'lib/restful_objects/action_description.rb', line 74

def 
  result = { 'friendlyName' => friendly_name,
             'description' => description,
             'returnType' => result_type,
             'hasParams' => has_params,
             'memberOrder' => member_order }
end

#parameters_listObject



86
87
88
89
90
91
92
93
94
# File 'lib/restful_objects/action_description.rb', line 86

def parameters_list
  result = {}
  parameters.each do |name, parameter|
    result[name] = {
      'extension' => parameter.
    }
  end
  result
end