Module: Sequencescape::Api::Actions::InstanceActionHelpers

Defined in:
lib/sequencescape-api/actions.rb

Instance Method Summary collapse

Instance Method Details

#has_create_action(*args) ⇒ Object

rubocop:todo Metrics/MethodLength



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequencescape-api/actions.rb', line 35

def has_create_action(*args) # rubocop:todo Metrics/MethodLength
  options      = args.extract_options!
  name         = args.first || :create!

  action       = options[:action] || :create
  result_class = 'self'
  result_class = "api.#{options[:resource]}" if options[:resource]

  line = __LINE__ + 1
  class_eval(%{
    def #{name}(attributes = nil)
      url = actions.try(#{action.to_sym.inspect}) or
        raise Sequencescape::Api::Error, "Cannot perform #{action} without an URL"

      #{result_class}.new(attributes || {}, false).tap do |object|
        object.save!(:url => url)
      end
    end
  }, __FILE__, line)
end

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sequencescape-api/actions.rb', line 56

def has_update_action(name, options = {})
  api_method = options[:verb] == :create ? :create : :update
  skip_json  = options[:skip_json] || false
  action     = options[:action] || :update

  line = __LINE__ + 1
  class_eval(%{
    def #{name}(body = nil)
      update_from_json(body || {}, false)
      modify!(action: #{action.to_sym.inspect}, http_verb: #{api_method.to_sym.inspect}, skip_json: #{skip_json})
    end
  }, __FILE__, line)
end