Skip to content

Commit

Permalink
fixes of some problems, as well as more thorough test coverage (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk committed Dec 27, 2023
1 parent efb93d9 commit 18d6fad
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .examples/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ start:
lol: 'text'
cheburek: 'cheburek'
users:
first: '1'
- 'Kirill'
- 'Ekaterina'
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
activeyaml (1.2.1)
activeyaml (1.2.2)
psych (~> 5.1)

GEM
Expand Down
2 changes: 1 addition & 1 deletion lib/active_yaml/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ActiveYaml
VERSION = '1.2.1'
VERSION = '1.2.2'
end
17 changes: 11 additions & 6 deletions lib/active_yaml/yaml_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
6 changes: 4 additions & 2 deletions test/lib/active_yaml/base_model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions test/lib/active_yaml/class_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/lib/active_yaml/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 44 additions & 2 deletions test/lib/active_yaml/yaml_hash_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}

Expand All @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion test/lib/active_yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 18d6fad

Please sign in to comment.