Class: ActiveMocker::ModelSchema

Inherits:
Object
  • Object
show all
Includes:
LoggerToJson
Defined in:
lib/active_mocker/model_schema.rb,
lib/active_mocker/model_schema/generate.rb

Defined Under Namespace

Classes: Attributes, Generate, Methods, Relationships

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name: nil, table_name: nil, attributes: nil, relationships: nil, methods: nil, constants: nil, modules: nil, is_join_table: nil, join_table_members: nil) ⇒ ModelSchema

Returns a new instance of ModelSchema.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_mocker/model_schema.rb', line 58

def initialize( class_name:    nil,
                table_name:    nil,
                attributes:    nil,
                relationships: nil,
                methods:       nil,
                constants:     nil,
                modules:       nil,
                is_join_table: nil,
                join_table_members: nil
                )
  @class_name    = class_name
  @table_name    = table_name
  @attributes    = attributes    unless attributes.nil?
  @relationships = relationships unless relationships.nil?
  @methods       = methods       unless (methods || []).empty?
  @constants     = constants     unless (constants || []).empty?
  @modules       = modules       unless (modules || {}).empty?
  @is_join_table = is_join_table unless is_join_table.nil?
  @join_table_members = join_table_members unless join_table_members.nil?
  # UnitLogger.unit.info "#{caller_locations[1]}\n"
  # obj = ::JSON.parse(self.to_json)
  # UnitLogger.unit.info "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
  # puts "#{self.class.name}: #{JSON.pretty_unparse(obj)}\n"
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def attributes
  @attributes
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def class_name
  @class_name
end

#methodsObject (readonly)

Returns the value of attribute methods.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def methods
  @methods
end

#modulesObject (readonly)

Returns the value of attribute modules.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def modules
  @modules
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def relationships
  @relationships
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



56
57
58
# File 'lib/active_mocker/model_schema.rb', line 56

def table_name
  @table_name
end

Instance Method Details

#associationsObject



152
153
154
155
156
157
158
# File 'lib/active_mocker/model_schema.rb', line 152

def associations
  hash = {}
  relationships.each do |r|
    hash[r.name] = nil
  end
  hash
end

#attribute_namesObject



128
129
130
# File 'lib/active_mocker/model_schema.rb', line 128

def attribute_names
  attributes.map(&:name)
end

#attributes_with_defaultsObject



144
145
146
147
148
149
150
# File 'lib/active_mocker/model_schema.rb', line 144

def attributes_with_defaults
  hash = {}
  attributes.each do |attr|
    hash[attr.name] = attr.default_value
  end
  hash
end

#belongs_toObject



95
96
97
# File 'lib/active_mocker/model_schema.rb', line 95

def belongs_to
  relation_find(:type, :belongs_to)
end

#belongs_to_foreign_key(foreign_key) ⇒ Object



103
104
105
# File 'lib/active_mocker/model_schema.rb', line 103

def belongs_to_foreign_key(foreign_key)
  belongs_to.select { |r| r.foreign_key.to_sym == foreign_key.to_sym }.first
end

#class_methodsObject



115
116
117
# File 'lib/active_mocker/model_schema.rb', line 115

def class_methods
  method_find(:class)
end

#constantsObject



83
84
85
# File 'lib/active_mocker/model_schema.rb', line 83

def constants
  @constants || []
end

#default_primary_keyObject



185
186
187
188
189
# File 'lib/active_mocker/model_schema.rb', line 185

def default_primary_key
  default_id = Attributes.new(name: 'id', primary_key: true, type: :integer)
  attributes.unshift(default_id)
  default_id
end

#has_and_belongs_to_manyObject



99
100
101
# File 'lib/active_mocker/model_schema.rb', line 99

def has_and_belongs_to_many
  relation_find(:type, :has_and_belongs_to_many)
end

#has_manyObject



87
88
89
# File 'lib/active_mocker/model_schema.rb', line 87

def has_many
  relation_find(:type, :has_many)
end

#has_oneObject



91
92
93
# File 'lib/active_mocker/model_schema.rb', line 91

def has_one
  relation_find(:type, :has_one)
end

#instance_methodsObject



111
112
113
# File 'lib/active_mocker/model_schema.rb', line 111

def instance_methods
  method_find(:instance)
end

#method_find(type) ⇒ Object



123
124
125
126
# File 'lib/active_mocker/model_schema.rb', line 123

def method_find(type)
  return [] if methods.nil?
  methods.select { |r| r.type.to_sym == type }
end

#mock_name(klass_name) ⇒ Object



160
161
162
# File 'lib/active_mocker/model_schema.rb', line 160

def mock_name(klass_name)
  klass_name + "Mock"
end

#mockable_class_methodsObject



164
165
166
# File 'lib/active_mocker/model_schema.rb', line 164

def mockable_class_methods
  class_methods.map(&:name).each_with_object({}) { |val, hash| hash[val] = nil }
end

#mockable_instance_methodsObject



168
169
170
# File 'lib/active_mocker/model_schema.rb', line 168

def mockable_instance_methods
  instance_methods.map(&:name).each_with_object({}) { |val, hash| hash[val] = nil }
end

#primary_keyObject



179
180
181
182
183
# File 'lib/active_mocker/model_schema.rb', line 179

def primary_key
  key_attribute = attributes.select { |attr| attr.primary_key }.first
  return key_attribute if key_attribute
  default_primary_key
end

#render(template, mock_append_name) ⇒ Object



172
173
174
175
# File 'lib/active_mocker/model_schema.rb', line 172

def render(template, mock_append_name)
  @mock_append_name = mock_append_name
  ERB.new(template, nil, '-').result(binding)
end

#scope_methodsObject



119
120
121
# File 'lib/active_mocker/model_schema.rb', line 119

def scope_methods
  method_find(:scope)
end

#types_hashObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/active_mocker/model_schema.rb', line 132

def types_hash
  types = {}
  attributes.each do |attr|
    types[attr.name] = "#{attr.ruby_type}"
  end

  type_array = types.map do |name, type|
    "#{name}: #{type}"
  end
  '{ ' + type_array.join(', ') + ' }'
end