Class: Class

Inherits:
Object show all
Defined in:
lib/trellis/utils.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attr_array(plural_array_name_sym, options = {}) ⇒ Object



79
80
81
82
83
84
# File 'lib/trellis/utils.rb', line 79

def attr_array(plural_array_name_sym, options={}) 
  create_accessor = options[:create_accessor].nil? ? true : options[:create_accessor]
  plural_array_name_sym = plural_array_name_sym.to_s #convert to string if it is a symbol
  instance_variable_set("@#{plural_array_name_sym}".to_sym, Array.new)
  meta_def(plural_array_name_sym) { instance_variable_get("@#{plural_array_name_sym}".to_sym) } if create_accessor
end

#class_attr_accessor(*syms) ⇒ Object



55
56
57
58
59
# File 'lib/trellis/utils.rb', line 55

def class_attr_accessor(*syms)
  syms.flatten.each do |sym|
    metaclass.instance_eval { attr_accessor(sym) }
  end
end

#class_attr_reader(*syms) ⇒ Object



67
68
69
70
71
# File 'lib/trellis/utils.rb', line 67

def class_attr_reader(*syms)
  syms.flatten.each do |sym|
    metaclass.instance_eval { attr_reader(sym) }
  end
end

#class_attr_writer(*syms) ⇒ Object



73
74
75
76
77
# File 'lib/trellis/utils.rb', line 73

def class_attr_writer(*syms)
  syms.flatten.each do |sym|
    metaclass.instance_eval { attr_writer(sym) }
  end
end

#class_to_symObject



42
43
44
# File 'lib/trellis/utils.rb', line 42

def class_to_sym
  underscore_class_name.to_sym
end

#create_child(class_name, mod = Object, register = true, &block) ⇒ Object



86
87
88
89
90
91
# File 'lib/trellis/utils.rb', line 86

def create_child(class_name, mod = Object, register = true, &block)
  klass = Class.new self
  klass.class_eval &block if block_given?
  mod.const_set class_name, klass if register
  klass
end

#instance_attr_accessor(*syms) ⇒ Object



61
62
63
64
65
# File 'lib/trellis/utils.rb', line 61

def instance_attr_accessor(*syms)
  syms.flatten.each do |sym|
    instance_eval { attr_accessor(sym) }
  end
end

#underscore_class_nameObject



46
47
48
49
50
51
52
53
# File 'lib/trellis/utils.rb', line 46

def underscore_class_name
  value = (name.empty? || name =~ /<class:(.*)/) ? self : name
  value.to_s.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase.split('/').last
end