Module: Formstack::Model::ClassMethods

Defined in:
lib/formstack/model.rb

Instance Method Summary collapse

Instance Method Details

#all(*args) ⇒ Object



43
44
45
# File 'lib/formstack/model.rb', line 43

def all(*args)
  new_from_response client.public_send("#{client_method}s", *args), context: args
end

#clientObject



68
69
70
# File 'lib/formstack/model.rb', line 68

def client
  Formstack.client
end

#client_methodObject

Raises:

  • (NotImplementedError)


72
73
74
# File 'lib/formstack/model.rb', line 72

def client_method
  raise NotImplementedError
end

#create(*args) ⇒ Object



51
52
53
54
55
# File 'lib/formstack/model.rb', line 51

def create(*args)
  new_from_response(client.public_send("create_#{client_method}", *args)).tap do |obj|
    obj.load if obj[:id]
  end
end

#find(*args) ⇒ Object



47
48
49
# File 'lib/formstack/model.rb', line 47

def find(*args)
  new_from_response client.public_send(client_method, *args)
end

#new_from_response(response, context: []) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/formstack/model.rb', line 57

def new_from_response(response, context: [])
  if response.respond_to?(:has_key?) && response.has_key?(response_nesting)
    response = response[response_nesting]
  end
  if response.is_a?(Array)
    Collection.new(response.map { |item| new item }, klass: self, context: context)
  else
    new response
  end
end

#response_nestingObject



76
77
78
# File 'lib/formstack/model.rb', line 76

def response_nesting
  ""
end