lib/materialist/materializer.rb in materialist-0.1.0 vs lib/materialist/materializer.rb in materialist-1.0.0

- old
+ new

@@ -34,10 +34,19 @@ end attr_reader :key, :mapping end + class LinkHrefMapping + def initialize(key:, as:) + @key = key + @as = as + end + + attr_reader :key, :as + end + module ClassMethods attr_reader :__materialist_options, :__materialist_dsl_mapping_stack def perform(url, action) materializer = Materializer.new(url, self) @@ -53,10 +62,14 @@ def capture(key, as: key) __materialist_dsl_mapping_stack.last << FieldMapping.new(key: key, as: as) end + def capture_link_href(key, as:) + __materialist_dsl_mapping_stack.last << LinkHrefMapping.new(key: key, as: as) + end + def link(key) link_mapping = LinkMapping.new(key: key) __materialist_dsl_mapping_stack.last << link_mapping __materialist_dsl_mapping_stack << link_mapping.mapping yield @@ -65,16 +78,16 @@ def persist_to(klass) __materialist_options[:model_class] = klass end - def after_upsert(method_name) - __materialist_options[:after_upsert] = method_name + def after_upsert(*method_array) + __materialist_options[:after_upsert] = method_array end - def after_destroy(method_name) - __materialist_options[:after_destroy] = method_name + def after_destroy(*method_array) + __materialist_options[:after_destroy] = method_array end end class Materializer @@ -87,11 +100,11 @@ def upsert(retry_on_race_condition: true) return unless root_resource if materialize_self? upsert_record.tap do |entity| - instance.send(after_upsert, entity) if after_upsert + send_messages(after_upsert, entity) unless after_upsert.nil? end end materialize_links rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid @@ -107,11 +120,11 @@ def destroy return unless materialize_self? model_class.find_by(source_url: url).tap do |entity| entity.destroy!.tap do |entity| - instance.send(after_destroy, entity) if after_destroy + send_messages(after_destroy, entity) unless after_destroy.nil? end if entity end end private @@ -175,21 +188,32 @@ return {} unless resource mapping.inject({}) do |result, m| case m when FieldMapping - result.tap { |r| r[m.as] = resource.body[m.key] } + result.tap { |r| r[m.as] = serializable_value(resource.body[m.key]) } + when LinkHrefMapping + result.tap do |r| + if resource.body._links.include?(m.key) + r[m.as] = resource.body._links[m.key].href + end + end when LinkMapping resource.body._links.include?(m.key) ? result.merge(build_attributes(resource_at(resource.send(m.key).url), m.mapping || [])) : result else result end end end + def serializable_value(value) + value_is_complex_object = value.is_a?(Hash) || value.is_a?(Array) + value_is_complex_object ? value.to_json : value + end + def resource_at(url) api_client.get(url, options: { enable_caching: false }) rescue Routemaster::Errors::ResourceNotFound # this is due to a race condition between an upsert event # and a :delete event @@ -199,9 +223,13 @@ def api_client @_api_client ||= Routemaster::APIClient.new( response_class: Routemaster::Responses::HateoasResponse ) + end + + def send_messages(messages, arguments) + messages.each { |message| instance.send(message, arguments) } end end end end end