Sha256: 01a1c8f7533292b39ad9d61bba3103bafd8b05bc773ea70ece749beb3abcae06

Contents?: true

Size: 564 Bytes

Versions: 7

Compression:

Stored size: 564 Bytes

Contents

# frozen_string_literal: true

class Post
  @posts ||= []

  class << self
    def all
      @posts
    end

    def where(user_id: nil)
      @posts.select do |post|
        post.user_id == Integer(user_id) if user_id
      end
    end

    def insert(record)
      @posts << record
    end
  end

  attr_reader :user_id, :body

  def initialize(user_id:, body:)
    @user_id = Integer(user_id)
    @body    = body

    self.class.insert self
  end
end

# Initial records
Post.new(user_id: 1, body: "I love Ruby!")
Post.new(user_id: 2, body: "I love RubyKaigi!")

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
toycol-1.0.0 examples/safe_ruby_with_sinatra/post.rb
toycol-0.3.1 examples/safe_ruby_with_sinatra/post.rb
toycol-0.3.0 examples/safe_ruby_with_sinatra/post.rb
toycol-0.2.2 examples/safe_ruby_with_sinatra/post.rb
toycol-0.2.1 examples/safe_ruby_with_sinatra/post.rb
toycol-0.2.0 examples/safe_ruby_with_sinatra/post.rb
toycol-0.1.0 examples/safe_ruby_with_sinatra/post.rb