Class: ActiveMocker::ModelReader
- Inherits:
-
Object
- Object
- ActiveMocker::ModelReader
- Defined in:
- lib/active_mocker/model_reader.rb
Instance Attribute Summary collapse
-
#file_reader ⇒ Object
readonly
Returns the value of attribute file_reader.
-
#model_dir ⇒ Object
readonly
Returns the value of attribute model_dir.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
Instance Method Summary collapse
- #belongs_to ⇒ Object
- #class_methods ⇒ Object
- #class_methods_with_arguments ⇒ Object
- #collections ⇒ Object
- #constants ⇒ Object
- #eval_file ⇒ Object
- #file_path ⇒ Object
- #has_and_belongs_to_many ⇒ Object
- #has_many ⇒ Object
- #has_one ⇒ Object
-
#initialize(options = {}) ⇒ ModelReader
constructor
A new instance of ModelReader.
- #instance_methods ⇒ Object
- #instance_methods_with_arguments ⇒ Object
- #klass ⇒ Object
- #parse(model_name) ⇒ Object
- #primary_key ⇒ Object
- #read_file ⇒ Object
- #relationships ⇒ Object
- #relationships_types ⇒ Object
- #single_relationships ⇒ Object
- #table_name ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ModelReader
Returns a new instance of ModelReader.
7 8 9 10 |
# File 'lib/active_mocker/model_reader.rb', line 7 def initialize(={}) @file_reader = [:file_reader] ||= FileReader @model_dir = [:model_dir] end |
Instance Attribute Details
#file_reader ⇒ Object (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_dir ⇒ Object (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_name ⇒ Object (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
#belongs_to ⇒ Object
81 82 83 |
# File 'lib/active_mocker/model_reader.rb', line 81 def belongs_to klass.relationships.belongs_to end |
#class_methods ⇒ Object
45 46 47 |
# File 'lib/active_mocker/model_reader.rb', line 45 def class_methods klass.methods(false) end |
#class_methods_with_arguments ⇒ Object
49 50 51 52 53 |
# File 'lib/active_mocker/model_reader.rb', line 49 def class_methods_with_arguments class_methods.map do |m| {m => klass.method(m).parameters } end end |
#collections ⇒ Object
73 74 75 |
# File 'lib/active_mocker/model_reader.rb', line 73 def collections klass.collections.flatten.compact end |
#constants ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/active_mocker/model_reader.rb', line 105 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 ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_mocker/model_reader.rb', line 23 def eval_file failure = false begin m = Module.new m.module_eval(read_file, file_path) rescue Exception => e Logger.error "ActiveMocker :: Error loading Model: #{model_name} \n \t\t\t\t\t\t\t\t`#{e}` \n" Logger.error "\t\t\t\t\t\t\t\t #{file_path}\n" failure = true end return m.const_get m.constants.first unless failure return false end |
#file_path ⇒ Object
41 42 43 |
# File 'lib/active_mocker/model_reader.rb', line 41 def file_path "#{model_dir}/#{model_name}.rb" end |
#has_and_belongs_to_many ⇒ Object
89 90 91 |
# File 'lib/active_mocker/model_reader.rb', line 89 def has_and_belongs_to_many klass.relationships.has_and_belongs_to_many end |
#has_many ⇒ Object
93 94 95 |
# File 'lib/active_mocker/model_reader.rb', line 93 def has_many klass.relationships.has_many end |
#has_one ⇒ Object
85 86 87 |
# File 'lib/active_mocker/model_reader.rb', line 85 def has_one klass.relationships.has_one end |
#instance_methods ⇒ Object
61 62 63 |
# File 'lib/active_mocker/model_reader.rb', line 61 def instance_methods klass.public_instance_methods(false) end |
#instance_methods_with_arguments ⇒ Object
55 56 57 58 59 |
# File 'lib/active_mocker/model_reader.rb', line 55 def instance_methods_with_arguments instance_methods.map do |m| {m => klass.new.method(m).parameters } end end |
#klass ⇒ Object
19 20 21 |
# File 'lib/active_mocker/model_reader.rb', line 19 def klass @klass ||= eval_file end |
#parse(model_name) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/active_mocker/model_reader.rb', line 12 def parse(model_name) @model_name = model_name klass return self unless @klass == false return false end |
#primary_key ⇒ Object
101 102 103 |
# File 'lib/active_mocker/model_reader.rb', line 101 def primary_key klass.primary_key end |
#read_file ⇒ Object
37 38 39 |
# File 'lib/active_mocker/model_reader.rb', line 37 def read_file file_reader.read(file_path) end |
#relationships ⇒ Object
69 70 71 |
# File 'lib/active_mocker/model_reader.rb', line 69 def relationships relationships_types.to_h.values.flatten end |
#relationships_types ⇒ Object
65 66 67 |
# File 'lib/active_mocker/model_reader.rb', line 65 def relationships_types klass.relationships end |
#single_relationships ⇒ Object
77 78 79 |
# File 'lib/active_mocker/model_reader.rb', line 77 def single_relationships klass.single_relationships.flatten.compact end |
#table_name ⇒ Object
97 98 99 |
# File 'lib/active_mocker/model_reader.rb', line 97 def table_name klass.table_name end |