Class: Reactive::MetaModel::ActiveRecordModel

Inherits:
Model
  • Object
show all
Defined in:
lib/activerecord_meta_model.rb

Defined Under Namespace

Classes: Association, Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ActiveRecordModel

Returns a new instance of ActiveRecordModel.



68
69
70
71
# File 'lib/activerecord_meta_model.rb', line 68

def initialize(model)
  @model = model
  @name = model.to_s.demodulize.underscore
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



64
65
66
# File 'lib/activerecord_meta_model.rb', line 64

def name
  @name
end

Instance Method Details

#associationsObject



85
86
87
# File 'lib/activerecord_meta_model.rb', line 85

def associations
  belong_tos + has_ones + has_manys
end

#attribute(name) ⇒ Object



97
98
99
# File 'lib/activerecord_meta_model.rb', line 97

def attribute(name)
  attributes.find {|attribute| attribute.name == name.to_sym}
end

#attributesObject



93
94
95
# File 'lib/activerecord_meta_model.rb', line 93

def attributes
  columns + associations
end

#belong_tosObject



81
82
83
# File 'lib/activerecord_meta_model.rb', line 81

def belong_tos
  @belong_tos ||= @model.reflect_on_all_associations(:belongs_to).map {|association| Association.new(association)}
end

#columnsObject



89
90
91
# File 'lib/activerecord_meta_model.rb', line 89

def columns
  @columns ||= @model.columns.map {|column| Column.new(column)}
end

#has_manysObject



73
74
75
# File 'lib/activerecord_meta_model.rb', line 73

def has_manys
  @has_manys ||= @model.reflect_on_all_associations(:has_many).map {|association| Association.new(association)}
end

#has_onesObject



77
78
79
# File 'lib/activerecord_meta_model.rb', line 77

def has_ones
  @has_ones ||= @model.reflect_on_all_associations(:has_one).map {|association| Association.new(association)}
end