<commit_before># encoding: utf-8
require File.join(File.dirname(__FILE__), "../spec_helper.rb")

describe Her::Model::Hooks do
  context "adding hooks to a model" do
    before do # {{{
      Object.instance_eval { remove_const :User } if Object.const_defined?(:User)
      class User
        include Her::Model
      end
    end # }}}

    describe "method hooks" do
    it "handles “before save” method hooks" do # {{{
      User.before_save :set_internal_id
      User.hooks[:before_save].length.should == 1
      User.hooks[:before_save].first.class.should == Symbol
    end # }}}

    it "handles “before create” method hooks" do # {{{
      User.before_create :set_internal_id
      User.hooks[:before_create].length.should == 1
      User.hooks[:before_create].first.class.should == Symbol
    end # }}}

    it "handles “before update” method hooks" do # {{{
      User.before_update :set_internal_id
      User.hooks[:before_update].length.should == 1
      User.hooks[:before_update].first.class.should == Symbol
    end # }}}

    it "handles “before destroy” method hooks" do # {{{
      User.before_destroy :set_internal_id
      User.hooks[:before_destroy].length.should == 1
      User.hooks[:before_destroy].first.class.should == Symbol
    end # }}}

    it "handles “after save” method hooks" do # {{{
      User.after_save :set_internal_id
      User.hooks[:after_save].length.should == 1
      User.hooks[:after_save].first.class.should == Symbol
    end # }}}

    it "handles “after create” method hooks" do # {{{
      User.after_create :set_internal_id
      User.hooks[:after_create].length.should == 1
      User.hooks[:after_create].first.class.should == Symbol
    end # }}}

    it "handles “after update” method hooks" do # {{{
      User.after_update :set_internal_id
      User.hooks[:after_update].length.should == 1
      User.hooks[:after_update].first.class.should == Symbol
    end # }}}

    it "handles “after destroy” method hooks" do # {{{
      User.after_destroy :set_internal_id
      User.hooks[:after_destroy].length.should == 1
      User.hooks[:after_destroy].first.class.should == Symbol
    end # }}}
    end

    describe "block hooks" do
    it "handles “before save” block hooks" do # {{{
      User.before_save { |record| record.internal_id = 42 }
      User.hooks[:before_save].length.should == 1
      User.hooks[:before_save].first.class.should == Proc
    end # }}}

    it "handles “before create” block hooks" do # {{{
      User.before_create { |record| record.internal_id = 42 }
      User.hooks[:before_create].length.should == 1
      User.hooks[:before_create].first.class.should == Proc
    end # }}}

    it "handles “before update” block hooks" do # {{{
      User.before_update { |record| record.internal_id = 42 }
      User.hooks[:before_update].length.should == 1
      User.hooks[:before_update].first.class.should == Proc
    end # }}}

    it "handles “before destroy” block hooks" do # {{{
      User.before_destroy { |record| record.internal_id = 42 }
      User.hooks[:before_destroy].length.should == 1
      User.hooks[:before_destroy].first.class.should == Proc
    end # }}}

    it "handles “after save” block hooks" do # {{{
      User.after_save { |record| record.internal_id = 42 }
      User.hooks[:after_save].length.should == 1
      User.hooks[:after_save].first.class.should == Proc
    end # }}}

    it "handles “after create” block hooks" do # {{{
      User.after_create { |record| record.internal_id = 42 }
      User.hooks[:after_create].length.should == 1
      User.hooks[:after_create].first.class.should == Proc
    end # }}}

    it "handles “after update” block hooks" do # {{{
      User.after_update { |record| record.internal_id = 42 }
      User.hooks[:after_update].length.should == 1
      User.hooks[:after_update].first.class.should == Proc
    end # }}}

    it "handles “after destroy” block hooks" do # {{{
      User.after_destroy { |record| record.internal_id = 42 }
      User.hooks[:after_destroy].length.should == 1
      User.hooks[:after_destroy].first.class.should == Proc
    end # }}}
    end
  end

  context "perform hooks on a model" do
      before do # {{{
        Her::API.setup :base_uri => "https://api.example.com"
        FakeWeb.register_uri(:post, "https://api.example.com/users", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:put, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:delete, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)

        Object.instance_eval { remove_const :User } if Object.const_defined?(:User)
        class User
          include Her::Model
          attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
          attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id

          def change_internal_save_id; @internal_save_id = 100; end
          def change_internal_create_id; @internal_create_id = 101; end
          def change_internal_update_id; @internal_update_id = 102; end
          def change_internal_destroy_id; @internal_destroy_id = 103; end

          def change_internal_after_save_id;
            @internal_after_save_id = 100;
            p "Resource modifie"
            p self
          end
          def change_internal_after_create_id; @internal_after_create_id = 101; end
          def change_internal_after_update_id; @internal_after_update_id = 102; end
          def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
        end
      end # }}}

    describe "method hooks" do
      before do # {{{
        User.before_save :change_internal_save_id
        User.before_update :change_internal_update_id
        User.before_create :change_internal_create_id
        User.before_destroy :change_internal_destroy_id

        User.after_save :change_internal_after_save_id
        User.after_update :change_internal_after_update_id
        User.after_create :change_internal_after_create_id
        User.after_destroy :change_internal_after_destroy_id<commit_msg>Remove debug code in spec<commit_after># encoding: utf-8
require File.join(File.dirname(__FILE__), "../spec_helper.rb")

