Class: Model

Inherits:
Object
  • Object
show all
Defined in:
lib/model-visualizer/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Model

Returns a new instance of Model.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/model-visualizer/model.rb', line 5

def initialize(name)
    @name = name
    @belongs_to = Array.new
    @has_one = Array.new
    @has_many = Array.new
    @has_and_belongs_to_many = Array.new
    @integer_attributes = Array.new
    @string_attributes = Array.new
    @primary_key_attributes = Array.new
    @text_attributes = Array.new
    @float_attributes = Array.new
    @decimal_attributes = Array.new
    @datetime_attributes = Array.new
    @timestamp_attributes = Array.new
    @time_attributes = Array.new
    @date_attributes = Array.new
    @binary_attributes = Array.new
    @boolean_attributes = Array.new
    @foreign_keys = Array.new
end

Instance Method Details

#add_belongs_to(model) ⇒ Object



26
27
28
# File 'lib/model-visualizer/model.rb', line 26

def add_belongs_to(model)
    @belongs_to << model
end

#add_binary_attribute(binary) ⇒ Object



82
83
84
# File 'lib/model-visualizer/model.rb', line 82

def add_binary_attribute(binary)
    @binary_attributes << binary
end

#add_boolean_attribute(boolean) ⇒ Object



86
87
88
# File 'lib/model-visualizer/model.rb', line 86

def add_boolean_attribute(boolean)
    @boolean_attributes << boolean
end

#add_date_attribute(date) ⇒ Object



78
79
80
# File 'lib/model-visualizer/model.rb', line 78

def add_date_attribute(date)
    @date_attributes << date
end

#add_datetime_attribute(datetime) ⇒ Object



66
67
68
# File 'lib/model-visualizer/model.rb', line 66

def add_datetime_attribute(datetime)
    @datetime_attributes << datetime
end

#add_decimal_attribute(decimal) ⇒ Object



62
63
64
# File 'lib/model-visualizer/model.rb', line 62

def add_decimal_attribute(decimal)
    @decimal_attributes << decimal
end

#add_float_attribute(float) ⇒ Object



58
59
60
# File 'lib/model-visualizer/model.rb', line 58

def add_float_attribute(float)
    @float_attributes << float
end

#add_foreign_key_attribute(key) ⇒ Object



90
91
92
# File 'lib/model-visualizer/model.rb', line 90

def add_foreign_key_attribute(key)
    @foreign_keys << key
end

#add_has_and_belongs_to_many(model) ⇒ Object



38
39
40
# File 'lib/model-visualizer/model.rb', line 38

def add_has_and_belongs_to_many(model)
    @has_and_belongs_to_many << model
end

#add_has_many(model) ⇒ Object



34
35
36
# File 'lib/model-visualizer/model.rb', line 34

def add_has_many(model)
    @has_many << model
end

#add_has_one(model) ⇒ Object



30
31
32
# File 'lib/model-visualizer/model.rb', line 30

def add_has_one(model)
    @has_one << model
end

#add_integer_attribute(integer) ⇒ Object



42
43
44
# File 'lib/model-visualizer/model.rb', line 42

def add_integer_attribute(integer)
    @integer_attributes << integer
end

#add_primary_key_attribute(primary_key) ⇒ Object



50
51
52
# File 'lib/model-visualizer/model.rb', line 50

def add_primary_key_attribute(primary_key)
    @primary_key_attributes << primary_key
end

#add_string_attribute(string) ⇒ Object



46
47
48
# File 'lib/model-visualizer/model.rb', line 46

def add_string_attribute(string)
    @string_attributes << string
end

#add_text_attribute(text) ⇒ Object



54
55
56
# File 'lib/model-visualizer/model.rb', line 54

def add_text_attribute(text)
    @text_attributes << text
end

#add_time_attribute(time) ⇒ Object



74
75
76
# File 'lib/model-visualizer/model.rb', line 74

def add_time_attribute(time)
    @time_attributes << time
end

#add_timestamp_attribute(timestamp) ⇒ Object



70
71
72
# File 'lib/model-visualizer/model.rb', line 70

def add_timestamp_attribute(timestamp)
    @timestamp_attributes << timestamp
end

#to_json(options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/model-visualizer/model.rb', line 94

def to_json(options = {})
    json = {
        'name' => @name,
        'associations' => {},
        'schema_info' => {}
    }

    if not @belongs_to.empty?
        json['associations']['belongs_to'] = @belongs_to
    end
    if not @has_one.empty?
        json['associations']['has_one'] = @has_one
    end
    if not @has_many.empty?
        json['associations']['has_many'] = @has_many
    end
    if not @has_and_belongs_to_many.empty?
        json['associations']['has_and_belongs_to_many'] = @has_and_belongs_to_many
    end

    if not @integer_attributes.empty?
        json['schema_info']['integer_attributes'] = @integer_attributes
    end
    if not @string_attributes.empty?
        json['schema_info']['string_attributes'] = @string_attributes
    end
    if not @primary_key_attributes.empty?
        json['schema_info']['primary_key_attributes'] = @primary_key_attributes
    end
    if not @text_attributes.empty?
        json['schema_info']['text_attributes'] = @text_attributes
    end
    if not @float_attributes.empty?
        json['schema_info']['float_attributes'] = @float_attributes
    end
    if not @decimal_attributes.empty?
        json['schema_info']['decimal_attributes'] = @decimal_attributes
    end
    if not @date_attributes.empty?
        json['schema_info']['date_attributes'] = @date_attributes
    end
    if not @datetime_attributes.empty?
        json['schema_info']['datetime_attributes'] = @datetime_attributes
    end
    if not @time_attributes.empty?
        json['schema_info']['time_attributes'] = @time_attributes
    end
    if not @timestamp_attributes.empty?
        json['schema_info']['timestamp_attributes'] = @timestamp_attributes
    end
    if not @binary_attributes.empty?
        json['schema_info']['binary_attributes'] = @binary_attributes
    end
    if not @boolean_attributes.empty?
        json['schema_info']['boolean_attributes'] = @boolean_attributes
    end
    if not @foreign_keys.empty?
        json['schema_info']['foreign_keys'] = @foreign_keys
    end

    json.to_json
end