Class: ActiveMocker::ModelReader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ModelReader



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

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

Instance Attribute Details

#file_readerObject (readonly)

Returns the value of attribute file_reader.



5
6
7
# File 'lib/active_mocker/model_reader.rb', line 5

def file_reader
  @file_reader
end

#model_dirObject (readonly)

Returns the value of attribute model_dir.



5
6
7
# File 'lib/active_mocker/model_reader.rb', line 5

def model_dir
  @model_dir
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



5
6
7
# File 'lib/active_mocker/model_reader.rb', line 5

def model_name
  @model_name
end

Instance Method Details

#class_methodsObject



46
47
48
# File 'lib/active_mocker/model_reader.rb', line 46

def class_methods
  klass.methods(false)
end

#class_methods_with_argumentsObject



50
51
52
53
54
# File 'lib/active_mocker/model_reader.rb', line 50

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

#eval_fileObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_mocker/model_reader.rb', line 22

def eval_file
  failure = true
  while failure
    begin
      m = Module.new
      m.module_eval(read_file)
    rescue NameError => e
      raise e
      result = e.to_s.scan /::(\w*)$/ # gets the Constant name from error
      const_name = result.flatten.first
      Logger_.debug "ActiveMocker :: Can't can't find Constant #{const_name} from class #{model_name}..\n #{caller}"
      # Object.const_set(const_name,const_name)
      next
    end
    failure = false
    model = m.const_get m.constants.first
  end
  model
end

#instance_methodsObject



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

def instance_methods
  klass.public_instance_methods(false)
end

#instance_methods_with_argumentsObject



56
57
58
59
60
# File 'lib/active_mocker/model_reader.rb', line 56

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

#klassObject



18
19
20
# File 'lib/active_mocker/model_reader.rb', line 18

def klass
  @klass ||= eval_file
end

#parse(model_name) ⇒ Object



12
13
14
15
16
# File 'lib/active_mocker/model_reader.rb', line 12

def parse(model_name)
  @model_name = model_name
  klass
  self
end

#read_fileObject



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

def read_file
  file_reader.read("#{model_dir}/#{model_name}.rb")
end

#relationshipsObject



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

def relationships
  relationships_types.to_h.values.flatten
end

#relationships_typesObject



66
67
68
# File 'lib/active_mocker/model_reader.rb', line 66

def relationships_types
  klass.relationships
end