Sha256: 8466972814d0a7a65806c4fc0377a9054d2365d9bb39e2a16cbd324056e6ec93

Contents?: true

Size: 1.16 KB

Versions: 7

Compression:

Stored size: 1.16 KB

Contents

class Post
  extend ActiveModel::Naming
  include ActiveModel::Validations

  class << self
    def posts
      POSTS
    end
    private :posts

    def all
      posts.compact
    end

    def create(attrs = {})
      post = new(attrs)
      post.save
      post
    end

    def persist(post)
      id = posts.length
      posts << post
      id
    end

    def unpersist(post)
      posts[post.id] = nil
    end

    def find(id)
      posts[id.to_i] or raise "not found"
    end

    def count
      posts.compact.length
    end
  end

  attr_reader :id
  attr_accessor :title, :body

  def initialize(attrs = {})
    self.attributes = attrs
  end

  def attributes=(attrs)
    return unless attrs
    attrs.each { |k, v| send("#{k}=", v) }
  end

  def attributes
    { 'title' => title, 'body' => body }
  end

  def to_model
    self
  end

  def to_param
    id.to_s
  end

  def to_key
    [id] if id
  end

  def persisted?
    !id.nil?
  end

  def save
    unless persisted?
      @id = self.class.persist(self)
    end
    true
  end

  def update_attributes(attrs)
    self.attributes = attrs
    save
  end

  def destroy
    self.class.unpersist(self)
    true
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
controll-0.3.2 spec/controll_app/app/models/post.rb
controll-0.3.1 spec/controll_app/app/models/post.rb
focused_controller-1.0.0 test/app/app/models/post.rb
controll-0.3.0 spec/controll_app/app/models/post.rb
controll-0.2.0 spec/app/app/models/post.rb
focused_controller-0.2.0 test/app/app/models/post.rb
focused_controller-0.1.0 test/app/app/models/post.rb