diff --git a/.gitignore b/.gitignore index cba43f7..e2bd271 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .DS_Store -.byebug_history +.byebug_history \ No newline at end of file diff --git a/.rubocop.yml b/.rubocop.yml index 848f06b..6d41c9a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,8 @@ require: - rubocop-minitest + +AllCops: + NewCops: enable + +Gemspec/RequireMFA: + Enabled: false diff --git a/Gemfile b/Gemfile index d47ce0e..c5d014d 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,9 @@ source 'https://rubygems.org' gemspec -gem 'byebug' -gem 'minitest' -gem 'rubocop' -gem 'rubocop-minitest' +group :development do + gem 'byebug' + gem 'minitest' + gem 'rubocop' + gem 'rubocop-minitest' +end diff --git a/Gemfile.lock b/Gemfile.lock index b083624..371c5e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - activeyaml (0.1.6) + activeyaml (1.0.1) psych (~> 5.1) GEM diff --git a/README.md b/README.md index 142037e..067ba71 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,16 @@ user = User.new user.yaml.kek.lol # output: 'text' user.yaml.kek.users.first # output: '1' ``` -If you complete the chain not to the final value. You will get an object like a hash. It won't respond to []. However, you can use a dot to go through the values further. You can also use data from Yaml files inside your model. + +If you complete the chain not to the final value. You will get an object like a hash. It won't respond to []. However, you can use a dot to go through the values further. This object also has a “hash” method, which returns the current received hash. + +```ruby +h = user.yaml.kek.hash +h == {'lok' => 'text'} # true +h.is_a?(Hash) # true +``` + +You can also use data from Yaml files inside your model. ```ruby class User < ActiveYaml::Base diff --git a/lib/active_yaml/method_redirection.rb b/lib/active_yaml/method_redirection.rb index 36636c1..9f520d4 100644 --- a/lib/active_yaml/method_redirection.rb +++ b/lib/active_yaml/method_redirection.rb @@ -6,7 +6,7 @@ module ActiveYaml # A module with methods for implementing tracking methods. # In the model in order to proxy them into a hash with YML data module MethodRedirection - def method_missing(method, *args, &block) + def method_missing(method, *args, &) value = yaml_data[method.to_s] if value diff --git a/lib/active_yaml/version.rb b/lib/active_yaml/version.rb index de94ffd..c40475c 100644 --- a/lib/active_yaml/version.rb +++ b/lib/active_yaml/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module ActiveYaml - VERSION = '0.1.6' + VERSION = '1.0.1' end diff --git a/lib/active_yaml/yaml_hash.rb b/lib/active_yaml/yaml_hash.rb index 3fcffb8..8917d6a 100644 --- a/lib/active_yaml/yaml_hash.rb +++ b/lib/active_yaml/yaml_hash.rb @@ -3,11 +3,13 @@ module ActiveYaml # Class for creating hashes of similar objects class YamlHash + attr_reader :hash + def initialize(hash) @hash = hash || {} end - def method_missing(method, *args, &block) + def method_missing(method, *args, &) value = hash[method.to_s] if value @@ -30,9 +32,5 @@ def to_s def respond_to_missing?(method, include_private = false) hash.key?(method.to_s) || super end - - private - - attr_reader :hash end end diff --git a/test/lib/active_yaml/base_test.rb b/test/lib/active_yaml/base_test.rb index 61230b6..5787a40 100644 --- a/test/lib/active_yaml/base_test.rb +++ b/test/lib/active_yaml/base_test.rb @@ -19,21 +19,23 @@ def setup # rubocop:disable Naming/VariableNumber def test_call_chain result_1 = @user.yaml.kek.lol - assert_equal result_1, 'text' + + assert_equal 'text', result_1 result_2 = @user.yaml.kek.users.first - assert_equal result_2, '1' + + assert_equal '1', result_2 end # rubocop:enable Naming/VariableNumber def test_some_method - assert_equal @user.some_method, 'text' + assert_equal 'text', @user.some_method end def test_incomplete_call result = @user.yaml.kek - assert_equal result.is_a?(ActiveYaml::YamlHash), true + assert result.is_a?(ActiveYaml::YamlHash) end def test_yaml_data diff --git a/test/lib/active_yaml/yaml_hash_test.rb b/test/lib/active_yaml/yaml_hash_test.rb new file mode 100644 index 0000000..5c00ffe --- /dev/null +++ b/test/lib/active_yaml/yaml_hash_test.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require_relative '../../test_helper' + +module ActiveYaml + class YamlHashTest < Minitest::Test + def setup + @hash = { + 'key1' => { + 'key2' => + { + 'key3' => 'value1', + 'key4' => 'vaalue2' + } + } + } + + @yaml_hash = YamlHash.new(@hash) + end + + def test_hash + assert_equal @yaml_hash.hash, @hash + end + + def test_call_chain + result = @yaml_hash.key1.key2.key3 + + assert_equal 'value1', result + end + + def test_not_full_call_chain + result = @yaml_hash.key1.key2 + + assert result.is_a?(ActiveYaml::YamlHash) + end + end +end diff --git a/test/lib/active_yaml_test.rb b/test/lib/active_yaml_test.rb index e4e2e0b..3a599ef 100644 --- a/test/lib/active_yaml_test.rb +++ b/test/lib/active_yaml_test.rb @@ -11,20 +11,22 @@ def setup # rubocop:disable Naming/VariableNumber def test_call_chain result_1 = @test_data.yaml.kek.lol - assert_equal result_1, 'text' + + assert_equal 'text', result_1 result_2 = @test_data.yaml.kek.users.first - assert_equal result_2, '1' + + assert_equal '1', result_2 end # rubocop:enable Naming/VariableNumber def test_incomplete_call result = @test_data.yaml.kek - assert_equal result.is_a?(ActiveYaml::YamlHash), true + assert result.is_a?(ActiveYaml::YamlHash) end def test_empty_file - assert_equal @test_empty_data.is_a?(ActiveYaml::YamlHash), true + assert @test_empty_data.is_a?(ActiveYaml::YamlHash) end end