Class: PPT::Presenters::Attribute
- Inherits:
-
Object
- Object
- PPT::Presenters::Attribute
- Defined in:
- lib/simple-orm/presenters.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #default(value = nil, &block) ⇒ Object
- #get(stage = nil) ⇒ Object
-
#initialize(name) ⇒ Attribute
constructor
A new instance of Attribute.
- #on_create(value = nil, &block) ⇒ Object
- #on_update(value = nil, &block) ⇒ Object
-
#private ⇒ Object
DSL.
-
#private? ⇒ Boolean
API.
- #required ⇒ Object
- #run_hook(name) ⇒ Object
- #set(value) ⇒ Object
- #type(type) ⇒ Object
- #validate(message, &block) ⇒ Object
- #validate!(stage = nil) ⇒ Object
Constructor Details
#initialize(name) ⇒ Attribute
Returns a new instance of Attribute.
24 25 26 27 |
# File 'lib/simple-orm/presenters.rb', line 24 def initialize(name) @name = name @validators, @hooks = Array.new, Hash.new end |
Instance Attribute Details
#instance ⇒ Object
Returns the value of attribute instance.
22 23 24 |
# File 'lib/simple-orm/presenters.rb', line 22 def instance @instance end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
23 24 25 |
# File 'lib/simple-orm/presenters.rb', line 23 def name @name end |
Instance Method Details
#default(value = nil, &block) ⇒ Object
53 54 55 56 |
# File 'lib/simple-orm/presenters.rb', line 53 def default(value = nil, &block) @hooks[:default] = value ? Proc.new { value } : block self end |
#get(stage = nil) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/simple-orm/presenters.rb', line 85 def get(stage = nil) if stage.nil? @value ||= self.run_hook(:default) elsif stage == :create @value ||= self.run_hook(:on_create) elsif stage == :update @value ||= self.run_hook(:on_update) else raise ArgumentError.new("Attribute#get takes an optional argument which can be either :create or :update.") end end |
#on_create(value = nil, &block) ⇒ Object
58 59 60 61 |
# File 'lib/simple-orm/presenters.rb', line 58 def on_create(value = nil, &block) @hooks[:on_create] = value ? Proc.new { value } : block self end |
#on_update(value = nil, &block) ⇒ Object
63 64 65 66 |
# File 'lib/simple-orm/presenters.rb', line 63 def on_update(value = nil, &block) @hooks[:on_update] = value ? Proc.new { value } : block self end |
#private ⇒ Object
DSL
30 31 32 33 |
# File 'lib/simple-orm/presenters.rb', line 30 def private @private = true self end |
#private? ⇒ Boolean
API
69 70 71 |
# File 'lib/simple-orm/presenters.rb', line 69 def private? @private end |
#required ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/simple-orm/presenters.rb', line 35 def required @validators << Validator.new('is required') do |value| value != nil && ! value.empty? end self end |
#run_hook(name) ⇒ Object
73 74 75 |
# File 'lib/simple-orm/presenters.rb', line 73 def run_hook(name) @hooks[name] && @instance.instance_eval(&@hooks[name]) end |
#set(value) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/simple-orm/presenters.rb', line 77 def set(value) if self.private? raise "Attribute #{@name} is private!" end @value = value end |
#type(type) ⇒ Object
48 49 50 51 |
# File 'lib/simple-orm/presenters.rb', line 48 def type(type) @type = type self end |
#validate(message, &block) ⇒ Object
43 44 45 46 |
# File 'lib/simple-orm/presenters.rb', line 43 def validate(, &block) self.validators << Validator.new(, &block) self end |
#validate!(stage = nil) ⇒ Object
97 98 99 100 101 |
# File 'lib/simple-orm/presenters.rb', line 97 def validate!(stage = nil) @validators.each do |validator| validator.validate!(self.name, self.get(stage)) end end |