-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
121 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
yaml: | ||
start: | ||
kek: | ||
lol: 'text' | ||
cheburek: 'cheburek' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
activeyaml (1.1.1) | ||
activeyaml (1.2.0) | ||
psych (~> 5.1) | ||
|
||
GEM | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'active_yaml/parser' | ||
require_relative 'active_yaml/yaml_hash' | ||
require_relative 'active_yaml/method_mapper' | ||
require_relative 'active_yaml/factory_hash' | ||
require_relative 'active_yaml/class_methods' | ||
|
||
# The main module from which work with gem begins | ||
module ActiveYaml | ||
extend FactoryHash | ||
|
||
# Base class to inherit from to create models | ||
class BaseModel | ||
include MethodMapper | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'method_mapper' | ||
require_relative 'parser' | ||
|
||
module ActiveYaml | ||
# no doc | ||
module ClassMethods | ||
include MethodMapper | ||
|
||
def yaml(file_path) | ||
define_singleton_method(:yaml_data) do | ||
@yaml_data = Parser.parse(file_path) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'yaml_hash' | ||
|
||
module ActiveYaml | ||
# no doc | ||
module MethodMapper | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActiveYaml | ||
VERSION = '1.1.1' | ||
VERSION = '1.2.0' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
class SomeUser | ||
extend ActiveYaml::ClassMethods | ||
yaml '.examples/example.yaml' | ||
end | ||
|
||
module ActiveYaml | ||
class ClassMethodsTest < Minitest::Test | ||
# rubocop:disable Naming/VariableNumber | ||
def test_call_chain | ||
result_1 = SomeUser.start.kek.lol | ||
|
||
assert_equal 'text', result_1 | ||
|
||
result_2 = SomeUser.start.kek.users.first | ||
|
||
assert_equal '1', result_2 | ||
end | ||
# rubocop:enable Naming/VariableNumber | ||
|
||
def test_incomplete_call | ||
result = SomeUser.start.kek | ||
|
||
assert result.is_a?(ActiveYaml::YamlHash) | ||
end | ||
|
||
def test_yaml_data | ||
expect_hash = { 'start' => { 'kek' => { 'lol' => 'text', 'cheburek' => 'cheburek', | ||
'users' => { 'first' => '1' } } } } | ||
|
||
assert_equal SomeUser.yaml_data, expect_hash | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters