Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor update #2

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gemspec
group :development do
gem 'byebug'
gem 'minitest'
gem 'minitest-reporters'
gem 'rubocop'
gem 'rubocop-minitest'
end
10 changes: 9 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -49,6 +56,7 @@ DEPENDENCIES
activeyaml!
byebug
minitest
minitest-reporters
rubocop
rubocop-minitest

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand All @@ -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"
Expand Down
22 changes: 18 additions & 4 deletions lib/active_yaml.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# 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
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
25 changes: 0 additions & 25 deletions lib/active_yaml/method_redirection.rb

This file was deleted.

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.0.1'
VERSION = '1.1.1'
end
10 changes: 6 additions & 4 deletions lib/active_yaml/yaml_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -21,16 +23,16 @@ 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

def to_s
hash.to_s
end

def respond_to_missing?(method, include_private = false)
hash.key?(method.to_s) || super
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative '../../test_helper'

class SomeUser < ActiveYaml::Base
class SomeUser < ActiveYaml::BaseModel
yaml '.examples/example.yaml'

def some_method
Expand All @@ -11,7 +11,7 @@ def some_method
end

module ActiveYaml
class BaseTest < Minitest::Test
class BaseModelTest < Minitest::Test
def setup
@user = SomeUser.new
end
Expand Down
15 changes: 15 additions & 0 deletions test/lib/active_yaml/parser_test.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions test/lib/active_yaml/varsion_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!