Class: Rumbly::Model::ActiveRecord::Attribute

Inherits:
Rumbly::Model::Attribute show all
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

Instance Method Summary collapse

Methods inherited from Rumbly::Model::Attribute

#<=>, #derived?, #label, #static?

Methods included from Rumbly::Model::Abstract

#stub_required_methods

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

#constraintsObject

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

#defaultObject

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

#derivedObject

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

#multiplicityObject

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

#nameObject

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

#propertiesObject

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

#staticObject

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

#typeObject

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

#visibilityObject

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