From 18d6fad652bb112c7699c7f69108e6921108e263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B8=D1=80=D0=B8=D0=BB=D0=BB=20=D0=9B=D0=B5=D0=BE?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2?= <71232234+leonovk@users.noreply.github.com> Date: Wed, 27 Dec 2023 19:15:50 +0300 Subject: [PATCH] fixes of some problems, as well as more thorough test coverage (#7) --- .examples/example.yaml | 3 +- Gemfile.lock | 2 +- lib/active_yaml/version.rb | 2 +- lib/active_yaml/yaml_hash.rb | 17 +++++--- test/lib/active_yaml/base_model_test.rb | 6 ++- test/lib/active_yaml/class_methods_test.rb | 6 ++- test/lib/active_yaml/parser_test.rb | 2 +- test/lib/active_yaml/yaml_hash_test.rb | 46 +++++++++++++++++++++- test/lib/active_yaml_test.rb | 4 +- 9 files changed, 71 insertions(+), 17 deletions(-) diff --git a/.examples/example.yaml b/.examples/example.yaml index 9b66181..00b9fc0 100644 --- a/.examples/example.yaml +++ b/.examples/example.yaml @@ -3,4 +3,5 @@ start: lol: 'text' cheburek: 'cheburek' users: - first: '1' + - 'Kirill' + - 'Ekaterina' diff --git a/Gemfile.lock b/Gemfile.lock index 91f696a..873dc85 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - activeyaml (1.2.1) + activeyaml (1.2.2) psych (~> 5.1) GEM diff --git a/lib/active_yaml/version.rb b/lib/active_yaml/version.rb index c94f536..1389aa7 100644 --- a/lib/active_yaml/version.rb +++ b/lib/active_yaml/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActiveYaml - VERSION = '1.2.1' + VERSION = '1.2.2' end diff --git a/lib/active_yaml/yaml_hash.rb b/lib/active_yaml/yaml_hash.rb index 65dbfcf..d63b406 100644 --- a/lib/active_yaml/yaml_hash.rb +++ b/lib/active_yaml/yaml_hash.rb @@ -3,16 +3,21 @@ module ActiveYaml # Class for creating hashes of similar objects class YamlHash - attr_reader :hash - def initialize(hash) @hash = hash || {} end + # The method is used to obtain a hash from an incomplete call chain. + # By default, if there is a value from a method of the same name, it will be returned. + # Otherwise the current hash will be returned + def hash + @hash['hash'] || @hash + end + # The main logic of this class is implemented in this method. # Allows you to filter method calls and redirect them to a hash by key def method_missing(method, *args, &) - value = hash[method.to_s] + value = @hash[method.to_s] if value return self.class.new(value) if value.is_a?(Hash) @@ -24,15 +29,15 @@ def method_missing(method, *args, &) end def respond_to_missing?(method, include_private = false) - hash.key?(method.to_s) || super + @hash.key?(method.to_s) || super end def inspect - hash + @hash end def to_s - hash.to_s + @hash.to_s end end end diff --git a/test/lib/active_yaml/base_model_test.rb b/test/lib/active_yaml/base_model_test.rb index e04674b..bdaf41a 100644 --- a/test/lib/active_yaml/base_model_test.rb +++ b/test/lib/active_yaml/base_model_test.rb @@ -24,7 +24,9 @@ def test_call_chain result_2 = @user.start.kek.users.first - assert_equal '1', result_2 + assert_equal 'Kirill', result_2 + + assert @user.start.kek.users.is_a?(Array) end # rubocop:enable Naming/VariableNumber @@ -40,7 +42,7 @@ def test_incomplete_call def test_yaml_data expect_hash = { 'start' => { 'kek' => { 'lol' => 'text', 'cheburek' => 'cheburek', - 'users' => { 'first' => '1' } } } } + 'users' => %w[Kirill Ekaterina] } } } assert_equal @user.yaml_data, expect_hash end diff --git a/test/lib/active_yaml/class_methods_test.rb b/test/lib/active_yaml/class_methods_test.rb index fd3d9e1..2ffeceb 100644 --- a/test/lib/active_yaml/class_methods_test.rb +++ b/test/lib/active_yaml/class_methods_test.rb @@ -17,7 +17,9 @@ def test_call_chain result_2 = SomeUser.start.kek.users.first - assert_equal '1', result_2 + assert_equal 'Kirill', result_2 + + assert SomeUser.start.kek.users.is_a?(Array) end # rubocop:enable Naming/VariableNumber @@ -29,7 +31,7 @@ def test_incomplete_call def test_yaml_data expect_hash = { 'start' => { 'kek' => { 'lol' => 'text', 'cheburek' => 'cheburek', - 'users' => { 'first' => '1' } } } } + 'users' => %w[Kirill Ekaterina] } } } assert_equal SomeUser.yaml_data, expect_hash end diff --git a/test/lib/active_yaml/parser_test.rb b/test/lib/active_yaml/parser_test.rb index d31acf7..8e15267 100644 --- a/test/lib/active_yaml/parser_test.rb +++ b/test/lib/active_yaml/parser_test.rb @@ -6,7 +6,7 @@ module ActiveYaml class ParserTest < Minitest::Test def test_parse expect_hash = { 'start' => { 'kek' => { 'lol' => 'text', 'cheburek' => 'cheburek', - 'users' => { 'first' => '1' } } } } + 'users' => %w[Kirill Ekaterina] } } } result = Parser.parse('.examples/example.yaml') assert_equal result, expect_hash diff --git a/test/lib/active_yaml/yaml_hash_test.rb b/test/lib/active_yaml/yaml_hash_test.rb index 5c00ffe..329b360 100644 --- a/test/lib/active_yaml/yaml_hash_test.rb +++ b/test/lib/active_yaml/yaml_hash_test.rb @@ -4,14 +4,24 @@ module ActiveYaml class YamlHashTest < Minitest::Test - def setup + def setup # rubocop:disable Metrics/MethodLength @hash = { 'key1' => { 'key2' => { 'key3' => 'value1', - 'key4' => 'vaalue2' + 'key4' => 'value2', + 'hash' => %w[el1 el2] + }, + 'key8' => { + 'hash' => { + 'key5' => 'value3' } + }, + 'hash' => 'hash_value' + }, + 'key6' => { + 'key7' => 'value4' } } @@ -20,6 +30,16 @@ def setup def test_hash assert_equal @yaml_hash.hash, @hash + + assert @yaml_hash.hash.is_a?(Hash) + end + + def test_call_chain_hash + result = @yaml_hash.key6.hash + + assert_equal result, @hash['key6'] + + assert result.is_a?(Hash) end def test_call_chain @@ -33,5 +53,27 @@ def test_not_full_call_chain assert result.is_a?(ActiveYaml::YamlHash) end + + def test_when_hash_is_value + result = @yaml_hash.key1.hash + + assert_equal 'hash_value', result + end + + def test_when_hash_is_hash + result = @yaml_hash.key1.key8.hash + + assert_equal result, @hash['key1']['key8']['hash'] + + assert result.is_a?(Hash) + end + + def test_when_hash_is_array + result = @yaml_hash.key1.key2.hash + + assert_equal result, @hash['key1']['key2']['hash'] + + assert result.is_a?(Array) + end end end diff --git a/test/lib/active_yaml_test.rb b/test/lib/active_yaml_test.rb index cf717e3..27e3705 100644 --- a/test/lib/active_yaml_test.rb +++ b/test/lib/active_yaml_test.rb @@ -18,7 +18,9 @@ def test_call_chain result_2 = @test_data.start.kek.users.first - assert_equal '1', result_2 + assert_equal 'Kirill', result_2 + + assert @test_data.start.kek.users.is_a?(Array) end # rubocop:enable Naming/VariableNumber