Class: Module

Inherits:
Object show all
Defined in:
motion/core_ext/module/aliasing.rb,
motion/core_ext/module/reachable.rb,
motion/core_ext/module/anonymous.rb,
motion/core_ext/module/delegation.rb,
motion/core_ext/module/remove_method.rb,
motion/core_ext/module/attr_internal.rb,
motion/core_ext/module/introspection.rb,
motion/core_ext/module/attribute_accessors.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.attr_internal_naming_formatObject

Returns the value of attribute attr_internal_naming_format


20
21
22
# File 'motion/core_ext/module/attr_internal.rb', line 20

def attr_internal_naming_format
  @attr_internal_naming_format
end

Instance Method Details

#alias_attribute(new_name, old_name) ⇒ Object

Allows you to make aliases for attributes, which includes getter, setter, and query methods.

class Content < ActiveRecord::Base
  # has a title attribute
end

class Email < Content
  alias_attribute :subject, :title
end

e = Email.find(1)
e.title    # => "Superstars"
e.subject  # => "Superstars"
e.subject? # => true
e.subject = "Megastars"
e.title    # => "Megastars"

62
63
64
65
66
67
68
# File 'motion/core_ext/module/aliasing.rb', line 62

def alias_attribute(new_name, old_name)
  module_exec do
    define_method(new_name) { self.send(old_name) }          # def subject; self.title; end
    define_method("#{new_name}?") { self.send("#{old_name}?") }        # def subject?; self.title?; end
    define_method("#{new_name}=") { |v| self.send("#{old_name}=", v) }  # def subject=(v); self.title = v; end
  end
end

#alias_method_chain(target, feature) {|aliased_target, punctuation| ... } ⇒ Object

Encapsulates the common pattern of:

alias_method :foo_without_feature, :foo
alias_method :foo, :foo_with_feature

With this, you simply do:

alias_method_chain :foo, :feature

And both aliases are set up for you.

Query and bang methods (foo?, foo!) keep the same punctuation:

alias_method_chain :foo?, :feature

is equivalent to

alias_method :foo_without_feature?, :foo?
alias_method :foo?, :foo_with_feature?

so you can safely chain foo, foo?, and foo! with the same feature.

Yields:

  • (aliased_target, punctuation)

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'motion/core_ext/module/aliasing.rb', line 23

def alias_method_chain(target, feature)
  # Strip out punctuation on predicates or bang methods since
  # e.g. target?_without_feature is not a valid method name.
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?

  with_method = "#{aliased_target}_with_#{feature}#{punctuation}"
  without_method = "#{aliased_target}_without_#{feature}#{punctuation}"

  alias_method without_method, target
  alias_method target, with_method

  case
  when public_method_defined?(without_method)
    public target
  when protected_method_defined?(without_method)
    protected target
  when private_method_defined?(without_method)
    private target
  end
end

#anonymous?Boolean

A module may or may not have a name.

module M; end
M.name # => "M"

m = Module.new
m.name # => nil

A module gets a name when it is first assigned to a constant. Either via the module or class keyword or by an explicit assignment:

m = Module.new # creates an anonymous module
M = m          # => m gets a name here as a side-effect
m.name         # => "M"

16
17
18
# File 'motion/core_ext/module/anonymous.rb', line 16

def anonymous?
  name.nil?
end

#attr_internal_accessor(*attrs) ⇒ Object Also known as: attr_internal

Declares an attribute reader and writer backed by an internally-named instance variable.


14
15
16
17
# File 'motion/core_ext/module/attr_internal.rb', line 14

def attr_internal_accessor(*attrs)
  attr_internal_reader(*attrs)
  attr_internal_writer(*attrs)
end

#attr_internal_reader(*attrs) ⇒ Object

Declares an attribute reader backed by an internally-named instance variable.


3
4
5
# File 'motion/core_ext/module/attr_internal.rb', line 3

def attr_internal_reader(*attrs)
  attrs.each {|attr_name| attr_internal_define(attr_name, :reader)}
end

#attr_internal_writer(*attrs) ⇒ Object

Declares an attribute writer backed by an internally-named instance variable.


8
9
10
# File 'motion/core_ext/module/attr_internal.rb', line 8

def attr_internal_writer(*attrs)
  attrs.each {|attr_name| attr_internal_define(attr_name, :writer)}
end

#delegate(*methods) ⇒ Object

