Class: ClosedStruct
- Inherits:
-
Object
- Object
- ClosedStruct
- Defined in:
- lib/closed_struct.rb,
lib/closed_struct/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #each_pair ⇒ Object
- #empty? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(contents, &block) ⇒ ClosedStruct
constructor
A new instance of ClosedStruct.
- #to_h ⇒ Object
Constructor Details
#initialize(contents, &block) ⇒ ClosedStruct
Returns a new instance of ClosedStruct.
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/closed_struct.rb', line 4 def initialize(contents, &block) @contents = contents.dup singleton_class = (class << self; self; end) @contents.each do |key, value| raise ArgumentError.new("Cannot define #{key} as it already exists") if respond_to?(key) singleton_class.send(:define_method, key) { value } end singleton_class.class_eval(&block) if block_given? end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
24 25 26 |
# File 'lib/closed_struct.rb', line 24 def ==(other) (other.class == self.class) && (other.to_h == self.to_h) end |
#each_pair ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/closed_struct.rb', line 33 def each_pair return enum_for(:each_pair) unless block_given? @contents.each_pair do |key, value| yield key, value end end |
#empty? ⇒ Boolean
29 30 31 |
# File 'lib/closed_struct.rb', line 29 def empty? @contents.empty? end |
#hash ⇒ Object
20 21 22 |
# File 'lib/closed_struct.rb', line 20 def hash @contents.hash end |
#to_h ⇒ Object
16 17 18 |
# File 'lib/closed_struct.rb', line 16 def to_h @contents end |