<commit_before>require "spira/timestamps/version"

module Spira
  module Timestamps
    def self.included(model)
      model.before_save :before_save
      model.extend ClassMethods
      model.timestamps
    end

    # Update this model's `updated` time without making any other changes.
    def touch
      update_timestamps
      save
    end

    private

    def before_save
      return unless changed?
      update_timestamps
    end

    def update_timestamps
      update_created if has_created?
      update_updated if has_updated?
    end

    def update_created
      self.created = self.created || DateTime.now
    end

    def update_updated
      self.updated = DateTime.now
    end

    def has_created?
      attributes.keys.include? 'created'
    end

    def has_updated?
      attributes.keys.include? 'updated'
    end

    module ClassMethods
      # Add timestamps to this model.
      def timestamps
        property :created, predicate: RDF::DC.created
        add_created_aliases

        property :updated, predicate: RDF::DC.modified
        add_updated_aliases
      end

      private

      def add_created_aliases
        alias_attribute :created_at, :created

        define_method("created_on") do
          self.created && self.created.to_date
        end

        define_method("created_on=") do |date|
          self.created = date && date.to_datetime
        end
      end

      def add_updated_aliases
        alias_attribute :updated_at, :updated

        define_method("updated_on") do
          self.updated && self.updated.to_date
        end

        define_method("updated_on=") do |date|
          self.updated = date && date.to_datetime
        end<commit_msg>Inline has_created? and has_updated? and collapse define_method calls.<commit_after>require "spira/timestamps/version"

module Spira
  module Timestamps
    def self.included(model)
      model.before_save :before_save
      model.extend ClassMethods
      model.timestamps
    end

    # Update this model's `updated` time without making any other changes.
    def touch
      update_timestamps
      save
    end

    private

    def before_save
      return unless changed?
      update_timestamps
    end

    def update_timestamps
      update_created if attributes.keys.include? 'created'
      update_updated if attributes.keys.include? 'updated'
    end

    def update_created
      self.created = self.created || DateTime.now
    end

    def update_updated
      self.updated = DateTime.now
    end

    module ClassMethods
      # Add timestamps to this model.
      def timestamps
        property :created, predicate: RDF::DC.created
        add_created_aliases

        property :updated, predicate: RDF::DC.modified
        add_updated_aliases
      end

      private

      def add_created_aliases
        alias_attribute :created_at, :created

        define_method("created_on") { self.created && self.created.to_date }
        define_method("created_on=") { |date| self.created = date && date.to_datetime }
      end

      def add_updated_aliases
        alias_attribute :updated_at, :updated

        define_method("updated_on") { self.updated && self.updated.to_date }
        define_method("updated_on=") { |date| self.updated = date && date.to_datetime }
{'fraction_non_alphanumeric': 0.06597750076010946, 'fraction_numerical': 0.0, 'mean_word_length': 2.4378265412748172, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1323327', 'n_tokens_mistral': 950, 'n_tokens_neox': 912, 'n_words': 236}