Module: Flatter::Mapper::Traits

Extended by:
ActiveSupport::Concern
Included in:
Flatter::Mapper
Defined in:
lib/flatter/mapper/traits.rb

Defined Under Namespace

Modules: ClassMethods, FactoryMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



165
166
167
168
169
170
171
172
# 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

Instance Method Details

#extend_with(extension) ⇒ Object



52
53
54
# File 'lib/flatter/mapper/traits.rb', line 52

def extend_with(extension)
  singleton_class.trait :extension, label: self.class.name, &extension
end

#full_nameObject



56
57
58
59
60
61
62
# File 'lib/flatter/mapper/traits.rb', line 56

def full_name
  if name == 'extension_trait'
    super
  else
    name
  end
end

#initialize(_, *traits, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/flatter/mapper/traits.rb', line 45

def initialize(_, *traits, **, &block)
  super

  set_traits(traits)
  extend_with(block) if block.present?
end

#mounter!Object



161
162
163
# File 'lib/flatter/mapper/traits.rb', line 161

def mounter!
  trait? ? mounter.mounter : mounter
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
# 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) ⇒ Object



99
100
101
# File 'lib/flatter/mapper/traits.rb', line 99

def set_traits(traits)
  @traits = resolve_trait_dependencies(traits)
end

#shared_methodsObject



149
150
151
# File 'lib/flatter/mapper/traits.rb', line 149

def shared_methods
  self.class.public_instance_methods(false) + self.class.protected_instance_methods(false)
end

#trait!Object



135
136
137
# File 'lib/flatter/mapper/traits.rb', line 135

def trait!
  @trait = true
end

#trait?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/flatter/mapper/traits.rb', line 131

def trait?
  !!@trait
end

#trait_namesObject



95
96
97
# File 'lib/flatter/mapper/traits.rb', line 95

def trait_names
  traits.map{ |trait| trait_name_for(trait) }
end

#traitsObject



91
92
93
# File 'lib/flatter/mapper/traits.rb', line 91

def traits
  @traits ||= []
end