1 2 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
begin
require 'active_support/core_ext/class/attribute'
rescue LoadError
# A dumbed down version of ActiveSupport's
# Class#class_attribute helper.
class Class
def class_attribute(*attrs)
instance_writer = true
attrs.each do |name|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{name}() nil end
def self.#{name}?() !!#{name} end
def self.#{name}=(val)
singleton_class.class_eval do
define_method(:#{name}) { val }
end
if singleton_class?
class_eval do
def #{name}
defined?(@#{name}) ? @#{name} : singleton_class.#{name}
end
end
end
val
end
def #{name}
defined?(@#{name}) ? @#{name} : self.class.#{name}
end
def #{name}?
!!#{name}
end
RUBY
attr_writer name if instance_writer
end
end
private
def singleton_class?
ancestors.first != self
end
end
end
begin
require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/hash/deep_merge'
rescue LoadError
class Hash
def stringify_keys
keys.each do |key|
self[key.to_s] = delete(key)
end
self
end if !{}.respond_to?(:stringify_keys)
def symbolize_keys
keys.each do |key|
self[(key.to_sym rescue key) || key] = delete(key)
end
self
end if !{}.respond_to?(:symbolize_keys)
def deep_merge(other_hash, &block)
dup.deep_merge!(other_hash, &block)
end if !{}.respond_to?(:deep_merge)
def deep_merge!(other_hash, &block)
other_hash.each_pair do |k,v|
tv = self[k]
if tv.is_a?(Hash) && v.is_a?(Hash)
self[k] = tv.deep_merge(v, &block)
else
self[k] = block && tv ? block.call(k, tv, v) : v
end
end
self
end if !{}.respond_to?(:deep_merge!)
end
end
begin
require 'active_support/core_ext/string/inflections'
rescue LoadError
class String
def constantize
names = self.split('::')
names.shift if names.empty? || names.first.empty?
constant = Object
names.each do |name|
constant = constant.const_defined?(name) ? constant.const_get(name) : constant.const_missing(name)
end
constant
end
end if !"".respond_to?(:constantize)
end
|