ruby-immutable-struct 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/ruby-immutable-struct.rb +51 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 85d73514f08b01000998a15e95f33ba87a4c6217
|
4
|
+
data.tar.gz: e41e2b39d4722c4cd0e7b97333826ab4553d93e8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fce8f9f4ae81eb44133ca3c739a8777af22ed29dcd141da7fc8ae340fc63da78d5f5ebd76114576dd5a359ee47d92381f345692f4e87ab737e33619de073d63e
|
7
|
+
data.tar.gz: 523f5b5cef2a16c82065b2856760803f25f95f8797a44a16a3cb9bb15198138be5f10b268d410bf9b6d83056d8a54c95a221042e87e2e453a9d8531bfe14d334
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RubyImmutableStruct
|
2
|
+
def self.new(*attributes, &block)
|
3
|
+
attributes = attributes.map(&:to_sym)
|
4
|
+
|
5
|
+
if !attributes.all? { |a| a.to_s =~ /^[_a-z][_a-zA-Z0-9_]*/ }
|
6
|
+
raise "ImmutableStruct only allows attributes that look like Ruby variables: /[_a-z][a-zA-Z0-9_]*/. That keeps things fast and simple."
|
7
|
+
end
|
8
|
+
|
9
|
+
klass = Class.new do
|
10
|
+
attr_reader(*attributes)
|
11
|
+
|
12
|
+
class_eval <<-EOT
|
13
|
+
def initialize(*args)
|
14
|
+
if args[0].is_a?(Hash)
|
15
|
+
hash = args[0]
|
16
|
+
#{attributes.map{ |a| "@#{a} = hash[:#{a}]" }.join(';')}
|
17
|
+
else
|
18
|
+
#{attributes.map.with_index{ |a, i| "@#{a} = args[#{i}]" }.join(';')}
|
19
|
+
end
|
20
|
+
|
21
|
+
freeze
|
22
|
+
end
|
23
|
+
|
24
|
+
def merge(hash)
|
25
|
+
merged_hash = to_h.merge!(hash)
|
26
|
+
self.class.new(merged_hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_h
|
30
|
+
{ #{attributes.map{ |a| "#{a}: @#{a}" }.join(',')} }
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_a
|
34
|
+
[ #{attributes.map{ |a| "@#{a}" }.join(',')} ]
|
35
|
+
end
|
36
|
+
|
37
|
+
def inspect
|
38
|
+
"#<#\{self.class.name} #\{to_a.map(&:inspect).join(',')}>"
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_s
|
42
|
+
inspect
|
43
|
+
end
|
44
|
+
EOT
|
45
|
+
end
|
46
|
+
|
47
|
+
klass.class_exec(&block) if !block.nil?
|
48
|
+
|
49
|
+
klass
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-immutable-struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Hooper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: adam@adamhooper.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/ruby-immutable-struct.rb
|
20
|
+
homepage: https://github.com/adamhooper/ruby-immutable-struct
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.8
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Like Structs, but you can't edit them once you construct them
|
44
|
+
test_files: []
|