Skip to content

Commit

Permalink
MVP
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk committed Dec 1, 2023
1 parent 969c482 commit 2fa7a7b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
Empty file added .examples/empty.yaml
Empty file.
2 changes: 1 addition & 1 deletion .examples/example.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file:
yaml:
kek:
lol: 'text'
cheburek: 'chevurek'
Expand Down
4 changes: 0 additions & 4 deletions lib/active_yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ module ActiveYaml
# base
class Base < MethodMapper
def self.yaml(file_path)
define_method(:yaml_file_path) do
@yaml_file_path = yaml_file_path
end

define_method(:yaml_data) do
@yaml_data = Parser.parse(file_path)
end
Expand Down
32 changes: 32 additions & 0 deletions lib/active_yaml/base_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module ActiveYaml
# base doc
class BaseObject
attr_reader :hash

def initialize(hash)
@hash = hash
end

def method_missing(method, *args, &block)
method_name = method.to_s
if hash.key?(method_name)
value = hash[method_name]
return BaseObject.new(value) if value.is_a?(Hash)

value
else
super
end
end

def respond_to_missing?(method, include_private = false)
@hash.key?(method.to_s) || super
end

def inspect
hash
end
end
end
18 changes: 17 additions & 1 deletion lib/active_yaml/method_mapper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
# frozen_string_literal: true

require_relative 'base_object'

module ActiveYaml
# base mapper
class MethodMapper
def initialize; end
def method_missing(method, *args, &block)
method_name = method.to_s
if yaml_data[method_name]
value = yaml_data[method_name]
return value unless value.is_a?(Hash)

BaseObject.new(value)
else
super
end
end

def respond_to_missing?(method, include_private = false)
yaml_data[method.to_s] || super
end
end
end
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.4'
VERSION = '0.1.5'
end

0 comments on commit 2fa7a7b

Please sign in to comment.