-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from gangelo/development
General refactors and README.md updates
- Loading branch information
Showing
8 changed files
with
50 additions
and
24 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
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: | ||
immutable_struct_ex (0.1.1) | ||
immutable_struct_ex (0.2.1) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
|
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module ImmutableStructEx | ||
# Makes a struct comparable with another struct or hash when extended. | ||
module Comparable | ||
class << self | ||
def extended(struct) | ||
struct.instance_eval do | ||
def ==(other) | ||
return false unless other.is_a?(Hash) || other.is_a?(Struct) | ||
|
||
to_h == other.to_h | ||
end | ||
end | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module ImmutableStructEx | ||
# Makes a struct immutable when extended. | ||
module Immutable | ||
class << self | ||
def extended(struct) | ||
[:[], *struct.members].each do |method| | ||
struct.instance_eval do | ||
undef :"#{method}=" | ||
end | ||
end | ||
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 |
---|---|---|
@@ -1,34 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'immutable_struct_ex/version' | ||
require 'comparable' | ||
require 'immutable' | ||
|
||
# Defines the methods used to create/manage the ImmutableStructEx struct. | ||
module ImmutableStructEx | ||
class << self | ||
def new(**hash, &block) | ||
options_struct = Struct.new(*hash.keys, keyword_init: true, &block) | ||
options_struct.new(**hash).tap do |struct| | ||
[:[], *struct.members].each do |method| | ||
evaluate(struct) do | ||
<<~RUBY | ||
undef :"#{method}=" | ||
RUBY | ||
end | ||
end | ||
evaluate(struct) do | ||
<<~RUBY | ||
def ==(object) | ||
return false unless object.respond_to? :to_h | ||
to_h == object.to_h | ||
end | ||
RUBY | ||
end | ||
struct.extend Comparable | ||
struct.extend Immutable | ||
end | ||
end | ||
|
||
def evaluate(struct) | ||
struct.instance_eval yield | ||
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 ImmutableStructEx | ||
VERSION = '0.1.1' | ||
VERSION = '0.2.1' | ||
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