, 여기에 내가 경우 사람이 관심에와 결국 무엇을 : 나는 검색 모델과 하위 모델에 표현되는 모든 데이터와 하이브리드 접근 방식과 연결가는 결국
lib 폴더의 모듈에있는 타사 사이트의 클라이언트.
모델/search.rb :
class Search
def initialize(params)
# code to start a new Search on third party site
@result = SearchApi.example_query(data_type_1_param)
# more code...
end
# other code to assist in parsing of search results
end
모델/검색/data_type_1.rb :
class Search::DataType1 < Hash
def initialize(DataModel1)
# code to convert DataModel1 to DataType1 for sending request
end
end
나는 또한 DataType1 유사하게 여러 가지 다른 검색 하위 모델을 가지고
. 또한 Search 하위 모델을 만들어 프로그램에서 사용하기 쉽도록 Search에 의해 반환 된 데이터를 나타내고 추상화 계층을 만들었습니다.
모델/검색/results.rb :이
class Search::Results
def initialize(result_hash)
@data = result_hash
end
def field_1
# code to parse and display field 1 from @data hash
end
def field_2
# code to parse and display field 2 from @data hash
end
#.... etc
end
검색 모델 자체가 완전히 필요하지 않을 수도있다 (대부분의 코드는 모듈과 하위 모델입니다)하지만,이 MVC에 편리하게 맞는 검색을 만들고 표시하기위한 프레임 워크.
마지막으로 제 3 자 사이트에 실제로 연락하여 검색을 수행하는 클라이언트 클래스가있는 모듈을 만들었습니다. 검색 모델에이 기능을 포함시킬 수는 있었지만 여러 가지 이유로이 클라이언트를 유지해야하고 여러 검색을 처리해야하는 반면 검색 모델은 모든 쿼리에 대해 다시 만들어야했습니다.
lib/search_api.RB :
require 'search_api/client'
module SearchApi
class << self
def client
@client ||= SearchApi::Client.new()
@client
end
def example_query(data)
results = client.query(formatted_data)
# among other code
end
# other code to perform validations and interact with client
end
end
lib 디렉토리/search_api/client.rb :
module SearchApi
class Client
include HTTParty
# code to create and handle connection to Search site
end
end
이 방법은 모든 모범 사례를 따라 상황이 많이 대한 아마 잔인한 사람이지만, 운동 듯하지 않을 수 있습니다 나는 문제를 해결하기 위해 노력하고 있었다. 누군가가 리팩터링에 대한 더 좋은 아이디어를 가지고 있다면, 나는 모두 귀입니다.