Skip to content

Commit

Permalink
Merge pull request #6 from gangelo/development
Browse files Browse the repository at this point in the history
General refactors and README.md updates
  • Loading branch information
gangelo authored Aug 13, 2022
2 parents 02441c0 + bbb23f5 commit 9e7d69c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 0.2.1
* Enhancements
* General refactors/cleanup
* Added Immutable module; when extended on a struct, makes it immutable.
* Added Comparable module; when extended on a struct, makes it comparable to other structs and hashes.

### 0.1.1
* Enhancements
* Added this CHANGELOG.md
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
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/
Expand Down
18 changes: 18 additions & 0 deletions lib/comparable.rb
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
16 changes: 16 additions & 0 deletions lib/immutable.rb
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
24 changes: 4 additions & 20 deletions lib/immutable_struct_ex.rb
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
2 changes: 1 addition & 1 deletion lib/immutable_struct_ex/version.rb
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
2 changes: 1 addition & 1 deletion spec/immutable_struct_ex_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

RSpec.describe ImmutableStructEx do
RSpec.describe ImmutableStructEx, type: :module do
subject { described_class.new(**hash) }

let(:hash) { { key1: :value1, key2: :value2 } }
Expand Down
4 changes: 3 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'immutable_struct_ex'
require 'pry-byebug'
require 'bundler/setup'
require 'simplecov'
Expand All @@ -9,6 +8,9 @@
add_filter 'spec'
end

require 'immutable_struct_ex'
Dir[File.join(Dir.pwd, "spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
Expand Down

0 comments on commit 9e7d69c

Please sign in to comment.