lib/haveapi/client/resource_instance.rb in haveapi-client-0.20.0 vs lib/haveapi/client/resource_instance.rb in haveapi-client-0.21.0
- old
+ new
@@ -22,32 +22,32 @@
if response
if response.is_a?(Hash)
@params = response
@prepared_args = response[:_meta][:path_params]
- @meta = response[:_meta] unless @meta
+ @meta ||= response[:_meta]
else
@response = response
@params = response.response
@prepared_args = response.meta[:path_params]
- @meta = response.meta unless @meta
+ @meta ||= response.meta
end
setup_from_clone(resource)
define_attributes
end
- unless @persistent
- setup_from_clone(resource)
- define_implicit_attributes
- define_attributes(@action.input_params)
- end
+ return if @persistent
+
+ setup_from_clone(resource)
+ define_implicit_attributes
+ define_attributes(@action.input_params)
end
def new
- raise NoMethodError.new
+ raise NoMethodError
end
# Invoke +create+ action if the object is not persistent,
# +update+ action if it is.
def save
@@ -56,26 +56,24 @@
else
@action.provide_args
@response = Response.new(@action, @action.execute(attributes_for_api(@action)))
- if @response.ok?
- @params = @response.response
- define_attributes
+ return nil unless @response.ok?
- else
- return nil
- end
+ @params = @response.response
+ define_attributes
@persistent = true
self
end
end
# Call #save and raise ActionFailed if it fails.
def save!
- raise ActionFailed.new(@response) if save.nil?
+ raise ActionFailed, @response if save.nil?
+
self
end
# Resolve the object (fetch it from the API) if it is not resolved yet.
def resolve
@@ -102,72 +100,73 @@
def attributes
@params
end
def to_s
- "<#{self.class.to_s}:#{object_id}:#{@resource._name}>"
+ "<#{self.class}:#{object_id}:#{@resource._name}>"
end
protected
+
# Define access/writer methods for object attributes.
def define_attributes(params = nil)
(params || @action.params).each do |name, param|
case param[:type]
- when 'Resource'
- @resource_instances[name] = find_association(param, @params[name])
+ when 'Resource'
+ @resource_instances[name] = find_association(param, @params[name])
- # id reader
- ensure_method(:"#{name}_id") do
- @params[name] && @params[name][ param[:value_id].to_sym ]
- end
+ # id reader
+ ensure_method(:"#{name}_id") do
+ @params[name] && @params[name][ param[:value_id].to_sym ]
+ end
- # id writer
- ensure_method(:"#{name}_id=") do |id|
- @params[name] ||= {}
- @params[name][ param[:value_id].to_sym ] = id
+ # id writer
+ ensure_method(:"#{name}_id=") do |id|
+ @params[name] ||= {}
+ @params[name][ param[:value_id].to_sym ] = id
- @resource_instances[name] = find_association(
- param,
- {
- param[:value_id] => id,
- :_meta => {
- resolved: false,
- # TODO: this will not work for nested resources, as they have
- # multiple IDs
- path_params: [id],
- },
+ @resource_instances[name] = find_association(
+ param,
+ {
+ param[:value_id] => id,
+ :_meta => {
+ resolved: false,
+ # TODO: this will not work for nested resources, as they have
+ # multiple IDs
+ path_params: [id]
}
- )
- end
+ }
+ )
+ end
- # value reader
- ensure_method(name) do
- @resource_instances[name] && @resource_instances[name].resolve
- end
+ # value reader
+ ensure_method(name) do
+ @resource_instances[name] && @resource_instances[name].resolve
+ end
- # value writer
- ensure_method(:"#{name}=") do |obj|
- @params[name] ||= {}
- @params[name][ param[:value_id].to_sym ] = obj.method(param[:value_id]).call
- @params[name][ param[:value_label].to_sym ] = obj.method(param[:value_label]).call
+ # value writer
+ ensure_method(:"#{name}=") do |obj|
+ @params[name] ||= {}
+ @params[name][ param[:value_id].to_sym ] = obj.method(param[:value_id]).call
+ @params[name][ param[:value_label].to_sym ] = obj.method(param[:value_label]).call
- @resource_instances[name] = obj
- end
+ @resource_instances[name] = obj
+ end
- else
- # reader
- ensure_method(name) { @params[name] }
+ else
+ # reader
+ ensure_method(name) { @params[name] }
- # writer
- ensure_method(:"#{name}=") { |new_val| @params[name] = new_val }
+ # writer
+ ensure_method(:"#{name}=") { |new_val| @params[name] = new_val }
end
end
end
# Define method +name+ with +block+ if it isn't defined yet.
- def ensure_method(name, &block)
- define_singleton_method(name, &block) unless respond_to?(name)
+ def ensure_method(name, &)
+ define_singleton_method(name, &) unless respond_to?(name)
end
# Define nil references to resource attributes.
# Used only for not-persistent objects.
def define_implicit_attributes
@@ -183,17 +182,17 @@
ret = {}
return ret if action.input_layout != :object
action.input_params.each do |name, param|
- case param[:type]
- when 'Resource'
- ret[name] = @params[name][ param[:value_id].to_sym ]
+ ret[name] = case param[:type]
+ when 'Resource'
+ @params[name][ param[:value_id].to_sym ]
- else
- ret[name] = @params[name]
- end
+ else
+ @params[name]
+ end
end
ret
end
@@ -207,16 +206,16 @@
tmp = tmp.method(r).call
end
# FIXME: read _meta namespace from description
ResourceInstance.new(
- @client,
- @api,
- tmp,
- action: tmp.actions[:show],
- resolved: res_val[:_meta][:resolved],
- response: res_val[:_meta][:resolved] ? res_val : nil,
- meta: res_val[:_meta]
+ @client,
+ @api,
+ tmp,
+ action: tmp.actions[:show],
+ resolved: res_val[:_meta][:resolved],
+ response: res_val[:_meta][:resolved] ? res_val : nil,
+ meta: res_val[:_meta]
)
end
# Override Resource.default_action_input_params.
def default_action_input_params(action)