Provides a delegate class method to easily expose contained objects' public methods as your own. Pass one or more methods (specified as symbols or strings) and the name of the target object via the :to option (also a symbol or string). At least one method and the :to option are required.

Delegation is particularly useful with Active Record associations:

class Greeter < ActiveRecord::Base
  def hello
    'hello'
  end

  def goodbye
    'goodbye'
  end
end

class Foo < ActiveRecord::Base
  belongs_to :greeter
  delegate :hello, to: :greeter
end

Foo.new.hello   # => "hello"
Foo.new.goodbye # => NoMethodError: undefined method `goodbye' for #<Foo:0x1af30c>

Multiple delegates to the same target are allowed:

class Foo < ActiveRecord::Base
  belongs_to :greeter
  delegate :hello, :goodbye, to: :greeter
end

Foo.new.goodbye # => "goodbye"

Methods can be delegated to instance variables, class variables, or constants by providing them as a symbols:

class Foo
  CONSTANT_ARRAY = [0,1,2,3]
  @@class_array  = [4,5,6,7]

  def initialize
    @instance_array = [8,9,10,11]
  end
  delegate :sum, to: :CONSTANT_ARRAY
  delegate :min, to: :@@class_array
  delegate :max, to: :@instance_array
end

Foo.new.sum # => 6
Foo.new.min # => 4
Foo.new.max # => 11

It's also possible to delegate a method to the class by using :class:

class Foo
  def self.hello
    "world"
  end

  delegate :hello, to: :class
end

Foo.new.hello # => "world"

Delegates can optionally be prefixed using the :prefix option. If the value is true, the delegate methods are prefixed with the name of the object being delegated to.

Person = Struct.new(:name, :address)

class Invoice < Struct.new(:client)
  delegate :name, :address, to: :client, prefix: true
end

john_doe = Person.new('John Doe', 'Vimmersvej 13')
invoice = Invoice.new(john_doe)
invoice.client_name    # => "John Doe"
invoice.client_address # => "Vimmersvej 13"

It is also possible to supply a custom prefix.

class Invoice < Struct.new(:client)
  delegate :name, :address, to: :client, prefix: :customer
end

invoice = Invoice.new(john_doe)
invoice.customer_name    # => 'John Doe'
invoice.customer_address # => 'Vimmersvej 13'

If the delegate object is nil an exception is raised, and that happens no matter whether nil responds to the delegated method. You can get a nil instead with the :allow_nil option.

class Foo
  attr_accessor :bar
  def initialize(bar = nil)
    @bar = bar
  end
  delegate :zoo, to: :bar
end

Foo.new.zoo   # raises NoMethodError exception (you called nil.zoo)

class Foo
  attr_accessor :bar
  def initialize(bar = nil)
    @bar = bar
  end
  delegate :zoo, to: :bar, allow_nil: true
end

Foo.new.zoo   # returns nil

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'motion/core_ext/module/delegation.rb', line 115

def delegate(*methods)
  options = methods.pop
  unless options.is_a?(Hash) && to = options[:to]
    raise ArgumentError, 'Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, to: :greeter).'
  end

  prefix, allow_nil = options.values_at(:prefix, :allow_nil)
  unguarded = !allow_nil

  if prefix == true && to =~ /^[^a-z_]/
    raise ArgumentError, 'Can only automatically set the delegation prefix when delegating to a method.'
  end

  method_prefix = \
    if prefix
      "#{prefix == true ? to : prefix}_"
    else
      ''
    end

  reference, *hierarchy = to.to_s.split('.')
  entry = resolver =
    case reference
    when 'self'
      ->(_self) { _self }
    when /^@@/
      ->(_self) { _self.class.class_variable_get(reference) }
    when /^@/
      ->(_self) { _self.instance_variable_get(reference) }
    when /^[A-Z]/
      ->(_self) { if reference.to_s =~ /::/ then reference.constantize else _self.class.const_get(reference) end }
    else
      ->(_self) { _self.send(reference) }
    end
  resolver = ->(_self) { hierarchy.reduce(entry.call(_self)) { |obj, method| obj.public_send(method) } } unless hierarchy.empty?

  methods.each do |method|
    module_exec do
      # def customer_name(*args, &block)
      #   begin
      #     if unguarded || client || client.respond_to?(:name)
      #       client.name(*args, &block)
      #     end
      #   rescue client.nil? && NoMethodError
      #     raise "..."
      #   end
      # end
      define_method("#{method_prefix}#{method}") do |*args, &block|
        target = resolver.call(self)
        if unguarded || target || target.respond_to?(method)
          begin
            target.public_send(method, *args, &block)
          rescue target.nil? && NoMethodError # only rescue NoMethodError when target is nil
            raise "#{self}##{method_prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: #{self.inspect}"
          end
        end
      end
    end
  end
