Class: Module

Inherits:
Object show all
Defined in:
lib/sugar-high/alias.rb,
lib/sugar-high/module.rb,
lib/sugar-high/delegate.rb,
lib/sugar-high/includes.rb,
lib/sugar-high/rails/concerns.rb

Instance Method Summary collapse

Instance Method Details

#alias_for(original, *aliases) ⇒ Object Also known as: aliases_for



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sugar-high/alias.rb', line 70

def alias_for(original, *aliases)
  pluralize = last_option(aliases)[:pluralize]
  singularize = last_option(aliases)[:singularize]

  class_eval "alias_method :#{original.to_s.singularize}, :#{original}" if singularize
  class_eval "alias_method :#{original.to_s.pluralize}, :#{original}" if pluralize

  aliases.flatten.select_labels.each do |alias_meth|
    class_eval "alias_method :#{alias_meth}, :#{original}"
    class_eval "alias_method :#{alias_meth.to_s.pluralize}, :#{original}" if pluralize
    class_eval "alias_method :#{alias_meth.to_s.singularize}, :#{original}" if singularize
  end
end

#alias_hash(hash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sugar-high/alias.rb', line 58

def alias_hash(hash)
  pluralize = hash.delete(:pluralize)
  singularize = hash.delete(:singularize)
  # option = :pluralize => pluralize, :singularize => singularize

  hash.each_pair do |original, alias_meth|
    alias_for original, alias_meth
    alias_for original.to_s.singularize, alias_meth.to_s.singularize, :singularize => true if singularize
    alias_for original.to_s.pluralize, alias_meth.to_s.pluralize, :pluralize => true if pluralize
  end
end

#alias_methods(name, original, aliases, config_options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sugar-high/alias.rb', line 46

def alias_methods name, original, aliases, config_options
  aliases.each do |alias_name|
    new_alias     = make_name(name, alias_name.to_s, config_options)
    original_name = make_name(name, original.to_s, config_options)
    begin
      alias_method new_alias, original_name
    rescue
      raise ArgumentError, "Error creating alias for ##{original_name} with ##{new_alias}"
    end
  end
end

#concerned_with(*concerns) ⇒ Object



2
3
4
5
6
7
8
9
# File 'lib/sugar-high/rails/concerns.rb', line 2

def concerned_with(*concerns)
  options = concerns.extract_options!
  concerns.flatten.each do |concern|
    next if concern.blank?
    require_concern name, concern
  end
  shared_concerns([options[:shared]].flatten.compact)
end

#delegate(*methods) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sugar-high/delegate.rb', line 3

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

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

  prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" || ''

  file, line = caller.first.split(':', 2)
  line = line.to_i

  methods.each do |method|
    on_nil =
      if options[:allow_nil]
        'return'
      else
        %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
      end
    # def customer_name(*args, &block)
    module_eval %{
      def #{prefix}#{method}(*args, &block)
        #{to}.__send__(#{method.inspect}, *args, &block)
      rescue NoMethodError
        if #{to}.nil?
          #{on_nil}
        else
          raise
        end
      end
    }
  end
end

#include_concerns(*concerns) ⇒ Object Also known as: include_concern



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sugar-high/rails/concerns.rb', line 11

def include_concerns(*concerns)
  options = concerns.extract_options!
  concerns.flatten.each do |concern|
    next if concern.blank?
    require_concern name, concern
    concern_ns = [name, concern.to_s.camelize].join('::')
    self.send :include, concern_ns.constantize
    begin
      self.extend [concern_ns, 'ClassMethods'].join('::').constantize
    rescue
    end
  end
  include_shared_concerns([options[:shared]].flatten.compact)
end

#include_shared_concerns(*concerns) ⇒ Object Also known as: include_shared_concern



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sugar-high/rails/concerns.rb', line 33

def include_shared_concerns(*concerns)
  concerns.flatten.each do |concern|
    next if concern.blank?
    require_shared concern
    concern_ns = concern.to_s.camelize

    self.send :include, shared_const(concern_ns)

    begin
      self.extend [concern_ns, 'ClassMethods'].join('::').constantize
    rescue
    end
  end
end

#includes_and_extends(*module_names) ⇒ Object Also known as: extends_and_includes



26
27
28
29
# File 'lib/sugar-high/includes.rb', line 26

def includes_and_extends *module_names
  includes module_names
  extends module_names
end

#includes_and_extends_from(module_name, *sub_modules) ⇒ Object Also known as: extends_and_includes_from



31
32
33
34
# File 'lib/sugar-high/includes.rb', line 31

def includes_and_extends_from module_name, *sub_modules
  includes module_name, *sub_modules
  extends module_name, *sub_modules
end

#last_nameObject Also known as: demodulize



4
5
6
# File 'lib/sugar-high/module.rb', line 4

def last_name
  name.demodulize
end

#multi_alias(*args) ⇒ Object

multi_alias name, :create => :new, :insert_into => [:inject_into, :update], :read => :X_content

:options => :after

create_xxx becomes new_xxx insert_into_xxx becomes inject_into_xxx and update_xxx read_xxx becomes xxx_content (overriding default :after action to insert at the X)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sugar-high/alias.rb', line 16

def multi_alias *args
  name = case args.first
  when Symbol, String
    args.first.to_s
  when Hash
    # default is :after
    args.first[:_before_] ? :before : :after
  end

  if name.kind_of? Symbol
    config_options = name
    options = args.first
    name = options[:"_#{name}_"]
  else
    options = args[1]
  end

  raise ArgumentError, "Name of method pattern to alias not specified. Please pass name as either first argument or as :_before_ or :_after_ option" if !name

  options.delete(:_after_)
  options.delete(:_before_)
  direction = options.delete(:_direction_)

  options = options.hash_revert if direction == :reverse

  options.each_pair do |original, aliases|
    alias_methods name.to_sym, original, [aliases].flatten, config_options
  end
end

#shared_concerns(*concerns) ⇒ Object Also known as: shared_concern



26
27
28
29
30
31
# File 'lib/sugar-high/rails/concerns.rb', line 26

def shared_concerns(*concerns)
  concerns.flatten.each do |concern|
    next if concern.blank?
    require_shared concern
  end
end