Sha256: 8c872fdf60036057140baa0818fbbd9745a56eea167a3f58c3592b38fa16da16

Contents?: true

Size: 853 Bytes

Versions: 1

Compression:

Stored size: 853 Bytes

Contents

module Shoden
  Proxy = Struct.new(:klass, :parent) do
    def create(args = {})
      klass.create(args.merge(key => parent.id))
    end

    def all
      klass.filter(parent_filter)
    end

    def count
      row = klass.query(fields: "COUNT(id)", conditions: parent_filter)
      row.first["count"].to_i
    end

    def any?
      count > 0
    end

    def first
      filter = { order: "id ASC LIMIT 1" }.merge!(parent_filter)
      klass.filter(filter).first
    end

    def last
      filter = { order: "id DESC LIMIT 1" }.merge!(parent_filter)
      klass.filter(filter).first
    end

    def [](id)
      filter = { id: id }.merge!(parent_filter)

      klass.filter(filter).first
    end

    private

    def parent_filter
      { key => parent.id }
    end

    def key
      "#{parent.class.to_reference}_id".freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shoden-1.0 lib/shoden/proxy.rb