Class: Nemo::MetaObject::DateAttribute

Inherits:
TextAttribute show all
Defined in:
lib/nemo/metaobject/attributes.rb

Instance Attribute Summary

Attributes inherited from Attribute

#cache, #metaobject, #symbol, #validation_rules

Instance Method Summary collapse

Methods inherited from TextAttribute

#cache

Methods inherited from Attribute

#add_validation_rule, #error_class, #format, #formatted_value, #required, #required?, #rule_class, #validate_cache, #value

Methods included from Util::Accessors

#bool_accessor, #call_accessor, #proc_accessor

Constructor Details

#initialize(symbol) ⇒ DateAttribute

Returns a new instance of DateAttribute.



123
124
125
126
# File 'lib/nemo/metaobject/attributes.rb', line 123

def initialize(symbol)
  super
  @format_with = Proc.new { |value| value.to_s }
end

Instance Method Details

#accept(visitor) ⇒ Object



128
129
130
# File 'lib/nemo/metaobject/attributes.rb', line 128

def accept(visitor)
  visitor.visit_date_attribute(self)
end

#add_required_ruleObject



132
133
134
# File 'lib/nemo/metaobject/attributes.rb', line 132

def add_required_rule
  add_validation_rule { |value| date_from_cached(value) && true }
end

#commit_cacheObject



152
153
154
# File 'lib/nemo/metaobject/attributes.rb', line 152

def commit_cache
  @metaobject.baseobject.send(@symbol.to_s+'=', date_from_cache) unless read_only?
end

#date_from_cacheObject



136
137
138
# File 'lib/nemo/metaobject/attributes.rb', line 136

def date_from_cache
  date_from_cached(@cache)
end

#date_from_cached(value) ⇒ Object



140
141
142
# File 'lib/nemo/metaobject/attributes.rb', line 140

def date_from_cached(value)
  begin Date.parse(value.to_s) rescue nil end
end

#date_to_cache(date) ⇒ Object



144
145
146
# File 'lib/nemo/metaobject/attributes.rb', line 144

def date_to_cache(date)
  @cache = begin @format_with.call(date) rescue date.to_s end
end

#refresh_cacheObject



148
149
150
# File 'lib/nemo/metaobject/attributes.rb', line 148

def refresh_cache
  date_to_cache(value)
end