Class: Anchormodel::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/anchormodel/attribute.rb

Overview

Metadata about an anchormodel attribute installed on a Rails model.

One instance is created per call to ModelMixin#belongs_to_anchormodel or ModelMixin#belongs_to_anchormodels and stored in the model class's anchormodel_attributes hash. Used internally by the AR type casters and by SimpleForm inputs to discover what an attribute points to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, attribute_name, anchormodel_class = nil, optional = false, multiple = false) ⇒ Attribute

Returns a new instance of Attribute.

Parameters:

  • model_class (Class)

    The ActiveRecord model class on which ModelMixin#belongs_to_anchormodel is called.

  • attribute_name (String, Symbol)

    The model attribute / DB column name.

  • anchormodel_class (Class, nil) (defaults to: nil)

    The Anchormodel subclass. Omit if attribute :foo_bar references FooBar.

  • optional (Boolean) (defaults to: false)

    If true, no presence validation is added.

  • multiple (Boolean) (defaults to: false)

    If true, this attribute holds a Set of anchormodels (CSV in column).



25
26
27
28
29
30
31
# File 'lib/anchormodel/attribute.rb', line 25

def initialize(model_class, attribute_name, anchormodel_class = nil, optional = false, multiple = false)
  @model_class = model_class
  @attribute_name = attribute_name.to_sym
  @anchormodel_class = anchormodel_class
  @optional = optional
  @multiple = multiple
end

Instance Attribute Details

#anchormodel_classObject (readonly)

Returns the value of attribute anchormodel_class.



14
15
16
# File 'lib/anchormodel/attribute.rb', line 14

def anchormodel_class
  @anchormodel_class
end

#attribute_nameObject (readonly)

Returns the value of attribute attribute_name.



10
11
12
# File 'lib/anchormodel/attribute.rb', line 10

def attribute_name
  @attribute_name
end

#optionalObject (readonly)

Returns the value of attribute optional.



18
19
20
# File 'lib/anchormodel/attribute.rb', line 18

def optional
  @optional
end

Instance Method Details

#multiple?Boolean

Returns true for belongs_to_anchormodels (collection), false for belongs_to_anchormodel (single).

Returns:

  • (Boolean)

    true for belongs_to_anchormodels (collection), false for belongs_to_anchormodel (single).



34
35
36
# File 'lib/anchormodel/attribute.rb', line 34

def multiple?
  @multiple
end