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.



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

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.



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

def attributes
  @attributes
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



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

def model_name
  @model_name
end

#relationsObject (readonly)

Returns the value of attribute relations.



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

def relations
  @relations
end

#unknownObject (readonly)

Returns the value of attribute unknown.



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

def unknown
  @unknown
end

Instance Method Details

#get_bindingObject



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

def get_binding
  binding
end

#longest_name_lengthObject



113
114
115
# File 'app/services/databasium/model.rb', line 113

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

#relation_name(relation) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'app/services/databasium/model.rb', line 117

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