class Materialist::Materializer::Internals::Materializer
Attributes
api_client[R]
instance[R]
options[R]
url[R]
Public Class Methods
new(url, klass, resource_payload: nil, api_client: nil)
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 8 def initialize(url, klass, resource_payload: nil, api_client: nil) @url = url @instance = klass.new @options = klass.__materialist_options @api_client = api_client || Materialist.configuration.api_client if resource_payload @resource = PayloadResource.new(resource_payload, client: @api_client) end end
Public Instance Methods
perform(action)
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 18 def perform(action) action.to_sym == :delete ? destroy : upsert end
Private Instance Methods
after_destroy()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 101 def after_destroy options[:after_destroy] end
after_upsert()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 93 def after_upsert options[:after_upsert] end
attributes()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 121 def attributes mappings.map{ |m| m.map(resource) }.compact.reduce(&:merge) || {} end
before_destroy()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 97 def before_destroy options[:before_destroy] end
before_upsert()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 85 def before_upsert options[:before_upsert] end
before_upsert_with_payload()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 89 def before_upsert_with_payload options[:before_upsert_with_payload] end
destroy()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 45 def destroy return unless materialize_self? model_class.find_by(source_lookup(url)).tap do |entity| send_messages(before_destroy, entity) unless before_destroy.nil? entity.destroy!.tap do |entity| send_messages(after_destroy, entity) unless after_destroy.nil? end if entity end end
fetch_resource()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 129 def fetch_resource api_client.get(url, options: { enable_caching: false, response_class: HateoasResource }) rescue Routemaster::Errors::ResourceNotFound nil end
mappings()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 81 def mappings options.fetch :mapping end
materialize_link(key, opts)
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 74 def materialize_link(key, opts) return unless link = resource.dig(:_links, key) return unless materializer_class = MaterializerFactory.class_from_topic(opts.fetch(:topic)) materializer_class.perform(link[:href], :noop) end
materialize_links()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 69 def materialize_links (options[:links_to_materialize] || []) .each { |key, opts| materialize_link(key, opts) } end
materialize_self?()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 57 def materialize_self? options.include? :model_class end
model_class()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 105 def model_class options.fetch(:model_class).to_s.camelize.constantize end
resource()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 125 def resource @resource ||= fetch_resource end
send_messages(messages, arguments)
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 135 def send_messages(messages, arguments) messages.each { |message| instance.send(message, arguments) } end
source_key()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 109 def source_key options.fetch(:source_key, :source_url) end
source_key_parser()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 113 def source_key_parser options[:source_key_parser] || ->(url, data) { url } end
source_lookup(url, resource={})
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 117 def source_lookup(url, resource={}) @_source_lookup ||= { source_key => source_key_parser.call(url, resource) } end
upsert(retry_on_race_condition: true)
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 24 def upsert(retry_on_race_condition: true) return unless resource if materialize_self? upsert_record.tap do |entity| send_messages(after_upsert, entity) unless after_upsert.nil? end end materialize_links rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid # when there is a race condition and uniqueness of :source_url # is enforced by database index, this error is raised # so we simply try upsert again # if error is due to another type of uniqueness constraint # second call will also fail and error would bubble up retry_on_race_condition ? upsert(retry_on_race_condition: false) : raise end
upsert_record()
click to toggle source
# File lib/materialist/materializer/internals/materializer.rb, line 61 def upsert_record model_class.find_or_initialize_by(source_lookup(url, resource)).tap do |entity| send_messages(before_upsert, entity) unless before_upsert.nil? before_upsert_with_payload&.each { |m| instance.send(m, entity, resource) } entity.update_attributes!(attributes) end end