Sha256: dc99f9c24620b5c5327d1de4ef553931d6c94a24c0a0e9a17914d27bc4fd8623

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require 'rubygems'
gem 'dm-core', '=0.9.4'
require 'dm-core'

module DataMapper
  module Model
    def find_or_create(search_attributes, create_attributes = {})
      first(search_attributes) || create(search_attributes.merge(create_attributes))
    end

    private

    def method_missing_with_find_by(method, *args, &block)
      if match = matches_dynamic_finder?(method)
        finder = determine_finder(match)
        attribute_names = extract_attribute_names_from_match(match)

        conditions = {}
        attribute_names.each {|key| conditions[key] = args.shift}

        send(finder, conditions)
      else
        method_missing_without_find_by(method, *args, &block)
      end
    end

    alias_method :method_missing_without_find_by, :method_missing
    alias_method :method_missing, :method_missing_with_find_by

    def matches_dynamic_finder?(method_id)
      /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
    end

    def determine_finder(match)
      match.captures.first == 'all_by' ? :all : :first
    end

    def extract_attribute_names_from_match(match)
      match.captures.last.split('_and_')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dm-ar-finders-0.9.4 lib/dm-ar-finders.rb