{"action":"created","comment":{"url":"https://api.github.com/repos/lotus/model/pulls/comments/29997503","id":29997503,"diff_hunk":"@@ -0,0 +1,196 @@\n+module Lotus\n+  module Repository\n+    # Timestamps handling using an entity's @created_at and @updated_at.\n+    #\n+    # @since x.x.x\n+    module Timestamps\n+      # Override existing public API into hosting class to support @created_at\n+      # and @updated_at using Ruby implementations (database agnostic).\n+      #\n+      # @since x.x.x\n+      #\n+      # @example\n+      #   require 'lotus/model'\n+      #\n+      #   class UserRepository\n+      #     include Lotus::Repository\n+      #     include Lotus::Repository::Timestamps\n+      #   end\n+      def self.included(base)\n+        base.class_eval do\n+          extend ClassMethods\n+        end\n+      end\n+\n+      module ClassMethods\n+        # Before creating or updating a record in the database for the given\n+        # entity, sets the entity's @created_at and @updated_at to the current\n+        # time in UTC. @created_at is left untouched if already set.\n+        #\n+        # @param entity [#id, #id=] the entity to persist\n+        #\n+        # @return [Object] the entity\n+        #\n+        # @since x.x.x\n+        #\n+        # @see Lotus::Repository#persist\n+        #\n+        # @example With a non persisted entity\n+        #   require 'lotus/model'\n+        #\n+        #   class ArticleRepository\n+        #     include Lotus::Repository\n+        #     include Lotus::Repository::Timestamps\n+        #   end\n+        #\n+        #   article = Article.new(title: 'Introducing Lotus::Model')\n+        #   article.id # => nil\n+        #   article.created_at # => nil\n+        #   article.updated_at # => nil\n+        #\n+        #   ArticleRepository.persist(article) # creates a record\n+        #   article.id # => 23\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:00:00 UTC\n+        #\n+        # @example With a persisted entity\n+        #   require 'lotus/model'\n+        #\n+        #   class ArticleRepository\n+        #     include Lotus::Repository\n+        #     include Lotus::Repository::Timestamps\n+        #   end\n+        #\n+        #   article = ArticleRepository.find(23)\n+        #   article.id # => 23\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:00:00 UTC\n+        #\n+        #   article.title = 'Launching Lotus::Model'\n+        #   ArticleRepository.persist(article) # updates the record\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:20:35 UTC\n+        #\n+        #   article = ArticleRepository.find(23)\n+        #   article.title # => \"Launching Lotus::Model\"\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:20:35 UTC\n+        def persist(entity)\n+          _update_timestamps(entity)\n+          super\n+        end\n+\n+        # Before creating a record in the database for the given entity, sets\n+        # the entity's @created_at and @updated_at to the current time in UTC.\n+        # @created_at is left untouched if already set.\n+        #\n+        # If already persisted (`id` present) it does nothing.\n+        #\n+        # @param entity [#id,#id=] the entity to create\n+        #\n+        # @return [Object] the entity\n+        #\n+        # @since x.x.x\n+        #\n+        # @see Lotus::Repository#create\n+        #\n+        # @example\n+        #   require 'lotus/model'\n+        #\n+        #   class ArticleRepository\n+        #     include Lotus::Repository\n+        #     include Lotus::Repository::Timestamps\n+        #   end\n+        #\n+        #   article = Article.new(title: 'Introducing Lotus::Model')\n+        #   article.id # => nil\n+        #   article.created_at # => nil\n+        #   article.updated_at # => nil\n+        #\n+        #   ArticleRepository.create(article) # creates a record\n+        #   article.id # => 23\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:00:00 UTC\n+        #\n+        #   ArticleRepository.create(article) # no-op\n+        def create(entity)\n+          _update_timestamps(entity) unless entity.id\n+          super\n+        end\n+\n+        # Before updating a record in the database corresponding to the given\n+        # entity, sets the entity's @created_at and @updated_at to the current\n+        # time in UTC. @created_at is left untouched if already set.\n+        #\n+        # If not already persisted (`id` present) it raises an exception.\n+        #\n+        # @param entity [#id] the entity to update\n+        #\n+        # @return [Object] the entity\n+        #\n+        # @raise [Lotus::Model::NonPersistedEntityError] if the given entity\n+        #   wasn't already persisted.\n+        #\n+        # @since x.x.x\n+        #\n+        # @see Lotus::Repository#update\n+        # @see Lotus::Model::NonPersistedEntityError\n+        #\n+        # @example With a persisted entity\n+        #   require 'lotus/model'\n+        #\n+        #   class ArticleRepository\n+        #     include Lotus::Repository\n+        #     include Lotus::Repository::Timestamps\n+        #   end\n+        #\n+        #   article = ArticleRepository.find(23)\n+        #   article.id # => 23\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:00:00 UTC\n+        #   article.title = 'Launching Lotus::Model'\n+        #\n+        #   ArticleRepository.update(article) # updates the record\n+        #   article.created_at # => 2015-02-20 10:00:00 UTC\n+        #   article.updated_at # => 2015-02-20 10:20:35 UTC\n+        #\n+        #\n+        #\n+        # @example With a non persisted entity\n+        #   require 'lotus/model'\n+        #\n+        #   class ArticleRepository\n+        #     include Lotus::Repository\n+        #   end\n+        #\n+        #   article = Article.new(title: 'Introducing Lotus::Model')\n+        #   article.id # => nil\n+        #   article.created_at # => nil\n+        #   article.updated_at # => nil\n+        #\n+        #   ArticleRepository.update(article) # raises Lotus::Model::NonPersistedEntityError\n+        def update(entity)\n+          _update_timestamps(entity) if entity.id\n+          super\n+        end\n+\n+        private\n+\n+        # Sets @created_at and @updated_at for the given entity to the current\n+        # time in UTC. @created_at is left untouched if already set.\n+        #\n+        # @param entity [#id] the entity to update\n+        #\n+        # @return [Object] the entity\n+        #\n+        # @api private\n+        # @since x.x.x\n+        def _update_timestamps(entity)","path":"lib/lotus/repository/timestamps.rb","position":188,"original_position":188,"commit_id":"819712c93466ed7bd52f22da0013b9425fc93aaf","original_commit_id":"819712c93466ed7bd52f22da0013b9425fc93aaf","user":{"login":"joneslee85","id":135605,"avatar_url":"https://avatars.githubusercontent.com/u/135605?v=3","gravatar_id":"","url":"https://api.github.com/users/joneslee85","html_url":"https://github.com/joneslee85","followers_url":"https://api.github.com/users/joneslee85/followers","following_url":"https://api.github.com/users/joneslee85/following{/other_user}","gists_url":"https://api.github.com/users/joneslee85/gists{/gist_id}","starred_url":"https://api.github.com/users/joneslee85/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/joneslee85/subscriptions","organizations_url":"https://api.github.com/users/joneslee85/orgs","repos_url":"https://api.github.com/users/joneslee85/repos","events_url":"https://api.github.com/users/joneslee85/events{/privacy}","received_events_url":"https://api.github.com/users/joneslee85/received_events","type":"User","site_admin":false},"body":"FYI #182 has added this update callback in Repository, thus you could remove it","created_at":"2015-05-10T00:20:03Z","updated_at":"2015-05-10T00:20:03Z","html_url":"https://github.com/lotus/model/pull/169#discussion_r29997503","pull_request_url":"https://api.github.com/repos/lotus/model/pulls/169","_links":{"self":{"href":"https://api.github.com/repos/lotus/model/pulls/comments/29997503"},"html":{"href":"https://github.com/lotus/model/pull/169#discussion_r29997503"},"pull_request":{"href":"https://api.github.com/repos/lotus/model/pulls/169"}}},"pull_request":{"url":"https://api.github.com/repos/lotus/model/pulls/169","id":32420389,"html_url":"https://github.com/lotus/model/pull/169","diff_url":"https://github.com/lotus/model/pull/169.diff","patch_url":"https://github.com/lotus/model/pull/169.patch","issue_url":"https://api.github.com/repos/lotus/model/issues/169","number":169,"state":"open","locked":false,"title":"Add repository timestamps","user":{"login":"dsnipe","id":1488587,"avatar_url":"https://avatars.githubusercontent.com/u/1488587?v=3","gravatar_id":"","url":"https://api.github.com/users/dsnipe","html_url":"https://github.com/dsnipe","followers_url":"https://api.github.com/users/dsnipe/followers","following_url":"https://api.github.com/users/dsnipe/following{/other_user}","gists_url":"https://api.github.com/users/dsnipe/gists{/gist_id}","starred_url":"https://api.github.com/users/dsnipe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsnipe/subscriptions","organizations_url":"https://api.github.com/users/dsnipe/orgs","repos_url":"https://api.github.com/users/dsnipe/repos","events_url":"https://api.github.com/users/dsnipe/events{/privacy}","received_events_url":"https://api.github.com/users/dsnipe/received_events","type":"User","site_admin":false},"body":"PR based on discussion in #134","created_at":"2015-04-01T13:50:04Z","updated_at":"2015-05-10T00:20:03Z","closed_at":null,"merged_at":null,"merge_commit_sha":"fb984c9ec424edd68a42c4971d54115061bc0233","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/lotus/model/pulls/169/commits","review_comments_url":"https://api.github.com/repos/lotus/model/pulls/169/comments","review_comment_url":"https://api.github.com/repos/lotus/model/pulls/comments{/number}","comments_url":"https://api.github.com/repos/lotus/model/issues/169/comments","statuses_url":"https://api.github.com/repos/lotus/model/statuses/819712c93466ed7bd52f22da0013b9425fc93aaf","head":{"label":"dsnipe:feature-timestamps","ref":"feature-timestamps","sha":"819712c93466ed7bd52f22da0013b9425fc93aaf","user":{"login":"dsnipe","id":1488587,"avatar_url":"https://avatars.githubusercontent.com/u/1488587?v=3","gravatar_id":"","url":"https://api.github.com/users/dsnipe","html_url":"https://github.com/dsnipe","followers_url":"https://api.github.com/users/dsnipe/followers","following_url":"https://api.github.com/users/dsnipe/following{/other_user}","gists_url":"https://api.github.com/users/dsnipe/gists{/gist_id}","starred_url":"https://api.github.com/users/dsnipe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsnipe/subscriptions","organizations_url":"https://api.github.com/users/dsnipe/orgs","repos_url":"https://api.github.com/users/dsnipe/repos","events_url":"https://api.github.com/users/dsnipe/events{/privacy}","received_events_url":"https://api.github.com/users/dsnipe/received_events","type":"User","site_admin":false},"repo":{"id":33125062,"name":"model","full_name":"dsnipe/model","owner":{"login":"dsnipe","id":1488587,"avatar_url":"https://avatars.githubusercontent.com/u/1488587?v=3","gravatar_id":"","url":"https://api.github.com/users/dsnipe","html_url":"https://github.com/dsnipe","followers_url":"https://api.github.com/users/dsnipe/followers","following_url":"https://api.github.com/users/dsnipe/following{/other_user}","gists_url":"https://api.github.com/users/dsnipe/gists{/gist_id}","starred_url":"https://api.github.com/users/dsnipe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dsnipe/subscriptions","organizations_url":"https://api.github.com/users/dsnipe/orgs","repos_url":"https://api.github.com/users/dsnipe/repos","events_url":"https://api.github.com/users/dsnipe/events{/privacy}","received_events_url":"https://api.github.com/users/dsnipe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dsnipe/model","description":"A persistence framework with entities, repositories, data mapper and query objects","fork":true,"url":"https://api.github.com/repos/dsnipe/model","forks_url":"https://api.github.com/repos/dsnipe/model/forks","keys_url":"https://api.github.com/repos/dsnipe/model/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dsnipe/model/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dsnipe/model/teams","hooks_url":"https://api.github.com/repos/dsnipe/model/hooks","issue_events_url":"https://api.github.com/repos/dsnipe/model/issues/events{/number}","events_url":"https://api.github.com/repos/dsnipe/model/events","assignees_url":"https://api.github.com/repos/dsnipe/model/assignees{/user}","branches_url":"https://api.github.com/repos/dsnipe/model/branches{/branch}","tags_url":"https://api.github.com/repos/dsnipe/model/tags","blobs_url":"https://api.github.com/repos/dsnipe/model/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dsnipe/model/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dsnipe/model/git/refs{/sha}","trees_url":"https://api.github.com/repos/dsnipe/model/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dsnipe/model/statuses/{sha}","languages_url":"https://api.github.com/repos/dsnipe/model/languages","stargazers_url":"https://api.github.com/repos/dsnipe/model/stargazers","contributors_url":"https://api.github.com/repos/dsnipe/model/contributors","subscribers_url":"https://api.github.com/repos/dsnipe/model/subscribers","subscription_url":"https://api.github.com/repos/dsnipe/model/subscription","commits_url":"https://api.github.com/repos/dsnipe/model/commits{/sha}","git_commits_url":"https://api.github.com/repos/dsnipe/model/git/commits{/sha}","comments_url":"https://api.github.com/repos/dsnipe/model/comments{/number}","issue_comment_url":"https://api.github.com/repos/dsnipe/model/issues/comments{/number}","contents_url":"https://api.github.com/repos/dsnipe/model/contents/{+path}","compare_url":"https://api.github.com/repos/dsnipe/model/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dsnipe/model/merges","archive_url":"https://api.github.com/repos/dsnipe/model/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dsnipe/model/downloads","issues_url":"https://api.github.com/repos/dsnipe/model/issues{/number}","pulls_url":"https://api.github.com/repos/dsnipe/model/pulls{/number}","milestones_url":"https://api.github.com/repos/dsnipe/model/milestones{/number}","notifications_url":"https://api.github.com/repos/dsnipe/model/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dsnipe/model/labels{/name}","releases_url":"https://api.github.com/repos/dsnipe/model/releases{/id}","created_at":"2015-03-30T13:36:24Z","updated_at":"2015-04-10T12:41:56Z","pushed_at":"2015-04-23T15:18:23Z","git_url":"git://github.com/dsnipe/model.git","ssh_url":"git@github.com:dsnipe/model.git","clone_url":"https://github.com/dsnipe/model.git","svn_url":"https://github.com/dsnipe/model","homepage":"","size":697,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"lotus:master","ref":"master","sha":"330873aaf5159427102fbe504d1e4ada46996beb","user":{"login":"lotus","id":3210273,"avatar_url":"https://avatars.githubusercontent.com/u/3210273?v=3","gravatar_id":"","url":"https://api.github.com/users/lotus","html_url":"https://github.com/lotus","followers_url":"https://api.github.com/users/lotus/followers","following_url":"https://api.github.com/users/lotus/following{/other_user}","gists_url":"https://api.github.com/users/lotus/gists{/gist_id}","starred_url":"https://api.github.com/users/lotus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lotus/subscriptions","organizations_url":"https://api.github.com/users/lotus/orgs","repos_url":"https://api.github.com/users/lotus/repos","events_url":"https://api.github.com/users/lotus/events{/privacy}","received_events_url":"https://api.github.com/users/lotus/received_events","type":"Organization","site_admin":false},"repo":{"id":16760719,"name":"model","full_name":"lotus/model","owner":{"login":"lotus","id":3210273,"avatar_url":"https://avatars.githubusercontent.com/u/3210273?v=3","gravatar_id":"","url":"https://api.github.com/users/lotus","html_url":"https://github.com/lotus","followers_url":"https://api.github.com/users/lotus/followers","following_url":"https://api.github.com/users/lotus/following{/other_user}","gists_url":"https://api.github.com/users/lotus/gists{/gist_id}","starred_url":"https://api.github.com/users/lotus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lotus/subscriptions","organizations_url":"https://api.github.com/users/lotus/orgs","repos_url":"https://api.github.com/users/lotus/repos","events_url":"https://api.github.com/users/lotus/events{/privacy}","received_events_url":"https://api.github.com/users/lotus/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/lotus/model","description":"A persistence framework with entities, repositories, data mapper and query objects","fork":false,"url":"https://api.github.com/repos/lotus/model","forks_url":"https://api.github.com/repos/lotus/model/forks","keys_url":"https://api.github.com/repos/lotus/model/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lotus/model/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lotus/model/teams","hooks_url":"https://api.github.com/repos/lotus/model/hooks","issue_events_url":"https://api.github.com/repos/lotus/model/issues/events{/number}","events_url":"https://api.github.com/repos/lotus/model/events","assignees_url":"https://api.github.com/repos/lotus/model/assignees{/user}","branches_url":"https://api.github.com/repos/lotus/model/branches{/branch}","tags_url":"https://api.github.com/repos/lotus/model/tags","blobs_url":"https://api.github.com/repos/lotus/model/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lotus/model/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lotus/model/git/refs{/sha}","trees_url":"https://api.github.com/repos/lotus/model/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lotus/model/statuses/{sha}","languages_url":"https://api.github.com/repos/lotus/model/languages","stargazers_url":"https://api.github.com/repos/lotus/model/stargazers","contributors_url":"https://api.github.com/repos/lotus/model/contributors","subscribers_url":"https://api.github.com/repos/lotus/model/subscribers","subscription_url":"https://api.github.com/repos/lotus/model/subscription","commits_url":"https://api.github.com/repos/lotus/model/commits{/sha}","git_commits_url":"https://api.github.com/repos/lotus/model/git/commits{/sha}","comments_url":"https://api.github.com/repos/lotus/model/comments{/number}","issue_comment_url":"https://api.github.com/repos/lotus/model/issues/comments{/number}","contents_url":"https://api.github.com/repos/lotus/model/contents/{+path}","compare_url":"https://api.github.com/repos/lotus/model/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lotus/model/merges","archive_url":"https://api.github.com/repos/lotus/model/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lotus/model/downloads","issues_url":"https://api.github.com/repos/lotus/model/issues{/number}","pulls_url":"https://api.github.com/repos/lotus/model/pulls{/number}","milestones_url":"https://api.github.com/repos/lotus/model/milestones{/number}","notifications_url":"https://api.github.com/repos/lotus/model/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lotus/model/labels{/name}","releases_url":"https://api.github.com/repos/lotus/model/releases{/id}","created_at":"2014-02-12T08:25:42Z","updated_at":"2015-05-08T21:54:40Z","pushed_at":"2015-05-10T00:17:41Z","git_url":"git://github.com/lotus/model.git","ssh_url":"git@github.com:lotus/model.git","clone_url":"https://github.com/lotus/model.git","svn_url":"https://github.com/lotus/model","homepage":"","size":3040,"stargazers_count":198,"watchers_count":198,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":68,"mirror_url":null,"open_issues_count":12,"forks":68,"open_issues":12,"watchers":198,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/lotus/model/pulls/169"},"html":{"href":"https://github.com/lotus/model/pull/169"},"issue":{"href":"https://api.github.com/repos/lotus/model/issues/169"},"comments":{"href":"https://api.github.com/repos/lotus/model/issues/169/comments"},"review_comments":{"href":"https://api.github.com/repos/lotus/model/pulls/169/comments"},"review_comment":{"href":"https://api.github.com/repos/lotus/model/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/lotus/model/pulls/169/commits"},"statuses":{"href":"https://api.github.com/repos/lotus/model/statuses/819712c93466ed7bd52f22da0013b9425fc93aaf"}}}}