Skip to content

Instantly share code, notes, and snippets.

@tbuehlmann
Created August 3, 2018 09:55
Show Gist options
  • Save tbuehlmann/9fb25202b7ff938f18d7433dd514c060 to your computer and use it in GitHub Desktop.
Save tbuehlmann/9fb25202b7ff938f18d7433dd514c060 to your computer and use it in GitHub Desktop.
class Foo < ApplicationRecord
def array
self[:array] ? LimitedArray.new(self[:array]) : self[:array]
end
def array=(value)
self[:array] = value ? LimitedArray.new(value.dup).to_a : value
end
end
class LimitedArray
def initialize(array, limit: 10)
@array = array
@array.slice!(limit..-1)
@limit = 10
end
def method_missing(method, *args, &block)
@array.public_send(method, *args, &block).tap do
@array.slice!(@limit..-1)
end
end
def respond_to_missing?(name, include_private = false)
@array.respond_to?(name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment