Sha256: 63febc5c03a7169a296d832ebe5c1833819ac76b4a485a3e5481e81e49ef89e2

Contents?: true

Size: 787 Bytes

Versions: 11

Compression:

Stored size: 787 Bytes

Contents

# -*- coding: utf-8 -*-
require 'tengine/core'

module Tengine::Core::FindByName
  extend ActiveSupport::Concern

  class Error < Tengine::Errors::NotFound
    attr_reader :klass, :name, :options
    def initialize(klass, name, options = nil)
      @klass, @name, @options = klass, name, options
    end
    def message
      result = "#{klass.name} named #{name.inspect}"
      result << " with #{options.inspect}" if options && !options.empty?
      result << ' not found'
      result
    end
  end

  module ClassMethods
    def find_by_name(name)
      first(:conditions => {:name => name})
    end

    def find_by_name!(name, *args, &block)
      result = find_by_name(name, *args, &block)
      raise Error.new(self, name, args.last) unless result
      result
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tengine_core-0.5.39 lib/tengine/core/find_by_name.rb
tengine_core-0.5.38 lib/tengine/core/find_by_name.rb
tengine_core-0.5.37 lib/tengine/core/find_by_name.rb
tengine_core-0.5.36 lib/tengine/core/find_by_name.rb
tengine_core-0.5.35 lib/tengine/core/find_by_name.rb
tengine_core-0.5.34 lib/tengine/core/find_by_name.rb
tengine_core-0.5.33 lib/tengine/core/find_by_name.rb
tengine_core-0.5.32 lib/tengine/core/find_by_name.rb
tengine_core-0.5.31 lib/tengine/core/find_by_name.rb
tengine_core-0.5.30 lib/tengine/core/find_by_name.rb
tengine_core-0.5.28 lib/tengine/core/find_by_name.rb