Class: PPT::Presenters::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-orm/presenters.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#instanceObject

Returns the value of attribute instance.



22
23
24
# File 'lib/simple-orm/presenters.rb', line 22

def instance
  @instance
end

#nameObject (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

#privateObject

DSL



30
31
32
33
# File 'lib/simple-orm/presenters.rb', line 30

def private
  @private = true
  self
end

#private?Boolean

API

Returns:

  • (Boolean)


69
70
71
# File 'lib/simple-orm/presenters.rb', line 69

def private?
  @private
end

#requiredObject



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(message, &block)
  self.validators << Validator.new(message, &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