describe Her::Model::Hooks do
  context "adding hooks to a model" do
    before do # {{{
      Object.instance_eval { remove_const :User } if Object.const_defined?(:User)
      class User
        include Her::Model
      end
    end # }}}

    describe "method hooks" do
    it "handles “before save” method hooks" do # {{{
      User.before_save :set_internal_id
      User.hooks[:before_save].length.should == 1
      User.hooks[:before_save].first.class.should == Symbol
    end # }}}

    it "handles “before create” method hooks" do # {{{
      User.before_create :set_internal_id
      User.hooks[:before_create].length.should == 1
      User.hooks[:before_create].first.class.should == Symbol
    end # }}}

    it "handles “before update” method hooks" do # {{{
      User.before_update :set_internal_id
      User.hooks[:before_update].length.should == 1
      User.hooks[:before_update].first.class.should == Symbol
    end # }}}

    it "handles “before destroy” method hooks" do # {{{
      User.before_destroy :set_internal_id
      User.hooks[:before_destroy].length.should == 1
      User.hooks[:before_destroy].first.class.should == Symbol
    end # }}}

    it "handles “after save” method hooks" do # {{{
      User.after_save :set_internal_id
      User.hooks[:after_save].length.should == 1
      User.hooks[:after_save].first.class.should == Symbol
    end # }}}

    it "handles “after create” method hooks" do # {{{
      User.after_create :set_internal_id
      User.hooks[:after_create].length.should == 1
      User.hooks[:after_create].first.class.should == Symbol
    end # }}}

    it "handles “after update” method hooks" do # {{{
      User.after_update :set_internal_id
      User.hooks[:after_update].length.should == 1
      User.hooks[:after_update].first.class.should == Symbol
    end # }}}

    it "handles “after destroy” method hooks" do # {{{
      User.after_destroy :set_internal_id
      User.hooks[:after_destroy].length.should == 1
      User.hooks[:after_destroy].first.class.should == Symbol
    end # }}}
    end

    describe "block hooks" do
    it "handles “before save” block hooks" do # {{{
      User.before_save { |record| record.internal_id = 42 }
      User.hooks[:before_save].length.should == 1
      User.hooks[:before_save].first.class.should == Proc
    end # }}}

    it "handles “before create” block hooks" do # {{{
      User.before_create { |record| record.internal_id = 42 }
      User.hooks[:before_create].length.should == 1
      User.hooks[:before_create].first.class.should == Proc
    end # }}}

    it "handles “before update” block hooks" do # {{{
      User.before_update { |record| record.internal_id = 42 }
      User.hooks[:before_update].length.should == 1
      User.hooks[:before_update].first.class.should == Proc
    end # }}}

    it "handles “before destroy” block hooks" do # {{{
      User.before_destroy { |record| record.internal_id = 42 }
      User.hooks[:before_destroy].length.should == 1
      User.hooks[:before_destroy].first.class.should == Proc
    end # }}}

    it "handles “after save” block hooks" do # {{{
      User.after_save { |record| record.internal_id = 42 }
      User.hooks[:after_save].length.should == 1
      User.hooks[:after_save].first.class.should == Proc
    end # }}}

    it "handles “after create” block hooks" do # {{{
      User.after_create { |record| record.internal_id = 42 }
      User.hooks[:after_create].length.should == 1
      User.hooks[:after_create].first.class.should == Proc
    end # }}}

    it "handles “after update” block hooks" do # {{{
      User.after_update { |record| record.internal_id = 42 }
      User.hooks[:after_update].length.should == 1
      User.hooks[:after_update].first.class.should == Proc
    end # }}}

    it "handles “after destroy” block hooks" do # {{{
      User.after_destroy { |record| record.internal_id = 42 }
      User.hooks[:after_destroy].length.should == 1
      User.hooks[:after_destroy].first.class.should == Proc
    end # }}}
    end
  end

  context "perform hooks on a model" do
      before do # {{{
        Her::API.setup :base_uri => "https://api.example.com"
        FakeWeb.register_uri(:post, "https://api.example.com/users", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:get, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:put, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)
        FakeWeb.register_uri(:delete, "https://api.example.com/users/1", :body => { :data => { :id => 1, :name => "<NAME>" } }.to_json)

        Object.instance_eval { remove_const :User } if Object.const_defined?(:User)
        class User
          include Her::Model
          attr_accessor :internal_save_id, :internal_create_id, :internal_update_id, :internal_destroy_id
          attr_accessor :internal_after_save_id, :internal_after_create_id, :internal_after_update_id, :internal_after_destroy_id

          def change_internal_save_id; @internal_save_id = 100; end
          def change_internal_create_id; @internal_create_id = 101; end
          def change_internal_update_id; @internal_update_id = 102; end
          def change_internal_destroy_id; @internal_destroy_id = 103; end

          def change_internal_after_save_id; @internal_after_save_id = 100; end
          def change_internal_after_create_id; @internal_after_create_id = 101; end
          def change_internal_after_update_id; @internal_after_update_id = 102; end
          def change_internal_after_destroy_id; @internal_after_destroy_id = 103; end
        end
      end # }}}

    describe "method hooks" do
      before do # {{{
        User.before_save :change_internal_save_id
        User.before_update :change_internal_update_id
        User.before_create :change_internal_create_id
        User.before_destroy :change_internal_destroy_id

        User.after_save :change_internal_after_save_id
        User.after_update :change_internal_after_update_id
        User.after_create :change_internal_after_create_id
        User.after_destroy :change_internal_after_destroy_id
{'fraction_non_alphanumeric': 0.13117332494495124, 'fraction_numerical': 0.010066058508965083, 'mean_word_length': 3.0890675241157557, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '85522', 'n_tokens_mistral': 4106, 'n_tokens_neox': 3979, 'n_words': 830}