module Flatter::Mapper::Traits
Public Class Methods
new(_, *traits, **, &block)
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 45 def initialize(_, *traits, **, &block) super set_traits(traits) extend_with(block) if block.present? end
Public Instance Methods
extend_with(extension)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 52 def extend_with(extension) singleton_class.trait :extension, label: self.class.name, &extension end
full_name()
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 56 def full_name if name == 'extension_trait' super else name end end
method_missing(name, *args, &block)
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 165 def method_missing(name, *args, &block) if trait? mounter.send(name, *args, &block) else trait = trait_mountings.detect{ |trait| trait.shared_methods.include?(name) } trait ? trait.send(name, *args, &block) : super end end
mounter!()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 161 def mounter! trait? ? mounter.mounter : mounter end
respond_to_missing?(name, *)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 153 def respond_to_missing?(name, *) return false if trait? trait_mountings.any? do |trait| trait.shared_methods.include?(name) end end
set_traits(traits)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 99 def set_traits(traits) @traits = resolve_trait_dependencies(traits) end
trait!()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 135 def trait! @trait = true end
trait?()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 131 def trait? !!@trait end
trait_names()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 95 def trait_names traits.map{ |trait| trait_name_for(trait) } end
traits()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 91 def traits @traits ||= [] end
Private Instance Methods
class_mountings(klass)
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 69 def class_mountings(klass) mountings = super.reject do |factory| factory.trait? && !(factory.name == 'extension_trait' || trait_names.include?(factory.name)) end # For a given mountings list, it's trait factories are reordered according to # order of the trait names specified for a given object. for example, list # [m1, t1, m2, m3, t2, t3, m4] for traits list of [t2, t3, t1] will be # transformed to [m1, t2, m2, m3, t3, t1, m4] traits = trait_names.map{ |name| mountings.find{ |f| f.name == name } }.compact traits. map{ |t| mountings.index(t) }. sort. reverse. each_with_index{ |index, i| mountings[index] = traits[i] } mountings end
include_traits_from!(traits, factories)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 115 def include_traits_from!(traits, factories) initial_length = traits.length traits.map! do |trait| factory = factories.find{ |f| f.name == trait_name_for(trait) } if factory.present? factories.delete(factory) Array(factory.options[:includes]).push(trait) else trait end end traits.flatten! throw :done if traits.length == initial_length end
local_mounting_names()
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 139 def local_mounting_names super.reject{ |name| trait_mountings.any?{ |mount| mount.name == name } } end
local_mountings()
click to toggle source
Calls superclass method
# File lib/flatter/mapper/traits.rb, line 64 def local_mountings @_local_mountings ||= class_mountings_for(singleton_class) + super end
resolve_trait_dependencies(traits)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 108 def resolve_trait_dependencies(traits) factories = self.class.mountings.values.select(&:trait?) catch(:done){ loop{ include_traits_from!(traits, factories) } } traits end
trait_mountings()
click to toggle source
# File lib/flatter/mapper/traits.rb, line 144 def trait_mountings @_trait_mountings ||= local_mountings.select(&:trait?) end
trait_name_for(trait)
click to toggle source
# File lib/flatter/mapper/traits.rb, line 103 def trait_name_for(trait) "#{trait.to_s}_trait" end