Created
August 3, 2018 09:55
-
-
Save tbuehlmann/9fb25202b7ff938f18d7433dd514c060 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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