Class: Nemo::MetaObject::Attribute
- Inherits:
-
Object
- Object
- Nemo::MetaObject::Attribute
show all
- Extended by:
- Util::Accessors
- Defined in:
- lib/nemo/metaobject/attributes.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
bool_accessor, call_accessor, proc_accessor
Constructor Details
#initialize(symbol) ⇒ Attribute
Returns a new instance of Attribute.
10
11
12
13
14
15
16
17
18
|
# File 'lib/nemo/metaobject/attributes.rb', line 10
def initialize(symbol)
@format_with = Proc.new { |value| value.nil? ? value : value.to_s }
@hidden = false
@label = String.new
@multiple_attribute = false
@read_only = false
@symbol= symbol
@validation_rules = Array.new
end
|
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
5
6
7
|
# File 'lib/nemo/metaobject/attributes.rb', line 5
def cache
@cache
end
|
Returns the value of attribute metaobject.
5
6
7
|
# File 'lib/nemo/metaobject/attributes.rb', line 5
def metaobject
@metaobject
end
|
#symbol ⇒ Object
Returns the value of attribute symbol.
5
6
7
|
# File 'lib/nemo/metaobject/attributes.rb', line 5
def symbol
@symbol
end
|
#validation_rules ⇒ Object
Returns the value of attribute validation_rules.
5
6
7
|
# File 'lib/nemo/metaobject/attributes.rb', line 5
def validation_rules
@validation_rules
end
|
Instance Method Details
#accept(visitor) ⇒ Object
28
29
30
|
# File 'lib/nemo/metaobject/attributes.rb', line 28
def accept(visitor)
raise NotImplementedError, 'subclass responsibility'
end
|
#add_required_rule ⇒ Object
57
58
59
|
# File 'lib/nemo/metaobject/attributes.rb', line 57
def add_required_rule
add_validation_rule { |value| ! value.nil? }.error_string('required')
end
|
#add_validation_rule(&block) ⇒ Object
69
70
71
72
|
# File 'lib/nemo/metaobject/attributes.rb', line 69
def add_validation_rule(&block)
@validation_rules << rule_class.new(&block)
@validation_rules.last
end
|
#commit_cache ⇒ Object
32
33
34
|
# File 'lib/nemo/metaobject/attributes.rb', line 32
def commit_cache
@metaobject.baseobject.send(@symbol.to_s+'=', @cache) unless read_only?
end
|
49
50
51
|
# File 'lib/nemo/metaobject/attributes.rb', line 49
def format(value)
@format_with ? @format_with.call(value).to_s : value.to_s
end
|
53
54
55
|
# File 'lib/nemo/metaobject/attributes.rb', line 53
def formatted_value
format(value)
end
|
#refresh_cache ⇒ Object
36
37
38
|
# File 'lib/nemo/metaobject/attributes.rb', line 36
def refresh_cache
@cache = value
end
|
#required ⇒ Object
61
62
63
|
# File 'lib/nemo/metaobject/attributes.rb', line 61
def required
add_required_rule
end
|
#required? ⇒ Boolean
65
66
67
|
# File 'lib/nemo/metaobject/attributes.rb', line 65
def required?
@validation_rules.size > 0
end
|
#validate_cache ⇒ Object
40
41
42
43
|
# File 'lib/nemo/metaobject/attributes.rb', line 40
def validate_cache
error = @validation_rules.find_all { |rule| ! rule.valid?(@cache) }.first
error_class.new(self, error.error_string) if error
end
|
#value ⇒ Object
45
46
47
|
# File 'lib/nemo/metaobject/attributes.rb', line 45
def value
@metaobject.baseobject.send(@symbol)
end
|