Sha256: a2f7be06cee6b1b5ff5f4f83fc19bbc8377b5de847c3a39c68d394389adb20cd

Contents?: true

Size: 741 Bytes

Versions: 7

Compression:

Stored size: 741 Bytes

Contents

require 'active_model'
require 'active_attr'

class ActiveMimic
  extend ActiveModel::Callbacks
  extend ActiveModel::Naming
  include ActiveModel::Serialization
  include ActiveAttr::Attributes
  include ActiveAttr::MassAssignment
  
  attribute :id
  attribute :type
  
  define_model_callbacks :save
  define_model_callbacks :destroy
  
  def self.create(attrs)
    new(attrs).tap(&:save)
  end
  
  def indexable_id
    "#{self.class.to_s}_#{self.id}"
  end
  
  def type
    self.class.to_s
  end
  
  def save
    self.id = self.class.next_id
    run_callbacks :save do
      true
    end
  end
  
  def destroy
    run_callbacks :destroy do
      true
    end
  end
  
  def self.next_id
    @next_id ||= 0
    @next_id += 1
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
activesearch-0.1.2 spec/spec_helper.rb
activesearch-0.1.1 spec/spec_helper.rb
activesearch-0.1.0 spec/spec_helper.rb
activesearch-0.0.16 spec/spec_helper.rb
activesearch-0.0.15 spec/spec_helper.rb
activesearch-0.0.14 spec/spec_helper.rb
activesearch-0.0.13 spec/spec_helper.rb