Sha256: 8fc933c7be09d1e98a15317a3e83066040899537c46558107e629ff9eba012ae

Contents?: true

Size: 1.74 KB

Versions: 22

Compression:

Stored size: 1.74 KB

Contents

module ActiveMocker

  class ModelReader

    attr_reader :model_name, :model_dir, :file_reader

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

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

    def klass
      @klass ||= eval_file
    end

    def eval_file
      failure = true
      while failure
        begin
          m = Module.new
          m.module_eval(read_file, file_path)
        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}"
          next
        end
        failure = false
        model = m.const_get m.constants.first
      end
      model
    end

    def read_file
      file_reader.read(file_path)
    end

    def file_path
      "#{model_dir}/#{model_name}.rb"
    end

    def class_methods
      klass.methods(false)
    end

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

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

    def instance_methods
      klass.public_instance_methods(false)
    end

    def relationships_types
      klass.relationships
    end

    def relationships
      relationships_types.to_h.values.flatten
    end

    def collections
      klass.collections.flatten.compact
    end

    def single_relationships
      klass.single_relationships.flatten.compact
    end

  end

end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
active_mocker-1.3.2 lib/active_mocker/model_reader.rb
active_mocker-1.3.1 lib/active_mocker/model_reader.rb
active_mocker-1.3 lib/active_mocker/model_reader.rb
active_mocker-1.2.4 lib/active_mocker/model_reader.rb
active_mocker-1.2.3 lib/active_mocker/model_reader.rb
active_mocker-1.2 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.11 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.10 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.9 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.8 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.7 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.6 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.5 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.4 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.3 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.2 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre.1 lib/active_mocker/model_reader.rb
active_mocker-1.2.pre lib/active_mocker/model_reader.rb
active_mocker-1.1.23 lib/active_mocker/model_reader.rb
active_mocker-1.1.22 lib/active_mocker/model_reader.rb