Skip to content

Commit

Permalink
Major update (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk committed Dec 3, 2023
1 parent 2c97f9f commit 5c88af6
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.DS_Store
.byebug_history
.byebug_history
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
require:
- rubocop-minitest

AllCops:
NewCops: enable

Gemspec/RequireMFA:
Enabled: false
10 changes: 6 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 (0.1.6)
activeyaml (1.0.1)
psych (~> 5.1)

GEM
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/active_yaml/method_redirection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 = '0.1.6'
VERSION = '1.0.1'
end
8 changes: 3 additions & 5 deletions lib/active_yaml/yaml_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
10 changes: 6 additions & 4 deletions test/lib/active_yaml/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions test/lib/active_yaml/yaml_hash_test.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions test/lib/active_yaml_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 5c88af6

Please sign in to comment.