Class: Property::Column
- Inherits:
-
ActiveRecord::ConnectionAdapters::Column
- Object
- ActiveRecord::ConnectionAdapters::Column
- Property::Column
- Defined in:
- lib/property/column.rb
Overview
The Column class is used to hold information about a Property declaration, such as name, type and options. It is also used to typecast from strings to the proper type (date, integer, float, etc).
Constant Summary collapse
- SAFE_NAMES_REGEXP =
%r{\A[a-zA-Z_]+\Z}
Instance Attribute Summary collapse
-
#caster ⇒ Object
Returns the value of attribute caster.
-
#index ⇒ Object
Returns the value of attribute index.
Instance Method Summary collapse
- #default_for(owner) ⇒ Object
-
#index_proc ⇒ Object
Return the Proc to use to build index (usually nil).
- #indexed? ⇒ Boolean
-
#initialize(name, default, type, options = {}) ⇒ Column
constructor
A new instance of Column.
-
#klass ⇒ Object
Return the class related to the type of property stored (String, Integer, etc).
-
#role ⇒ Object
Return the role in which the column was originally defined.
- #should_create_accessors? ⇒ Boolean
- #type_cast(value) ⇒ Object
- #validate(value, errors) ⇒ Object
Constructor Details
#initialize(name, default, type, options = {}) ⇒ Column
Returns a new instance of Column.
13 14 15 16 17 18 19 20 21 |
# File 'lib/property/column.rb', line 13 def initialize(name, default, type, ={}) name = name.to_s if type.kind_of?(Class) @klass = type end super(name, default, type, ) () end |
Instance Attribute Details
#caster ⇒ Object
Returns the value of attribute caster.
9 10 11 |
# File 'lib/property/column.rb', line 9 def caster @caster end |
#index ⇒ Object
Returns the value of attribute index.
9 10 11 |
# File 'lib/property/column.rb', line 9 def index @index end |
Instance Method Details
#default_for(owner) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/property/column.rb', line 37 def default_for(owner) default = self.default if default.kind_of?(Proc) default.call elsif default.kind_of?(Symbol) owner.send(default) else default end end |
#index_proc ⇒ Object
Return the Proc to use to build index (usually nil).
75 76 77 |
# File 'lib/property/column.rb', line 75 def index_proc @index_proc end |
#indexed? ⇒ Boolean
33 34 35 |
# File 'lib/property/column.rb', line 33 def indexed? @index end |
#klass ⇒ Object
Return the class related to the type of property stored (String, Integer, etc).
49 50 51 |
# File 'lib/property/column.rb', line 49 def klass @klass || super end |
#role ⇒ Object
Return the role in which the column was originally defined.
54 55 56 |
# File 'lib/property/column.rb', line 54 def role @role end |
#should_create_accessors? ⇒ Boolean
29 30 31 |
# File 'lib/property/column.rb', line 29 def should_create_accessors? name =~ SAFE_NAMES_REGEXP end |
#type_cast(value) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/property/column.rb', line 61 def type_cast(value) if @caster && val = @caster.type_cast(value) val elsif type == :string value = value.to_s value.blank? ? nil : value elsif @klass value else super end end |
#validate(value, errors) ⇒ Object
23 24 25 26 27 |
# File 'lib/property/column.rb', line 23 def validate(value, errors) if @klass && !value.kind_of?(@klass) errors.add(name, "cannot cast #{value.class} to #{@klass}") end end |