-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from AMHOL/feature/nazrin-struct
Add Nazrin::DataAccessor::Struct
- Loading branch information
Showing
8 changed files
with
296 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require 'aws-sdk-cloudsearch' | ||
|
||
module Nazrin | ||
class DataAccessor | ||
class Struct < Nazrin::DataAccessor | ||
class MissingDomainNameConfigError < StandardError; end | ||
|
||
class << self | ||
attr_reader :config | ||
|
||
def [](config) | ||
Class.new(self).tap do |clazz| | ||
clazz.instance_variable_set(:@config, config) | ||
end | ||
end | ||
|
||
def transform_attributes(attributes) | ||
attributes.each_with_object({}) do |(name, value), hash| | ||
type = field_types[name] | ||
|
||
if type.end_with?('array') | ||
hash[name] = value | ||
else | ||
hash[name] = value.first | ||
end | ||
end | ||
end | ||
|
||
def field_types | ||
return @field_types if defined?(@field_types) | ||
|
||
response = cloudsearch_client.describe_index_fields( | ||
domain_name: config.domain_name | ||
) | ||
|
||
@field_types = response.index_fields.each_with_object({}) do |field, fields| | ||
name = field.options[:index_field_name] | ||
type = field.options[:index_field_type] | ||
|
||
fields[name] = type | ||
end | ||
end | ||
|
||
private | ||
|
||
def cloudsearch_client | ||
@cloudsearch_client ||= Aws::CloudSearch::Client.new( | ||
access_key_id: config.access_key_id, | ||
secret_access_key: config.secret_access_key, | ||
logger: config.logger | ||
) | ||
end | ||
end | ||
|
||
def load_all(data) | ||
data.map do |attributes| | ||
model.new(attributes) | ||
end | ||
end | ||
|
||
def data_from_response(res) | ||
res.data[:hits][:hit].map do |hit| | ||
self.class.transform_attributes(hit[:fields]) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'spec_helper' | ||
|
||
describe Nazrin::DataAccessor::Struct do | ||
let(:clazz) do | ||
Class.new do | ||
def self.name; 'CustomStruct'; end | ||
include Nazrin::Searchable | ||
|
||
attr_reader :attributes | ||
|
||
searchable_configure do |config| | ||
config.domain_name = 'my-domain-name' | ||
end | ||
|
||
def initialize(attributes) | ||
@attributes = attributes | ||
end | ||
end | ||
end | ||
let(:data_accessor) { Nazrin::DataAccessor.for(clazz) } | ||
|
||
it do | ||
expect(data_accessor).to be < Nazrin::DataAccessor::Struct | ||
end | ||
|
||
describe '.field_types' do | ||
let(:cs_client) do | ||
instance_double(Aws::CloudSearch::Client) | ||
end | ||
let(:index_fields_response) { double(index_fields: index_fields) } | ||
let(:index_fields) do | ||
[id_field, name_field, tags_field] | ||
end | ||
let(:id_field) do | ||
double( | ||
options: { | ||
index_field_name: 'id', | ||
index_field_type: 'int' | ||
} | ||
) | ||
end | ||
let(:name_field) do | ||
double( | ||
options: { | ||
index_field_name: 'name', | ||
index_field_type: 'text' | ||
} | ||
) | ||
end | ||
let(:tags_field) do | ||
double( | ||
options: { | ||
index_field_name: 'tags', | ||
index_field_type: 'literal-array' | ||
} | ||
) | ||
end | ||
|
||
before do | ||
allow(Aws::CloudSearch::Client).to receive(:new).and_return( | ||
cs_client | ||
) | ||
allow(cs_client).to receive(:describe_index_fields).and_return( | ||
index_fields_response | ||
) | ||
end | ||
|
||
it do | ||
expect(Aws::CloudSearch::Client).to receive(:new).with( | ||
access_key_id: Nazrin.config.access_key_id, | ||
secret_access_key: Nazrin.config.secret_access_key, | ||
logger: Nazrin.config.logger | ||
) | ||
expect(cs_client).to receive(:describe_index_fields).with( | ||
domain_name: 'my-domain-name' | ||
) | ||
expect(data_accessor.field_types).to eq( | ||
'id' => 'int', | ||
'name' => 'text', | ||
'tags' => 'literal-array' | ||
) | ||
end | ||
end | ||
|
||
describe '.transform_attributes' do | ||
let(:attributes) do | ||
{ | ||
'id' => ['1'], | ||
'name' => ['Michael'], | ||
'tags' => ['one', 'two', 'three'] | ||
} | ||
end | ||
|
||
before do | ||
allow(data_accessor).to receive(:field_types).and_return( | ||
'id' => 'int', | ||
'name' => 'text', | ||
'tags' => 'literal-array' | ||
) | ||
end | ||
|
||
it do | ||
expect(data_accessor.transform_attributes(attributes)).to eq( | ||
'id' => '1', | ||
'name' => 'Michael', | ||
'tags' => ['one', 'two', 'three'] | ||
) | ||
end | ||
end | ||
end |
Oops, something went wrong.