Class: Rumbly::Model::ActiveRecord::Attribute
- Inherits:
-
Rumbly::Model::Attribute
- Object
- Rumbly::Model::Attribute
- Rumbly::Model::ActiveRecord::Attribute
- Defined in:
- lib/rumbly/model/active_record/attribute.rb
Overview
This class is an ActiveRecord-specific implementation of the abstract Rumbly::Model::Attribute class use dto represent declared attributes (columns) on model classes in the currently loaded environment.
Constant Summary
Constants inherited from Rumbly::Model::Attribute
Rumbly::Model::Attribute::ATTRIBUTES
Class Method Summary collapse
-
.all_from_klass(klass) ⇒ Object
Returns an array of
Rumbly::Model::ActiveRecord::Attributeobjects, each of which wraps a field (column) from the givenActiveRecordmodel class.
Instance Method Summary collapse
-
#constraints ⇒ Object
Returns an
ArrayofStringvalues representing constraints placed on this attribute viaActiveModelvalidations. -
#default ⇒ Object
Returns this attribute’s default value based on
ActiveRecordcolumn definition. -
#derived ⇒ Object
Returns
nilsinceActiveRecorddoesn’t declare derived attributes. -
#initialize(klass, column) ⇒ Attribute
constructor
A new instance of Attribute.
-
#multiplicity ⇒ Object
Returns
nilsinceActiveRecorddoesn’t allow for non-intrinsic attributes. -
#name ⇒ Object
Returns the name of this
ActiveRecordattribute based on the column definition. -
#properties ⇒ Object
Returns
nilsinceActiveRecorddoesn’t support any of the standard UML attribute properties (e.g. read-only, union, composite, etc.). -
#static ⇒ Object
Returns
nilsinceActiveRecorddoesn’t declare static (class) attributes. -
#type ⇒ Object
Returns the type of this
ActiveRecordattribute based on the column definition. -
#visibility ⇒ Object
Returns
nilsinceActiveRecorddoesn’t declare attribute visibility.
Methods inherited from Rumbly::Model::Attribute
#<=>, #derived?, #label, #static?
Methods included from Rumbly::Model::Abstract
Constructor Details
#initialize(klass, column) ⇒ Attribute
Returns a new instance of Attribute.
20 21 22 23 24 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 20 def initialize (klass, column) @klass = klass @cls = klass.cls @column = column end |
Class Method Details
.all_from_klass(klass) ⇒ Object
Returns an array of Rumbly::Model::ActiveRecord::Attribute objects, each of which wraps a field (column) from the given ActiveRecord model class.
14 15 16 17 18 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 14 def self.all_from_klass (klass) klass.cls.columns.map do |column| new(klass, column) end end |
Instance Method Details
#constraints ⇒ Object
Returns an Array of String values representing constraints placed on this attribute via ActiveModel validations. Only simple, declarative validations will be reflected as constraints (i.e. not conditional or custom validations). Also, some parameters or conditions on simple validations will not be shown, e.g. scope or case-sensitivity on a uniqueness validation. Currently, the following ActiveModel validations are ignored: inclusion, exclusion, format, and any conditional validations.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 75 def constraints @constraints ||= begin constraints = [] constraints << 'required' if required? constraints << 'unique' if unique? append_numeric_constraints(constraints) append_length_constraints(constraints) constraints end end |
#default ⇒ Object
Returns this attribute’s default value based on ActiveRecord column definition.
58 59 60 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 58 def default @default ||= @column.default end |
#derived ⇒ Object
Returns nil since ActiveRecord doesn’t declare derived attributes.
87 88 89 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 87 def derived nil end |
#multiplicity ⇒ Object
Returns nil since ActiveRecord doesn’t allow for non-intrinsic attributes.
53 54 55 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 53 def multiplicity nil end |
#name ⇒ Object
Returns the name of this ActiveRecord attribute based on the column definition.
28 29 30 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 28 def name @name ||= @column.name end |
#properties ⇒ Object
Returns nil since ActiveRecord doesn’t support any of the standard UML attribute properties (e.g. read-only, union, composite, etc.).
64 65 66 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 64 def properties [] end |
#static ⇒ Object
Returns nil since ActiveRecord doesn’t declare static (class) attributes.
92 93 94 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 92 def static nil end |
#type ⇒ Object
Returns the type of this ActiveRecord attribute based on the column definition.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 34 def type @type ||= begin type = @column.type.to_s unless @column.limit.nil? type += "(#{@column.limit})" end unless @column.precision.nil? || @column.scale.nil? type += "(#{@column.precision},#{@column.scale})" end type end end |
#visibility ⇒ Object
Returns nil since ActiveRecord doesn’t declare attribute visibility.
48 49 50 |
# File 'lib/rumbly/model/active_record/attribute.rb', line 48 def visibility nil end |