Class: Databasium::Model::ModelData

Inherits:
Object
  • Object
show all
Defined in:
app/services/databasium/model.rb

Defined Under Namespace

Classes: Attribute, Validation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_name:, attributes:, relations:, unknown: []) ⇒ ModelData

Returns a new instance of ModelData.



98
99
100
101
102
103
# File 'app/services/databasium/model.rb', line 98

def initialize(model_name:, attributes:, relations:, unknown: [])
  @model_name = model_name
  @attributes = attributes
  @relations = relations
  @unknown = unknown
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



96
97
98
# File 'app/services/databasium/model.rb', line 96

def attributes
  @attributes
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



96
97
98
# File 'app/services/databasium/model.rb', line 96

def model_name
  @model_name
end

#relationsObject (readonly)

Returns the value of attribute relations.



96
97
98
# File 'app/services/databasium/model.rb', line 96

def relations
  @relations
end

#unknownObject (readonly)

Returns the value of attribute unknown.



96
97
98
# File 'app/services/databasium/model.rb', line 96

def unknown
  @unknown
end

Instance Method Details

#get_bindingObject



105
106
107
# File 'app/services/databasium/model.rb', line 105

def get_binding
  binding
end

#longest_name_lengthObject



109
110
111
# File 'app/services/databasium/model.rb', line 109

def longest_name_length
  attributes.map { |a| (a[:name] || a["name"]).to_s.length }.max || 0
end

#relation_name(relation) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'app/services/databasium/model.rb', line 113

def relation_name(relation)
  table_name = relation[:table_name].to_s

  case relation[:type]
  when "has_many", "has_and_belongs_to_many"
    table_name.tableize
  when "belongs_to", "has_one"
    table_name.singularize.underscore
  end
end