Class: Traits::Attribute

Inherits:
Base
  • Object
show all
Includes:
EssayShortcuts, Inheritance, Key, Naming, Polymorphism, Querying, Type
Defined in:
lib/traits/attribute.rb,
lib/traits/attribute.rb,
lib/traits/attribute/key.rb,
lib/traits/attribute/type.rb,
lib/traits/attribute/naming.rb,
lib/traits/attribute/querying.rb,
lib/traits/attribute/inheritance.rb,
lib/traits/attribute/polymorphism.rb,
lib/traits/attribute/essay_shortcuts.rb

Defined Under Namespace

Modules: EssayShortcuts, Inheritance, Key, Naming, Polymorphism, Querying, Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EssayShortcuts

#features

Methods included from Inheritance

#inheritance_type?, #to_hash

Methods included from Querying

#arel

Methods included from Polymorphism

#polymorphic_key?, #polymorphic_type?, #to_hash

Methods included from Type

#active_record_timestamp?, #big?, #binary?, #datetime?, #decimal?, #float?, #integer?, #number?, #real?, #string?, #text?, #to_hash, #type

Methods included from Naming

#name, #quoted_name, #to_hash

Methods included from Key

#foreign_key?, #key?, #primary_key?, #to_hash

Methods inherited from Base

#to_hash

Constructor Details

#initialize(active_record, column_definition) ⇒ Attribute

Returns a new instance of Attribute.



29
30
31
32
# File 'lib/traits/attribute.rb', line 29

def initialize(active_record, column_definition)
  @active_record      = active_record
  @column_definition = column_definition
end

Instance Attribute Details

#active_recordObject (readonly)

Returns the value of attribute active_record.



27
28
29
# File 'lib/traits/attribute.rb', line 27

def active_record
  @active_record
end

#column_definitionObject (readonly)

Returns the value of attribute column_definition.



27
28
29
# File 'lib/traits/attribute.rb', line 27

def column_definition
  @column_definition
end

Instance Method Details

#associationObject



59
60
61
# File 'lib/traits/attribute.rb', line 59

def association
  model.associations.first_where(from_key_name: name)
end

#inspectObject



51
52
53
# File 'lib/traits/attribute.rb', line 51

def inspect
  "#{model}##{name}"
end

#modelObject



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

def model
  active_record.traits
end

#null?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/traits/attribute.rb', line 63

def null?
  column_definition.null
end

#regular?Boolean

TODO Remove this

Returns:

  • (Boolean)


39
40
41
# File 'lib/traits/attribute.rb', line 39

def regular?
  !(key? || inheritance_type? || polymorphic_type? || active_record_timestamp?)
end

#to_sObject



55
56
57
# File 'lib/traits/attribute.rb', line 55

def to_s
  name
end

#validatorsObject



43
44
45
# File 'lib/traits/attribute.rb', line 43

def validators
  active_record.validators_on(name)
end

#value_from(model_instance) ⇒ Object



47
48
49
# File 'lib/traits/attribute.rb', line 47

def value_from(model_instance)
  model_instance.send(name)
end