Sha256: 0a664abe1366b46ff6ab24ac8fe3c10cb1c70d9edf091ae02f6f916f29ba3094

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

# encoding: UTF-8
module MarkMapper
  module Plugins
    module Associations
      class SingleAssociation < Base
        def setup(model)
          @model = model
          model.associations_module.module_eval <<-end_eval
            def #{name}
              proxy = get_proxy(associations[#{name.inspect}])
              proxy.nil? ? nil : proxy
            end

            def #{name}=(value)
              association = associations[#{name.inspect}]
              proxy = get_proxy(association)

              if proxy.nil? || proxy.target != value
                proxy = build_proxy(association)
              end

              proxy.replace(value)
              value
            end

            def #{name}?
              get_proxy(associations[#{name.inspect}]).present?
            end

            def build_#{name}(attrs={}, &block)
              get_proxy(associations[#{name.inspect}]).build(attrs, &block)
            end

            def create_#{name}(attrs={}, &block)
              get_proxy(associations[#{name.inspect}]).create(attrs, &block)
            end

            def create_#{name}!(attrs={}, &block)
              get_proxy(associations[#{name.inspect}]).create!(attrs, &block)
            end
          end_eval
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/plugins/associations/single_association.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/associations/single_association.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/associations/single_association.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/associations/single_association.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/associations/single_association.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/associations/single_association.rb