Class: ActiveMocker::ModelSchema

Inherits:
AttrPermit
  • Object
show all
Defined in:
lib/active_mocker/model_schema.rb,
lib/active_mocker/model_schema/assemble.rb

Defined Under Namespace

Classes: Assemble, Attributes, Methods, Relationships

Instance Method Summary collapse

Instance Method Details

#abstract_classObject



64
65
66
# File 'lib/active_mocker/model_schema.rb', line 64

def abstract_class
  !!call_method(:abstract_class)
end

#associationsObject



141
142
143
144
145
146
147
# File 'lib/active_mocker/model_schema.rb', line 141

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

#associations_by_classObject



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

def associations_by_class
  hash = {}
  relationships.each do |r|
    hash[r.class_name] ||= {}
    hash[r.class_name][r.type] ||= []
    hash[r.class_name][r.type] << r.name
  end
  hash
end

#attribute_namesObject



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

def attribute_names
  attributes.map(&:name)
end

#attributes_with_defaultsObject



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

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

#belongs_toObject



80
81
82
# File 'lib/active_mocker/model_schema.rb', line 80

def belongs_to
  relation_find(:type, :belongs_to)
end

#belongs_to_foreign_key(foreign_key) ⇒ Object



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

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

#class_methodsObject



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

def class_methods
  method_find(:class)
end

#constantsObject



68
69
70
# File 'lib/active_mocker/model_schema.rb', line 68

def constants
  call_method(:constants) || []
end

#default_primary_keyObject



193
194
195
196
197
# File 'lib/active_mocker/model_schema.rb', line 193

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

#has_and_belongs_to_manyObject



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

def has_and_belongs_to_many
  relation_find(:type, :has_and_belongs_to_many)
end

#has_manyObject



72
73
74
# File 'lib/active_mocker/model_schema.rb', line 72

def has_many
  relation_find(:type, :has_many)
end

#has_oneObject



76
77
78
# File 'lib/active_mocker/model_schema.rb', line 76

def has_one
  relation_find(:type, :has_one)
end

#instance_methodsObject



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

def instance_methods
  method_find(:instance)
end

#is_child_class?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/active_mocker/model_schema.rb', line 176

def is_child_class?
  call_method(:parent_class).present?
end

#method_find(type) ⇒ Object



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

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

#methodsObject



108
109
110
# File 'lib/active_mocker/model_schema.rb', line 108

def methods
  call_method(:_methods) || []
end

#mock_name(klass_name) ⇒ Object



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

def mock_name(klass_name)
  klass_name + "Mock"
end

#mockable_class_methodsObject



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

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

#mockable_instance_methodsObject



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

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

#parent_classObject



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

def parent_class
  return mock_name(call_method(:parent_class)) if call_method(:parent_class).present?
  'ActiveMocker::Mock::Base'
end

#primary_keyObject



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

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



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

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

#scope_methodsObject



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

def scope_methods
  method_find(:scope)
end

#types_hashObject



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/active_mocker/model_schema.rb', line 121

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