Module: Metafy::Base
- Defined in:
- lib/metafy/base.rb
Class Method Summary collapse
Instance Method Summary collapse
- #define_accessor_for_attribute(att) ⇒ Object
- #define_accessors_for_attributes ⇒ Object
- #get_metafied_attribute(att) ⇒ Object
- #has_metafied_attribute?(attribute) ⇒ Boolean
- #initialize(attributes = nil) ⇒ Object
- #populate_metafied_attributes ⇒ Object
- #read_metafied_attribute(attribute) ⇒ Object
- #set_metafied_attribute(att, value = nil) ⇒ Object
- #singleton_class ⇒ Object
-
#update_attributes(attributes) ⇒ Object
overrides update_attributes to take meta attributes into account.
- #write_metafied_attribute(meta_attr) ⇒ Object
- #write_metafied_attributes ⇒ Object
Class Method Details
.included(object) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/metafy/base.rb', line 67 def self.included object super object.after_initialize :populate_metafied_attributes object.after_find :populate_metafied_attributes object.after_save :write_metafied_attributes end |
Instance Method Details
#define_accessor_for_attribute(att) ⇒ Object
55 56 57 |
# File 'lib/metafy/base.rb', line 55 def define_accessor_for_attribute(att) singleton_class.send(:attr_accessor, att.to_sym) end |
#define_accessors_for_attributes ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/metafy/base.rb', line 59 def define_accessors_for_attributes self..each do | | unless respond_to?(.to_s) define_accessor_for_attribute() end end end |
#get_metafied_attribute(att) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/metafy/base.rb', line 82 def (att) unless respond_to?(att.to_s) define_accessor_for_attribute(att) end send(att.to_s) end |
#has_metafied_attribute?(attribute) ⇒ Boolean
15 16 17 |
# File 'lib/metafy/base.rb', line 15 def (attribute) return self..include?(attribute.to_sym) end |
#initialize(attributes = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/metafy/base.rb', line 3 def initialize(attributes = nil) unless attributes.nil? super(attributes.except(*self.)) define_accessors_for_attributes attributes.each do |n,v| (n,v) if (n) end else super(nil) end end |
#populate_metafied_attributes ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/metafy/base.rb', line 40 def define_accessors_for_attributes attributes = Mattr.(self.class.name, self.id) attributes.each do | | ( ..to_sym, ..to_s ) end end |
#read_metafied_attribute(attribute) ⇒ Object
19 20 21 |
# File 'lib/metafy/base.rb', line 19 def (attribute) (attribute.to_sym) ? Mattr.value(self.class.name, self.id, attribute.to_s) : nil end |
#set_metafied_attribute(att, value = nil) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/metafy/base.rb', line 74 def (att, value = nil) unless respond_to?(att.to_s + '=') define_accessor_for_attribute(att) end send(att.to_s + '=', value) end |
#singleton_class ⇒ Object
49 50 51 52 53 |
# File 'lib/metafy/base.rb', line 49 def singleton_class class << self self end end |
#update_attributes(attributes) ⇒ Object
overrides update_attributes to take meta attributes into account
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/metafy/base.rb', line 91 def update_attributes(attributes) attributes.each do |n,v| if (n.to_sym) ( n.to_sym, v ) ( n.to_sym ) end end attributes.nil? ? super(nil) : super(attributes.except(*self.)) end |
#write_metafied_attribute(meta_attr) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/metafy/base.rb', line 29 def ( ) = .to_sym = ( ) = ( ) if( != ) Mattr. = [ self.class.name, self.id, .to_s, ( ) ] end end |
#write_metafied_attributes ⇒ Object
23 24 25 26 27 |
# File 'lib/metafy/base.rb', line 23 def self..each do | | ( ) end end |