Module: PleaseRun::Configurable
- Included in:
- Platform::Base, User::Base
- Defined in:
- lib/pleaserun/configurable.rb
Overview
A mixin class that provides ‘attribute’ to a class. The main use for such attributes is to provide validation for mutators.
Example:
class Person
include PleaseRun::Configurable
attribute :greeting, "A simple greeting" do |greeting|
# do any validation here.
raise "Greeting must be a string!" unless greeting.is_a?(String)
end
end
person = Person.new
person.greeting = 1234 # Fails!
person.greeting = "Hello, world!"
puts person.greeting
# "Hello, world!"
Defined Under Namespace
Modules: ClassMixin Classes: ConfigurationError, Facet, FacetDSL, ValidationError
Class Method Summary collapse
Instance Method Summary collapse
- #configurable_setup ⇒ Object
-
#validate ⇒ Object
def self.included.
Class Method Details
.included(klass) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/pleaserun/configurable.rb', line 28 def self.included(klass) klass.extend(ClassMixin) m = respond_to?(:initialize) ? method(:initialize) : nil define_method(:initialize) do |*args, &block| m.call(*args, &block) if m configurable_setup end end |
Instance Method Details
#configurable_setup ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/pleaserun/configurable.rb', line 45 def configurable_setup @attributes = {} self.class.ancestors.each do |ancestor| next unless ancestor.include?(PleaseRun::Configurable) ancestor.attributes.each do |facet| @attributes[facet.name] = facet.clone end end end |
#validate ⇒ Object
def self.included
38 39 40 41 42 43 |
# File 'lib/pleaserun/configurable.rb', line 38 def validate # validate the value of each attribute self.class.attributes.each do |attribute| attribute.validate(send(attribute.name)) end end |