Sha256: 9a316a264a228decfea7d32b9a39a93fdf420ab8647c4b58cf3536d24aba44eb
Contents?: true
Size: 1.37 KB
Versions: 3
Compression:
Stored size: 1.37 KB
Contents
# This module allows you to alias one field as another. There's a bit of a circuitous route to # getting it done, because you need to push the after_find hook call onto the end of the hash of # existing hook calls. See Rooftop::HookCalls for more details. module Rooftop module FieldAliases def self.included(base) # Include Rooftop::HookCalls to allow us to push things into a list of hooks in the right order base.include Rooftop::HookCalls base.extend ClassMethods # Add the call to the :after_find hook to the list of hook calls, to be processed later. # This is where we iterate over our previously established list of field aliases. base.send(:add_to_hook, :after_find, ->(r){ r.field_aliases.each do |old, new| if r.respond_to?(old) r.send("#{new}=",r.send(old)) end end }) end module ClassMethods # Call alias_field(foo: :bar) in a class to alias the foo as bar. # @param aliases [Sym] a hash of old and new field names def alias_field(*aliases) @field_aliases ||= {} aliases.each do |alias_hash| @field_aliases.merge!(alias_hash) end @field_aliases end end # Class method to get the class's field aliases def field_aliases self.class.instance_variable_get(:"@field_aliases") || {} end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rooftop-0.0.6 | lib/rooftop/field_aliases.rb |
rooftop-0.0.5 | lib/rooftop/field_aliases.rb |
rooftop-0.0.3 | lib/rooftop/field_aliases.rb |