Class: ActiveMocker::ModelReader 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(options = {}) ⇒ ModelReader

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 ModelReader.



9
10
11
12
# File 'lib/active_mocker/model_reader.rb', line 9

def initialize(options={})
  @file_reader = options[:file_reader] ||= FileReader
  @model_dir   = options[:model_dir]
end

Instance Attribute Details

#file_readerObject (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.



7
8
9
# File 'lib/active_mocker/model_reader.rb', line 7

def file_reader
  @file_reader
end

#model_dirObject (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.



7
8
9
# File 'lib/active_mocker/model_reader.rb', line 7

def model_dir
  @model_dir
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.



7
8
9
# File 'lib/active_mocker/model_reader.rb', line 7

def model_name
  @model_name
end

Instance Method Details

#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.



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

def belongs_to
  klass.relationships.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.



81
82
83
# File 'lib/active_mocker/model_reader.rb', line 81

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.



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

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

#collectionsObject

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.



121
122
123
# File 'lib/active_mocker/model_reader.rb', line 121

def collections
  klass.collections.flatten.compact
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.



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

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

#eval_file(string, file_path) ⇒ 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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/active_mocker/model_reader.rb', line 45

def eval_file(string, file_path)
  failure = false
  begin

    module_namespace.module_eval(string, file_path)
    _klass = module_namespace.const_get(module_namespace.constants.last)
  rescue SyntaxError => e
    log_loading_error(e, true)
    failure = true
  rescue Exception => e
    log_loading_error(e, false)
    failure = true
  end
  return false if failure
  _klass
end

#file_path(m_name = model_name) ⇒ 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.



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

def file_path(m_name=model_name)
  "#{model_dir}/#{m_name}.rb"
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.



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

def has_and_belongs_to_many
  klass.relationships.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.



141
142
143
# File 'lib/active_mocker/model_reader.rb', line 141

def has_many
  klass.relationships.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.



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

def has_one
  klass.relationships.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.



107
108
109
110
111
# File 'lib/active_mocker/model_reader.rb', line 107

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.



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

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

#klassObject

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.



21
22
23
# File 'lib/active_mocker/model_reader.rb', line 21

def klass
  @klass ||= eval_file(sandbox_model, file_path)
end

#load_parent_class(class_name) ⇒ 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.



35
36
37
38
39
# File 'lib/active_mocker/model_reader.rb', line 35

def load_parent_class(class_name)
  file_name = class_name.tableize.singularize
  source = ActiveMocker::RubyParse.new(read_file(file_name)).modify_parent_class('ActiveMocker::ActiveRecord::Base')
  eval_file(source, file_path(file_name))
end

#log_loading_error(msg, print_to_stdout = false) ⇒ 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.



62
63
64
65
66
67
# File 'lib/active_mocker/model_reader.rb', line 62

def log_loading_error(msg, print_to_stdout=false)
  main = "ActiveMocker :: Error loading Model: #{model_name} \n\t#{msg}\n"
  file = "\t#{file_path}\n"
  Logger.error main + file
  print main + file if print_to_stdout
end

#module_namespaceObject

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.



41
42
43
# File 'lib/active_mocker/model_reader.rb', line 41

def module_namespace
  @module ||= Module.new
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.



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

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

#parse(model_name) ⇒ 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.



14
15
16
17
18
19
# File 'lib/active_mocker/model_reader.rb', line 14

def parse(model_name)
  @model_name = model_name
  klass
  return self if @klass
  return false
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.



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

def primary_key
  klass.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.



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

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

#read_file(m_name = model_name) ⇒ 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.



73
74
75
# File 'lib/active_mocker/model_reader.rb', line 73

def read_file(m_name=model_name)
  file_reader.read(file_path(m_name))
end

#realObject

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.



69
70
71
# File 'lib/active_mocker/model_reader.rb', line 69

def real
  model_name.classify.constantize
end

#relationshipsObject

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.



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

def relationships
  relationships_types.to_h.values.flatten
end

#relationships_typesObject

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.



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

def relationships_types
  klass.relationships
end

#sandbox_modelObject

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.



25
26
27
28
29
30
31
32
33
# File 'lib/active_mocker/model_reader.rb', line 25

def sandbox_model
  source = ActiveMocker::RubyParse.new(read_file)
  if source.has_parent_class? && source.parent_class_name == 'ActiveRecord::Base'
    source.modify_parent_class('ActiveMocker::ActiveRecord::Base')
  else
    load_parent_class(source.parent_class_name)
    read_file
  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.



85
86
87
# File 'lib/active_mocker/model_reader.rb', line 85

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.



89
90
91
92
93
# File 'lib/active_mocker/model_reader.rb', line 89

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

#single_relationshipsObject

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.



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

def single_relationships
  klass.single_relationships.flatten.compact
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.



145
146
147
# File 'lib/active_mocker/model_reader.rb', line 145

def table_name
  klass.table_name
end