Module: DuckMap::Attributes::ClassMethods
- Defined in:
- lib/duck_map/attributes.rb
Instance Method Summary collapse
- #is_sitemap_attributes_defined? ⇒ TrueClass, FalseClass
-
#sitemap_attributes ⇒ Hash
Returns the entire attributes Hash that has been defined for an object.
Instance Method Details
#is_sitemap_attributes_defined? ⇒ TrueClass, FalseClass
37 38 39 40 41 42 43 44 |
# File 'lib/duck_map/attributes.rb', line 37 def is_sitemap_attributes_defined? if self.sitemap_attributes_defined.nil? self.sitemap_attributes_defined = false end return self.sitemap_attributes_defined end |
#sitemap_attributes ⇒ Hash
Returns the entire attributes Hash that has been defined for an object. The actual Hash is maintained via an accessor method named: sitemap_attributes_hash. sitemap_attributes is actually a wrapper method for sitemap_attributes_hash accessor method.
There are actually two definitions of sitemap_attributes_hash accessor method. The purpose of two definitions is to allow common code contained in DuckMap::Attributes to be included in the Config and all controller classes. The code works by referencing sitemap_attributes_hash accessor method, however, the actual variable reference is different depending on the object that is referring to it.
When DuckMap::Attributes module is included in Config, then, self.sitemap_attributes_hash is actually referencing the class level method defined on Config.
When DuckMap::Attributes module is included in all controller classes (it is by default), then, self.sitemap_attributes_hash is actually referencing the class level method defined by InheritableClassAttributes via class_attribute method. This means that the actual variable that will contain the Hash value never gets initialized. So, self.sitemap_attributes_hash will ALWAYS be uninitialized during the first access from within a controller and will ALWAYS copy values from Config.
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 |
# File 'lib/duck_map/attributes.rb', line 63 def sitemap_attributes # check the current state of self.sitemap_attributes_hash. If it is a Hash, then, it is considered initialized. # otherwise, a new Hash is populated and assigned to self.sitemap_attributes_hash and a reference is returned. # # when this module is included in DuckMap::Config self.sitemap_attributes_hash is actually referencing the class # level method defined on DuckMap::Config. # # When this module is included in all controller classes self.sitemap_attributes_hash is actually referencing the class # level method defined on InheritableClassAttributes which never gets initialized. So, self.sitemap_attributes_hash # will NEVER be a Hash on the first access from within a controller and will ALWAYS copy values from {DuckMap::Config} unless self.sitemap_attributes_hash.kind_of?(Hash) # I actually have code to do a deep clone of a Hash, however, I can't release it right now. # I will in a later release. For now, I will commit another sin. self.sitemap_attributes_hash = {} source = DuckMap::Config.sitemap_attributes_hash source.each do |item| self.sitemap_attributes_hash[item.first] = {}.merge(item.last) self.sitemap_attributes_hash[item.first][:handler] = {}.merge(item.last[:handler]) end end return self.sitemap_attributes_hash end |