Class: Module

Inherits:
Object show all
Defined in:
lib/poolparty/core/module.rb

Overview

Module overloads

Instance Method Summary collapse

Instance Method Details

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

Gives us alias_method_chain from rails

Yields:

  • (aliased_target, punctuation)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/poolparty/core/module.rb', line 4

def alias_method_chain(target, feature)
  aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
  yield(aliased_target, punctuation) if block_given?

  with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{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

#attr_accessor_with_default(*syms, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/poolparty/core/module.rb', line 22

def attr_accessor_with_default( *syms, &block )
  raise 'Default value in block required' unless block
  syms.each do | sym |
    module_eval do
      attr_writer( sym )
      define_method( sym ) do | |
        class << self; self; end.class_eval do
          attr_reader( sym )
        end
        instance_variables.include?("@#{sym}") ? instance_variable_get( "@#{sym}" ) : instance_variable_set( "@#{sym}", block.call )
      end
    end
  end
  nil
end

#instance_variables_from_hash(h = {}) ⇒ Object



37
38
39
# File 'lib/poolparty/core/module.rb', line 37

def instance_variables_from_hash(h={})
  h.each {|k,v| instance_eval "@#{k} = #{v}"}
end