end

#local_constantsObject

:nodoc:


57
58
59
# File 'motion/core_ext/module/introspection.rb', line 57

def local_constants #:nodoc:
  constants(false)
end

#mattr_accessor(*syms) ⇒ Object

Extends the module object with module and instance accessors for class attributes, just like the native attr* accessors for instance attributes.

module AppConfiguration
  mattr_accessor :google_api_key

  self.google_api_key = "123456789"
end

AppConfiguration.google_api_key # => "123456789"
AppConfiguration.google_api_key = "overriding the api key!"
AppConfiguration.google_api_key # => "overriding the api key!"

60
61
62
63
# File 'motion/core_ext/module/attribute_accessors.rb', line 60

def mattr_accessor(*syms)
  mattr_reader(*syms)
  mattr_writer(*syms)
end

#mattr_reader(*syms) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'motion/core_ext/module/attribute_accessors.rb', line 2

def mattr_reader(*syms)
  receiver = self
  options = syms.extract_options!
  syms.each do |sym|
    raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
    class_exec do
      unless class_variable_defined?("@@#{sym}")
        class_variable_set("@@#{sym}", nil)
      end

      define_singleton_method sym do
        class_variable_get("@@#{sym}")
      end
    end

    unless options[:instance_reader] == false || options[:instance_accessor] == false
      class_exec do
        define_method sym do
          receiver.class_variable_get("@@#{sym}")
        end
      end
    end
  end
end

#mattr_writer(*syms) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'motion/core_ext/module/attribute_accessors.rb', line 27

def mattr_writer(*syms)
  receiver = self
  options = syms.extract_options!
  syms.each do |sym|
    raise NameError.new('invalid attribute name') unless sym =~ /^[_A-Za-z]\w*$/
    class_exec do
      define_singleton_method "#{sym}=" do |obj|
        class_variable_set("@@#{sym}", obj)
      end
    end

    unless options[:instance_writer] == false || options[:instance_accessor] == false
      class_exec do
        define_method "#{sym}=" do |obj|
          receiver.class_variable_set("@@#{sym}", obj)
        end
      end
    end
  end
end

#parentObject

Returns the module which contains this one according to its name.

module M
  module N
  end
end
X = M::N

M::N.parent # => M
X.parent    # => M

The parent of top-level and anonymous modules is Object.

M.parent          # => Object
Module.new.parent # => Object

28
29
30
# File 'motion/core_ext/module/introspection.rb', line 28

def parent
  parent_name ? parent_name.constantize : Object
end

#parent_nameObject

Returns the name of the module containing this one.

M::N.parent_name # => "M"

5
6
7
8
9
10
11
# File 'motion/core_ext/module/introspection.rb', line 5

def parent_name
  if defined? @parent_name
    @parent_name
  else
    @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
  end
end

#parentsObject

Returns all the parents of this module according to its name, ordered from nested outwards. The receiver is not contained within the result.

module M
  module N
  end
end
X = M::N

M.parents    # => [Object]
M::N.parents # => [M, Object]
X.parents    # => [M, Object]

44
45
46
47
48
49
50
51
52
53
54
55
# File 'motion/core_ext/module/introspection.rb', line 44

def parents
  parents = []
  if parent_name
    parts = parent_name.split('::')
    until parts.empty?
      parents << (parts * '::').constantize
      parts.pop
    end
  end
  parents << Object unless parents.include? Object
  parents
end

#reachable?Boolean

:nodoc:


2
3
4
# File 'motion/core_ext/module/reachable.rb', line 2

def reachable? #:nodoc:
  !anonymous? && name.safe_constantize.equal?(self)
end

#redefine_method(method, &block) ⇒ Object


8
9
10
11
# File 'motion/core_ext/module/remove_method.rb', line 8

def redefine_method(method, &block)
  remove_possible_method(method)
  define_method(method, &block)
end

#remove_possible_method(method) ⇒ Object


2
3
4
5
6
# File 'motion/core_ext/module/remove_method.rb', line 2

def remove_possible_method(method)
  if method_defined?(method) || private_method_defined?(method)
    undef_method(method)
  end
end