Module: Sequencescape::Api::Actions

Instance Method Summary collapse

Instance Method Details

#has_create_action(name = :create!, options = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sequencescape-api/actions.rb', line 2

def has_create_action(name = :create!, options = {})
  action = options[:action] || :create

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

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

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



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sequencescape-api/actions.rb', line 18

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

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