From 75b1244f6437ccce707380b823ab781cee26236a 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: Sun, 3 Dec 2023 20:06:49 +0300 Subject: [PATCH] Minor update (#2) --- Gemfile | 1 + Gemfile.lock | 10 +++++++- README.md | 4 +-- lib/active_yaml.rb | 22 +++++++++++++--- lib/active_yaml/method_redirection.rb | 25 ------------------- lib/active_yaml/version.rb | 2 +- lib/active_yaml/yaml_hash.rb | 10 +++++--- .../{base_test.rb => base_model_test.rb} | 4 +-- test/lib/active_yaml/parser_test.rb | 15 +++++++++++ test/lib/active_yaml/varsion_test.rb | 11 ++++++++ test/test_helper.rb | 4 +++ 11 files changed, 69 insertions(+), 39 deletions(-) delete mode 100644 lib/active_yaml/method_redirection.rb rename test/lib/active_yaml/{base_test.rb => base_model_test.rb} (92%) create mode 100644 test/lib/active_yaml/parser_test.rb create mode 100644 test/lib/active_yaml/varsion_test.rb diff --git a/Gemfile b/Gemfile index c5d014d..86d0a45 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ gemspec group :development do gem 'byebug' gem 'minitest' + gem 'minitest-reporters' gem 'rubocop' gem 'rubocop-minitest' end diff --git a/Gemfile.lock b/Gemfile.lock index 371c5e4..b056903 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,17 +1,24 @@ PATH remote: . specs: - activeyaml (1.0.1) + activeyaml (1.1.1) psych (~> 5.1) GEM remote: https://rubygems.org/ specs: + ansi (1.5.0) ast (2.4.2) + builder (3.2.4) byebug (11.1.3) json (2.6.3) language_server-protocol (3.17.0.3) minitest (5.20.0) + minitest-reporters (1.6.1) + ansi + builder + minitest (>= 5.0) + ruby-progressbar parallel (1.23.0) parser (3.2.2.4) ast (~> 2.4.1) @@ -49,6 +56,7 @@ DEPENDENCIES activeyaml! byebug minitest + minitest-reporters rubocop rubocop-minitest diff --git a/README.md b/README.md index 067ba71..5c0f1a9 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Then, you could do the following: ```ruby require 'active_yaml' -class User < ActiveYaml::Base +class User < ActiveYaml::BaseModel yaml 'examples/example.yaml' end ``` @@ -64,7 +64,7 @@ h.is_a?(Hash) # true You can also use data from Yaml files inside your model. ```ruby -class User < ActiveYaml::Base +class User < ActiveYaml::BaseModel yaml 'examples/example.yaml' # The method will return "text" diff --git a/lib/active_yaml.rb b/lib/active_yaml.rb index da2e20a..4854694 100644 --- a/lib/active_yaml.rb +++ b/lib/active_yaml.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative 'active_yaml/parser' -require_relative 'active_yaml/method_redirection' +require_relative 'active_yaml/yaml_hash' require_relative 'active_yaml/factory_hash' # The main module from which work with gem begins @@ -9,13 +9,27 @@ module ActiveYaml extend FactoryHash # Base class to inherit from to create models - class Base - include MethodRedirection - + class BaseModel def self.yaml(file_path) define_method(:yaml_data) do @yaml_data = Parser.parse(file_path) end end + + def method_missing(method, *args, &) + value = yaml_data[method.to_s] + + if value + return value unless value.is_a?(Hash) + + YamlHash.new(value) + else + super + end + end + + def respond_to_missing?(method, include_private = false) + yaml_data[method.to_s] || super + end end end diff --git a/lib/active_yaml/method_redirection.rb b/lib/active_yaml/method_redirection.rb deleted file mode 100644 index 9f520d4..0000000 --- a/lib/active_yaml/method_redirection.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -require_relative 'yaml_hash' - -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, &) - value = yaml_data[method.to_s] - - if value - return value unless value.is_a?(Hash) - - YamlHash.new(value) - else - super - end - end - - def respond_to_missing?(method, include_private = false) - yaml_data[method.to_s] || super - end - end -end diff --git a/lib/active_yaml/version.rb b/lib/active_yaml/version.rb index c40475c..b0179e5 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.0.1' + VERSION = '1.1.1' end diff --git a/lib/active_yaml/yaml_hash.rb b/lib/active_yaml/yaml_hash.rb index 8917d6a..65dbfcf 100644 --- a/lib/active_yaml/yaml_hash.rb +++ b/lib/active_yaml/yaml_hash.rb @@ -9,6 +9,8 @@ def initialize(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] @@ -21,6 +23,10 @@ def method_missing(method, *args, &) end end + def respond_to_missing?(method, include_private = false) + hash.key?(method.to_s) || super + end + def inspect hash end @@ -28,9 +34,5 @@ def inspect def to_s hash.to_s end - - def respond_to_missing?(method, include_private = false) - hash.key?(method.to_s) || super - end end end diff --git a/test/lib/active_yaml/base_test.rb b/test/lib/active_yaml/base_model_test.rb similarity index 92% rename from test/lib/active_yaml/base_test.rb rename to test/lib/active_yaml/base_model_test.rb index 5787a40..9ceabec 100644 --- a/test/lib/active_yaml/base_test.rb +++ b/test/lib/active_yaml/base_model_test.rb @@ -2,7 +2,7 @@ require_relative '../../test_helper' -class SomeUser < ActiveYaml::Base +class SomeUser < ActiveYaml::BaseModel yaml '.examples/example.yaml' def some_method @@ -11,7 +11,7 @@ def some_method end module ActiveYaml - class BaseTest < Minitest::Test + class BaseModelTest < Minitest::Test def setup @user = SomeUser.new end diff --git a/test/lib/active_yaml/parser_test.rb b/test/lib/active_yaml/parser_test.rb new file mode 100644 index 0000000..762dbbd --- /dev/null +++ b/test/lib/active_yaml/parser_test.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require_relative '../../test_helper' + +module ActiveYaml + class ParserTest < Minitest::Test + def test_parse + expect_hash = { 'yaml' => { 'kek' => { 'lol' => 'text', 'cheburek' => 'cheburek', + 'users' => { 'first' => '1' } } } } + result = Parser.parse('.examples/example.yaml') + + assert_equal result, expect_hash + end + end +end diff --git a/test/lib/active_yaml/varsion_test.rb b/test/lib/active_yaml/varsion_test.rb new file mode 100644 index 0000000..9d5fd6d --- /dev/null +++ b/test/lib/active_yaml/varsion_test.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +require_relative '../../test_helper' + +module ActiveYaml + class VersionTest < Minitest::Test + def test_version + refute_nil VERSION + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 823eaf1..7dbc111 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,4 +2,8 @@ require 'minitest/autorun' require 'byebug' +require 'minitest/reporters' require_relative '../lib/active_yaml' +require_relative '../lib/active_yaml/version' + +Minitest::Reporters.use!