Sha256: faf052b99e68327e3dab261e25d853928ced82c20302544078f5585bdaca47f5

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'active_support/concern'

module Journey::Resource::AttributeLoading
  extend ActiveSupport::Concern

  included do
    def load(attributes, remove_root = false, persisted = false)
      # 'data' is a reserved key in ActiveResource,
      # but Journey uses it for the Oplog
      if content = attributes.delete('data')
        attributes['content'] = content
      end

      super(attributes, remove_root, persisted).tap do

        # set the cached value of any updated associations (e.g. parent for parent_id)
        # to nil so that they can be re-fetched
        attributes.keys.map(&:to_s).select{ |key| key =~ /_id$/ }.each do |association_key|
          association = association_key.gsub /_id$/, ''
          ivar_name = "@#{association}"
          remove_instance_variable(ivar_name) if instance_variable_defined?(ivar_name)
        end

        # allow enum_sets to be loaded by key (rather than index)
        # by auto-converting them on initialization
        if enum_sets = self.class.instance_variable_get(:@enum_sets)
          enum_sets.each do |enum_attr|
            send("#{enum_attr}=", send(enum_attr))
          end
        end
      end
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
embark-journey-0.2.1 lib/journey/resource/attribute_loading.rb
embark-journey-0.1.8 lib/journey/resource/attribute_loading.rb