Class: ActiveMocker::ModelReader::ParsedProperties Private

Inherits:
Object
  • Object
show all
Defined in:
lib/active_mocker/model_reader.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, parent_class, model_name) ⇒ ParsedProperties

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ParsedProperties.



78
79
80
81
82
# File 'lib/active_mocker/model_reader.rb', line 78

def initialize(klass, parent_class, model_name)
  @klass        = klass
  @parent_class = parent_class
  @model_name   = model_name
end

Instance Attribute Details

#klassObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def klass
  @klass
end

#model_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def model_name
  @model_name
end

#parent_classObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def parent_class
  @parent_class
end

Instance Method Details

#abstract_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def abstract_class
  rails_version.try(:abstract_class)
end

#belongs_toObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'lib/active_mocker/model_reader.rb', line 102

def belongs_to
  select_only_current_class(:belongs_to)
end

#class_methodsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def class_methods
  klass.methods(false)
end

#class_methods_with_argumentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



142
143
144
145
146
# File 'lib/active_mocker/model_reader.rb', line 142

def class_methods_with_arguments
  class_methods.map do |m|
    {m => klass.method(m).parameters}
  end
end

#constantsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



160
161
162
163
164
165
166
167
# File 'lib/active_mocker/model_reader.rb', line 160

def constants
  const = {}
  klass.constants.each { |c| const[c] = klass.const_get(c) }
  const = const.reject do |c, v|
    v.class == Module || v.class == Class
  end
  const
end

#has_and_belongs_to_manyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



110
111
112
# File 'lib/active_mocker/model_reader.rb', line 110

def has_and_belongs_to_many
  select_only_current_class(:has_and_belongs_to_many)
end

#has_manyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



114
115
116
# File 'lib/active_mocker/model_reader.rb', line 114

def has_many
  select_only_current_class(:has_many)
end

#has_oneObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



106
107
108
# File 'lib/active_mocker/model_reader.rb', line 106

def has_one
  select_only_current_class(:has_one)
end

#instance_methodsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



154
155
156
157
158
# File 'lib/active_mocker/model_reader.rb', line 154

def instance_methods
  methods = klass.public_instance_methods(false)
  methods << klass.superclass.public_instance_methods(false) if klass.superclass != ActiveRecord::Base
  methods.flatten
end

#instance_methods_with_argumentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



148
149
150
151
152
# File 'lib/active_mocker/model_reader.rb', line 148

def instance_methods_with_arguments
  instance_methods.map do |m|
    {m => klass.instance_method(m).parameters}
  end
end

#modulesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def modules
  {included: process_module_names(klass._included),
   extended: process_module_names(klass._extended)}
end

#primary_keyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def primary_key
  rails_version.primary_key
end

#process_module_names(names) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def process_module_names(names)
  names.reject { |m| /#{klass.inspect}/ =~ m.name }.map(&:inspect)
end

#rails_versionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
88
89
90
# File 'lib/active_mocker/model_reader.rb', line 84

def rails_version
  begin
    @rails_version ||= model_name.classify.constantize
  rescue
    raise ModelLoadError::LoadingModelInRails.new($!, model_name)
  end
end

#scopesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'lib/active_mocker/model_reader.rb', line 132

def scopes
  klass.get_named_scopes
end

#scopes_with_argumentsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



136
137
138
139
140
# File 'lib/active_mocker/model_reader.rb', line 136

def scopes_with_arguments
  scopes.map do |name, proc|
    {name => proc.parameters, :proc => proc}
  end
end

#select_only_current_class(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



96
97
98
99
100
# File 'lib/active_mocker/model_reader.rb', line 96

def select_only_current_class(type)
  rails_version.reflect_on_all_associations(type).select do |a|
    klass.relationships.send(type).map(&:name).include?(a.name)
  end
end

#table_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



118
119
120
121
122
# File 'lib/active_mocker/model_reader.rb', line 118

def table_name
  return rails_version.try(:table_name) if rails_version.try(:superclass).try(:name) == 'ActiveRecord::Base'
  return nil if rails_version.superclass.try(:table_name) == rails_version.try(:table_name)
  rails_version.try(:table_name)
end