Sha256: 595a6573c5dbc7639b921c5e7aebec97cebb7a68c46dcc055479fef9d5276c2e

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Hobo

  module Lifecycles

    module Actions

      def set_or_check_who!(record, user)
        case who
        when :nobody
          user == :nobody
        when :anybody
          true
        when :self
          record == user
        when Array
          who.detect {|attribute| record.send(attribute) == user }
        else
          if (current = record.send(who)) # it's already set, check it's the same user
            user == current
          elsif user.is_a?(record.class.attr_type(who))
            record.send("#{who}=", user)
            true
          else
            false
          end
        end
      end


      def run_hook(record, hook, *args)
        if hook.is_a?(Symbol)
          record.send(hook, *args)
        elsif hook.is_a?(Proc)
          hook.call(record, *args)
        end
      end


      def fire_event(record, event)
        record.instance_eval(&event) if event
      end


      def check_guard(record, user)
        !options[:if] || run_hook(record, options[:if], user)
      end

      def check_invariants(record)
        record.lifecycle.invariants_satisfied?
      end


      def prepare(record, user, attributes=nil)
        if attributes
          attributes = extract_attributes(attributes)
          record.attributes = attributes
        end
        set_or_check_who!(record, user) && record
      end


      def prepare_and_check!(record, user, attributes=nil)
        prepare(record, user, attributes) && check_guard(record, user) && check_invariants(record)
      end
      
      def publishable?
        who != :nobody
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hobo-0.8 lib/hobo/lifecycles/actions.rb