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



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

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.



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

def attributes
  @attributes
end

#class_nameObject (readonly)

Returns the value of attribute class_name.



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

def class_name
  @class_name
end

#methodsObject (readonly)

Returns the value of attribute methods.



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

def methods
  @methods
end

#modulesObject (readonly)

Returns the value of attribute modules.



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

def modules
  @modules
end

#relationshipsObject (readonly)

Returns the value of attribute relationships.



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

def relationships
  @relationships
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#associationsObject



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

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

#attribute_namesObject



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

def attribute_names
  attributes.map(&:name)
end

#attributes_with_defaultsObject



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

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

#belongs_toObject



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

def belongs_to
  relation_find(:type, :belongs_to)
end

#belongs_to_foreign_key(foreign_key) ⇒ Object



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

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

#class_methodsObject



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

def class_methods
  method_find(:class)
end

#constantsObject



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

def constants
  @constants || []
end

#default_primary_keyObject



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

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



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

def has_and_belongs_to_many
  relation_find(:type, :has_and_belongs_to_many)
end

#has_manyObject



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

def has_many
  relation_find(:type, :has_many)
end

#has_oneObject



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

def has_one
  relation_find(:type, :has_one)
end

#instance_methodsObject



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

def instance_methods
  method_find(:instance)
end

#method_find(type) ⇒ Object



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

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

#mock_name(klass_name) ⇒ Object



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

def mock_name(klass_name)
  klass_name + "Mock"
end

#mockable_class_methodsObject



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

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

#mockable_instance_methodsObject



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

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

#primary_keyObject



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

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



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

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

#scope_methodsObject



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

def scope_methods
  method_find(:scope)
end

#types_hashObject



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

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