Class: Nemo::MetaObject::Attribute

Inherits:
Object
  • Object
show all
Extended by:
Util::Accessors
Defined in:
lib/nemo/metaobject/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Accessors

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

#cacheObject

Returns the value of attribute cache.



5
6
7
# File 'lib/nemo/metaobject/attributes.rb', line 5

def cache
  @cache
end

#metaobjectObject

Returns the value of attribute metaobject.



5
6
7
# File 'lib/nemo/metaobject/attributes.rb', line 5

def metaobject
  @metaobject
end

#symbolObject

Returns the value of attribute symbol.



5
6
7
# File 'lib/nemo/metaobject/attributes.rb', line 5

def symbol
  @symbol
end

#validation_rulesObject

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

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/nemo/metaobject/attributes.rb', line 28

def accept(visitor)
  raise NotImplementedError, 'subclass responsibility'
end

#add_required_ruleObject



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_cacheObject



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

#error_classObject



20
21
22
# File 'lib/nemo/metaobject/attributes.rb', line 20

def error_class
  Nemo::MetaObject::ValidationError
end

#format(value) ⇒ Object



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

#formatted_valueObject



53
54
55
# File 'lib/nemo/metaobject/attributes.rb', line 53

def formatted_value
  format(value)
end

#refresh_cacheObject



36
37
38
# File 'lib/nemo/metaobject/attributes.rb', line 36

def refresh_cache
  @cache = value
end

#requiredObject



61
62
63
# File 'lib/nemo/metaobject/attributes.rb', line 61

def required
  add_required_rule
end

#required?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/nemo/metaobject/attributes.rb', line 65

def required?
  @validation_rules.size > 0
end

#rule_classObject



24
25
26
# File 'lib/nemo/metaobject/attributes.rb', line 24

def rule_class
  Nemo::MetaObject::ValidationRule
end

#validate_cacheObject



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

#valueObject



45
46
47
# File 'lib/nemo/metaobject/attributes.rb', line 45

def value
  @metaobject.baseobject.send(@symbol)
end