Skip to content

Commit

Permalink
Fix Marshaller interface to copy extension names to a subclass
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
piotrmurach committed Apr 5, 2021
1 parent b0494b0 commit 6e9f101
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,9 @@
### Changed
* Change #delete to allow removing any subkey of a deeply nested key

### Fixed
* Fix Marshaller interface to copy extension names to a subclass

## [v0.4.0] - 2020-01-25

### Added
Expand Down
16 changes: 13 additions & 3 deletions lib/tty/config/marshaller.rb
Expand Up @@ -16,7 +16,7 @@ def self.included(base)

module ExtensionsStore
def ext
@ext ||= []
@_ext ||= []
end

# Set a list of extensions
Expand All @@ -27,11 +27,21 @@ def ext
# @api public
def extension(*extensions)
if extensions[0].is_a?(Array)
@ext = extensions[0]
@_ext = extensions[0]
else
@ext = extensions
@_ext = extensions
end
end

# Copy extensions to a subclass
#
# @param [Object] subclass
#
# @api private
def inherited(subclass)
super
subclass.instance_variable_set(:@_ext, @_ext.dup)
end
end

# Marshal object into a given format
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/register_marshaller_spec.rb
Expand Up @@ -60,4 +60,27 @@ def unmarshal(file); end
TTY::Config::Marshallers::JavaPropsMarshaller
])
end

it "inherits from existing marshaller" do
stub_const("CustomYAMLMarshaller",
Class.new(TTY::Config::Marshallers::YAMLMarshaller) do
def marshal(data)
YAML.safe_load(data, aliases: true)
end
end)

config = TTY::Config.new
config.register_marshaller :yaml, CustomYAMLMarshaller

expect(config.marshallers).to eq([
CustomYAMLMarshaller,
TTY::Config::Marshallers::JSONMarshaller,
TTY::Config::Marshallers::TOMLMarshaller,
TTY::Config::Marshallers::INIMarshaller,
TTY::Config::Marshallers::HCLMarshaller,
TTY::Config::Marshallers::JavaPropsMarshaller
])
expect(CustomYAMLMarshaller.ext).to eq(%w[.yaml .yml])
expect(CustomYAMLMarshaller.dep_name).to eq(%w[yaml])
end
end

0 comments on commit 6e9f101

Please sign in to comment.