| 1 |
module Zena |
|---|
| 2 |
module ZafuExtension |
|---|
| 3 |
module UseZafu |
|---|
| 4 |
|
|---|
| 5 |
def self.included(base) |
|---|
| 6 |
|
|---|
| 7 |
base.extend Zena::ZafuExtension::TriggerClassMethod |
|---|
| 8 |
end |
|---|
| 9 |
end |
|---|
| 10 |
|
|---|
| 11 |
module ClassMethods |
|---|
| 12 |
|
|---|
| 13 |
end |
|---|
| 14 |
|
|---|
| 15 |
module TriggerClassMethod |
|---|
| 16 |
def use_zafu |
|---|
| 17 |
class_eval <<-END |
|---|
| 18 |
include Zena::ZafuExtension::InstanceMethods |
|---|
| 19 |
@@_zafu_readable ||= {} |
|---|
| 20 |
@@_safe_attribute ||= {} |
|---|
| 21 |
@@_zafu_context ||= {} |
|---|
| 22 |
@@_zafu_readable_attributes ||= {} |
|---|
| 23 |
@@_safe_attribute_list ||= {} |
|---|
| 24 |
@@_zafu_known_contexts ||= {} |
|---|
| 25 |
|
|---|
| 26 |
def self.zafu_readable(*list) |
|---|
| 27 |
@@_zafu_readable[self] ||= [] |
|---|
| 28 |
@@_zafu_readable[self] = (@@_zafu_readable[self] + list.map{|l| l.to_s}).uniq |
|---|
| 29 |
end |
|---|
| 30 |
|
|---|
| 31 |
def self.safe_attribute(*list) |
|---|
| 32 |
@@_safe_attribute[self] ||= [] |
|---|
| 33 |
@@_safe_attribute[self] = (@@_safe_attribute[self] + list.map{|l| l.to_s}).uniq |
|---|
| 34 |
end |
|---|
| 35 |
|
|---|
| 36 |
def self.zafu_context(hash) |
|---|
| 37 |
@@_zafu_context[self] ||= {} |
|---|
| 38 |
@@_zafu_context[self].merge!(hash.stringify_keys) |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def self.zafu_readable_attributes |
|---|
| 42 |
@@_zafu_readable_attributes[self] ||= if superclass == ActiveRecord::Base |
|---|
| 43 |
@@_zafu_readable[self] || [] |
|---|
| 44 |
else |
|---|
| 45 |
(superclass.zafu_readable_attributes + (@@_zafu_readable[self] || [])).uniq.sort |
|---|
| 46 |
end |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
def self.safe_attribute_list |
|---|
| 50 |
@@_safe_attribute_list[self] ||= if superclass == ActiveRecord::Base |
|---|
| 51 |
@@_safe_attribute[self] || [] |
|---|
| 52 |
else |
|---|
| 53 |
(superclass.safe_attribute_list + (@@_safe_attribute[self] || [])).uniq.sort |
|---|
| 54 |
end |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
def self.zafu_known_contexts |
|---|
| 58 |
@@_zafu_known_contexts[self] ||= begin |
|---|
| 59 |
res = {} |
|---|
| 60 |
if superclass == ActiveRecord::Base |
|---|
| 61 |
@@_zafu_context[self] || {} |
|---|
| 62 |
else |
|---|
| 63 |
superclass.zafu_known_contexts.merge(@@_zafu_context[self] || {}) |
|---|
| 64 |
end.each do |k,v| |
|---|
| 65 |
if v.kind_of?(Hash) |
|---|
| 66 |
res[k] = v.merge(:node_class => parse_class(v[:node_class])) |
|---|
| 67 |
else |
|---|
| 68 |
res[k] = {:node_class => parse_class(v)} |
|---|
| 69 |
end |
|---|
| 70 |
end |
|---|
| 71 |
res |
|---|
| 72 |
end |
|---|
| 73 |
end |
|---|
| 74 |
|
|---|
| 75 |
def self.parse_class(klass) |
|---|
| 76 |
if klass.kind_of?(Array) |
|---|
| 77 |
if klass[0].kind_of?(String) |
|---|
| 78 |
[Module::const_get(klass[0])] |
|---|
| 79 |
else |
|---|
| 80 |
klass |
|---|
| 81 |
end |
|---|
| 82 |
else |
|---|
| 83 |
if klass.kind_of?(String) |
|---|
| 84 |
Module::const_get(klass) |
|---|
| 85 |
else |
|---|
| 86 |
klass |
|---|
| 87 |
end |
|---|
| 88 |
end |
|---|
| 89 |
end |
|---|
| 90 |
|
|---|
| 91 |
def self.safe_attribute?(sym) |
|---|
| 92 |
column_names.include?(sym) || zafu_readable?(sym) || safe_attribute_list.include?(sym.to_s) |
|---|
| 93 |
end |
|---|
| 94 |
|
|---|
| 95 |
def self.zafu_readable?(sym) |
|---|
| 96 |
if sym.to_s =~ /(.*)_zips?$/ |
|---|
| 97 |
return true if self.ancestors.include?(Node) && RelationProxy.find_by_role($1.singularize) |
|---|
| 98 |
end |
|---|
| 99 |
self.zafu_readable_attributes.include?(sym.to_s) |
|---|
| 100 |
end |
|---|
| 101 |
END |
|---|
| 102 |
end |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
module InstanceMethods |
|---|
| 106 |
def zafu_read(sym) |
|---|
| 107 |
return "'#{sym}' not readable" unless self.class.zafu_readable?(sym) |
|---|
| 108 |
self.send(sym) |
|---|
| 109 |
end |
|---|
| 110 |
end |
|---|
| 111 |
end |
|---|
| 112 |
end |
|---|
| 113 |
|
|---|
| 114 |
ActiveRecord::Base.send :include, Zena::ZafuExtension::UseZafu |
|---|
| 115 |
ActiveRecord::Base.send :use_zafu |
|---|