Sha256: 9790d589aea7963837fc4b34627b022005dd6a7f0e98d366b1c5b232267c4be7

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

module Troo
  module ModelHelpers
    module ClassMethods
      # @param  [Hash]
      # @return [Ohm::Model]
      def first(criteria = {})
        return all.first if criteria.nil? || criteria.empty?
        find(criteria).first
      end

      # @param  [Hash]
      # @return [TrueClass]
      def update(criteria = {})
        return false if criteria.nil? || criteria.empty?
        all.map { |record| record.update(criteria) }
        true
      end

      # @return [Ohm::Model]
      def default
        first(default: true)
      end

      # @return [TrueClass, FalseClass]
      def default?
        !!(default)
      end

      # @param  [Hash]
      # @return [Integer]
      def count(criteria = {})
        return all.count if criteria.empty?
        find(criteria).size
      end

      # @param  [String]
      # @return [Ohm::Model]
      def by_external_id(id)
        first(external_id: id)
      end

      # @param  [String, NilClass]
      # @param  [Hash]
      # @return [Ohm::Model]
      def retrieve(id = nil, options = {})
        Troo::Retrieval::Local.retrieve(self, id, options)
      end
    end

    module InstanceMethods
      def set_default!
        Behaviours::SetDefault.for(self)
      end
    end

    def self.included(receiver)
      receiver.extend(ClassMethods)
      receiver.send(:include, InstanceMethods)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
troo-0.0.15 lib/troo/helpers/model_helpers.rb
troo-0.0.14 lib/troo/helpers/model_helpers.rb
troo-0.0.13 lib/troo/helpers/model_helpers.rb
troo-0.0.12 lib/troo/helpers/model_helpers.rb
troo-0.0.11 lib/troo/helpers/model_helpers.rb