<commit_before>require 'uuidtools'

module RAWS::SDB::Model
  class Select < RAWS::SDB::Select
    def initialize(model)
      super()
      @model = model
    end

    def attr_filter(val)
      @model.new(val.last, val.first)
    end
  end

  module ClassMethods
    attr_accessor :domain_name

    def create_domain
      RAWS::SDB[domain_name].create_domain
    end

    def delete_domain
      RAWS::SDB[domain_name].delete_domain
    end

    def select
      Select.new(self).from(domain_name)
    end

    def find(id)
      if attrs = RAWS::SDB[domain_name].get(id)
        self.new(id, attrs)
      end
    end

    def generate_id
      UUIDTools::UUID.random_create
    end

    def sdb_reader(*names)
      names.each do |name|
        module_eval %Q{
          def #{name}
            self['#{name}']
          end
        }
      end
    end

    def sdb_writer(*names)
      names.each do |name|
        module_eval %Q{
          def #{name}=(val)
            self['#{name}'] = val
          end
        }
      end
    end

    def sdb_accessor(*names)
      sdb_reader(*names)
      sdb_writer(*names)
    end
  end
  
  module InstanceMethods
    attr_reader :id, :values

    def initialize(values={}, id=nil)
      @id, @values = id, values
      after_initialize
    end

    def [](key)
      values[key]
    end

    def []=(key, val)
      values[key] = val
    end

    def delete
      before_delete
      RAWS::SDB[self.class.domain_name].delete(id) if id
      after_delete
    end

    def save
      before_save
      if id
        before_update
        RAWS::SDB[self.class.domain_name].put(id, values)
        after_update
      else
        before_insert
        @id = self.class.generate_id
        RAWS::SDB[self.class.domain_name].put(id, values)
        after_insert
      end
      after_save
    end

    def after_initialize; end
    def before_delete; end
    def after_delete; end
    def before_save; end
    def after_save; end
    def before_update; end
    def after_update; end
    def before_insert; end
    def after_insert; end
  end

  def self.included(mod)
    mod.class_eval do
      include InstanceMethods
      extend ClassMethods
    end
  end
end
<commit_msg>あっ！間違ってた。。。<commit_after>require 'uuidtools'

module RAWS::SDB::Model
  class Select < RAWS::SDB::Select
    def initialize(model)
      super()
      @model = model
    end

    def attr_filter(val)
      @model.new(val.last, val.first)
    end
  end

  module ClassMethods
    attr_accessor :domain_name

    def create_domain
      RAWS::SDB[domain_name].create_domain
    end

    def delete_domain
      RAWS::SDB[domain_name].delete_domain
    end

    def select
      Select.new(self).from(domain_name)
    end

    def find(id)
      if attrs = RAWS::SDB[domain_name].get(id)
        self.new(attrs, id)
      end
    end

    def generate_id
      UUIDTools::UUID.random_create
    end

    def sdb_reader(*names)
      names.each do |name|
        module_eval %Q{
          def #{name}
            self['#{name}']
          end
        }
      end
    end

    def sdb_writer(*names)
      names.each do |name|
        module_eval %Q{
          def #{name}=(val)
            self['#{name}'] = val
          end
        }
      end
    end

    def sdb_accessor(*names)
      sdb_reader(*names)
      sdb_writer(*names)
    end
  end
  
  module InstanceMethods
    attr_reader :id, :values

    def initialize(values={}, id=nil)
      @id, @values = id, values
      after_initialize
    end

    def [](key)
      values[key]
    end

    def []=(key, val)
      values[key] = val
    end

    def delete
      before_delete
      RAWS::SDB[self.class.domain_name].delete(id) if id
      after_delete
    end

    def save
      before_save
      if id
        before_update
        RAWS::SDB[self.class.domain_name].put(id, values)
        after_update
      else
        before_insert
        @id = self.class.generate_id
        RAWS::SDB[self.class.domain_name].put(id, values)
        after_insert
      end
      after_save
    end

    def after_initialize; end
    def before_delete; end
    def after_delete; end
    def before_save; end
    def after_save; end
    def before_update; end
    def after_update; end
    def before_insert; end
    def after_insert; end
  end

  def self.included(mod)
    mod.class_eval do
      include InstanceMethods
      extend ClassMethods
    end
  end
end

{'fraction_non_alphanumeric': 0.08656447249774572, 'fraction_numerical': 0.0, 'mean_word_length': 1.9639278557114228, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5792554', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1449, 'n_words': 342}