strong_struct 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/strong_struct.rb +24 -51
- data/readme.md +25 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b6f0a136241ec861653b212ed33710243ed86d3a53a6d4e60e090ce7074f76c7
|
4
|
+
data.tar.gz: 9ae643bcc1af66ec510607faa643f3b7ddecd0d441cfb672a28068d21b65bb27
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b94d4d7e510ea594d4262fd9e93bd3f9d89c74a0c833e42e4a33a429b833c812c4c7acf38e5fb0fc7bbb0bf09fd6e3d6fa24affe05426498f3524bf86b8e73d3
|
7
|
+
data.tar.gz: 15af8433cf22d12031153f8e53e1f0feea206b5f1a091f38e1e5571a6486d5ecabd6ab63ad043852f21b7ffeb0e124f0feb38baa7174f0f5ecf2a56547d1c5ef
|
data/lib/strong_struct.rb
CHANGED
@@ -1,33 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
module StrongStruct
|
3
|
+
module Error
|
4
|
+
class ClassInUseError < StandardError; end
|
5
|
+
end
|
6
|
+
|
3
7
|
module ClassMethods
|
4
8
|
def accessors
|
5
9
|
@accessors ||= []
|
6
10
|
end
|
7
11
|
|
8
|
-
def name
|
9
|
-
return super unless defined?(@name)
|
10
|
-
@name
|
11
|
-
end
|
12
|
-
|
13
|
-
def name=(value)
|
14
|
-
if defined?(@name)
|
15
|
-
raise "#{StrongStruct} pseudo-classes may not be renamed."
|
16
|
-
end
|
17
|
-
|
18
|
-
@name = value
|
19
|
-
end
|
20
|
-
|
21
|
-
def to_s
|
22
|
-
return super unless defined?(@name)
|
23
|
-
super.gsub(/^#<Class:/, "#<#{@name}:")
|
24
|
-
end
|
25
|
-
|
26
|
-
def inspect
|
27
|
-
return super unless defined?(@name)
|
28
|
-
super.gsub(/^#<Class:/, "#<#{@name}:")
|
29
|
-
end
|
30
|
-
|
31
12
|
private
|
32
13
|
|
33
14
|
def add_accessor(accessor)
|
@@ -54,35 +35,8 @@ module StrongStruct
|
|
54
35
|
hash
|
55
36
|
end
|
56
37
|
|
57
|
-
def to_s
|
58
|
-
base = super
|
59
|
-
klass_name = class_name
|
60
|
-
return base unless klass_name != 'Object'
|
61
|
-
base.gsub(/^#<#<Class:/, "#<#<#{klass_name}:")
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
def inspect
|
66
|
-
base = super
|
67
|
-
klass_name = class_name
|
68
|
-
return base unless klass_name != 'Object'
|
69
|
-
base.gsub(/^#<#<Class:/, "#<#<#{klass_name}:")
|
70
|
-
end
|
71
|
-
|
72
38
|
private
|
73
39
|
|
74
|
-
def class_name
|
75
|
-
klass = self.class
|
76
|
-
return klass.name if klass.name
|
77
|
-
|
78
|
-
while klass.superclass
|
79
|
-
klass = klass.superclass
|
80
|
-
break if klass.name
|
81
|
-
end
|
82
|
-
|
83
|
-
klass.name
|
84
|
-
end
|
85
|
-
|
86
40
|
def accessors
|
87
41
|
@accessors ||= get_accessors
|
88
42
|
end
|
@@ -107,14 +61,33 @@ module StrongStruct
|
|
107
61
|
|
108
62
|
module Core
|
109
63
|
def new(*args)
|
64
|
+
name = name_from_params(args)
|
65
|
+
|
66
|
+
if name && const_defined?(name)
|
67
|
+
raise Error::ClassInUseError.new("Class already in use: #{name}")
|
68
|
+
end
|
69
|
+
|
70
|
+
klass = build_class(args)
|
71
|
+
|
72
|
+
name ? Object.const_set(name, klass) : klass
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def build_class(attribute_names)
|
110
78
|
Class.new do
|
111
79
|
extend ClassMethods
|
112
80
|
include InstanceMethods
|
113
81
|
|
114
|
-
|
82
|
+
attribute_names.each { |attr| add_accessor(attr) }
|
83
|
+
|
115
84
|
add_accessors
|
116
85
|
end
|
117
86
|
end
|
87
|
+
|
88
|
+
def name_from_params(params)
|
89
|
+
params.first.to_s.match(/^[A-Z]/) ? params.shift : nil
|
90
|
+
end
|
118
91
|
end
|
119
92
|
|
120
93
|
extend Core
|
data/readme.md
CHANGED
@@ -78,6 +78,31 @@ object = my_struct.new(:id => 3, :name => 'John Doe')
|
|
78
78
|
object.attributes #=> {'id' => 3, 'name' => 'John Doe', 'ssn' => nil }
|
79
79
|
```
|
80
80
|
|
81
|
+
Naming
|
82
|
+
------
|
83
|
+
|
84
|
+
There are two ways to get `to_s`, `inspect` and friends
|
85
|
+
to give you meaningful information
|
86
|
+
(instead of `#<#Class0x00000>:0x00000>` or the like).
|
87
|
+
|
88
|
+
The first is by simply setting the result of `StrongStruct.new` to a constant:
|
89
|
+
`Foo = StrongStruct.new(:name)`
|
90
|
+
|
91
|
+
The second is dynamically by specifying a string
|
92
|
+
that starts with a capital letter as the first parameter:
|
93
|
+
`StrongStruct.new('Foo', :fizz, :buzz)`
|
94
|
+
|
95
|
+
*Please note*: This will create a new `Foo` constant!
|
96
|
+
|
97
|
+
`StrongStruct` raises an error (`StrongStruct::Errors::ClassInUseError`)
|
98
|
+
if you attempt to call `new` twice with the same constant string.
|
99
|
+
|
100
|
+
If you REALLY need to do this, it is recommended that you create these
|
101
|
+
constants yourself and use `StrongStruct` classes without
|
102
|
+
specifying a constant name.
|
103
|
+
You can also call `const_set` yourself:
|
104
|
+
`const_set 'MyConstant', StrongStruct.new(:name)`
|
105
|
+
|
81
106
|
|
82
107
|
Testing ActiveRecord Models
|
83
108
|
---------------------------
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: strong_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Travis Herrick
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -140,8 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
|
-
|
144
|
-
rubygems_version: 2.5.1
|
143
|
+
rubygems_version: 3.0.1
|
145
144
|
signing_key:
|
146
145
|
specification_version: 4
|
147
146
|
summary: OpenStruct-like classes with strong attributes
|