Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. module Dependencies
  2. # Ensure dependencies from the superclass are inherited by the subclass.
  3. def inherited(subclass)
  4. if superclass.respond_to?(:dependencies)
  5. subclass.dependencies.concat dependencies
  6. end
  7.  
  8. super
  9. end
  10.  
  11. # Stores a list of dependency attributes for use by the initializer.
  12. def dependencies
  13. @dependencies ||= []
  14. end
  15.  
  16. # Defines a new dependency attribute with a default value.
  17. def dependency(attribute, default)
  18. # Define a getter that lazily sets a default value.
  19. define_method attribute do
  20. if instance_variable_defined?(:"@#{attribute}")
  21. instance_variable_get(:"@#{attribute}")
  22. else
  23. instance_variable_set(:"@#{attribute}", default.call)
  24. end
  25. end
  26.  
  27. # Define a setter that lazily sets a default value.
  28. attr_writer attribute
  29.  
  30. # Mark the getter and setter as protected.
  31. protected attribute, "#{attribute}="
  32.  
  33. # Add the attribute to the list of dependencies.
  34. dependencies << attribute
  35. end
  36. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement