Class: ThinModels::Struct
- Inherits:
-
Object
- Object
- ThinModels::Struct
show all
- Defined in:
- lib/thin_models/struct.rb,
lib/thin_models/struct/identity.rb
Direct Known Subclasses
Typed
Defined Under Namespace
Modules: IdentityMethods
Classes: Typed
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(values = nil, skip_checks = false, &lazy_values) ⇒ Struct
Returns a new instance of Struct.
11
12
13
14
15
|
# File 'lib/thin_models/struct.rb', line 11
def initialize(values=nil, skip_checks=false, &lazy_values)
@values = values || {}
@lazy_values = lazy_values if lazy_values
check_attributes if values && !skip_checks
end
|
Instance Attribute Details
#lazy_values ⇒ Object
Returns the value of attribute lazy_values.
49
50
51
|
# File 'lib/thin_models/struct.rb', line 49
def lazy_values
@lazy_values
end
|
Class Method Details
.attributes ⇒ Object
154
155
156
|
# File 'lib/thin_models/struct.rb', line 154
def attributes
@attributes ||= (superclass < Struct ? superclass.attributes.dup : Set.new)
end
|
.json_create(json_values) ⇒ Object
146
147
148
149
150
|
# File 'lib/thin_models/struct.rb', line 146
def self.json_create(json_values)
values = {}
attributes.each {|a| values[a] = json_values[a.to_s] if json_values.has_key?(a.to_s)}
new(values)
end
|
.new_skipping_checks(values, &lazy_values) ⇒ Object
7
8
9
|
# File 'lib/thin_models/struct.rb', line 7
def self.new_skipping_checks(values, &lazy_values)
new(values, true, &lazy_values)
end
|
Instance Method Details
#[](attribute) ⇒ Object
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/thin_models/struct.rb', line 69
def [](attribute)
if @values.has_key?(attribute)
@values[attribute]
else
raise NameError, "no attribute #{attribute} in #{self.class}" unless self.class.attributes.include?(attribute)
if @lazy_values
@values[attribute] = @lazy_values.call(self, attribute)
end
end
end
|
#[]=(attribute, value) ⇒ Object
93
94
95
96
|
# File 'lib/thin_models/struct.rb', line 93
def []=(attribute, value)
raise NameError, "no attribute #{attribute.inspect} in #{self.class}" unless self.class.attributes.include?(attribute)
@values[attribute] = value
end
|
#attribute_loaded?(attribute) ⇒ Boolean
Also known as:
has_key?
44
45
46
|
# File 'lib/thin_models/struct.rb', line 44
def attribute_loaded?(attribute)
@values.has_key?(attribute)
end
|
#attributes ⇒ Object
60
61
62
|
# File 'lib/thin_models/struct.rb', line 60
def attributes
self.class.attributes
end
|
#check_attributes ⇒ Object
17
18
19
20
21
22
|
# File 'lib/thin_models/struct.rb', line 17
def check_attributes
attributes = self.class.attributes
@values.each_key do |attribute|
raise NameError, "no attribute #{attribute} in #{self.class}" unless attributes.include?(attribute)
end
end
|
#fetch(attribute) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/thin_models/struct.rb', line 80
def fetch(attribute)
if @values.has_key?(attribute)
@values[attribute]
else
raise NameError, "no attribute #{attribute} in #{self.class}" unless self.class.attributes.include?(attribute)
if @lazy_values
@values[attribute] = @lazy_values.call(self, attribute)
else
raise PartialDataError, "attribute #{attribute} not loaded"
end
end
end
|
#freeze ⇒ Object
32
33
34
35
|
# File 'lib/thin_models/struct.rb', line 32
def freeze
super
@values.freeze
end
|
#has_lazy_values? ⇒ Boolean
56
57
58
|
# File 'lib/thin_models/struct.rb', line 56
def has_lazy_values?
instance_variable_defined?(:@lazy_values)
end
|
#initialize_copy(other) ⇒ Object
this allows ‘dup’ to work in a desirable way for these instances, ie use a dup’d properties hash instance for the dup, meaning it can be updated without affecting the state of the original.
27
28
29
30
|
# File 'lib/thin_models/struct.rb', line 27
def initialize_copy(other)
super
@values = @values.dup
end
|
#inspect ⇒ Object
Also known as:
to_s
Based on Matz’s code for OpenStruct#inspect in the stdlib.
Note the trick with the Thread-local :inspect_key, which ruby internals appear to use but isn’t documented anywhere. If you use it in the same way the stdlib uses it, you can override inspect without breaking its cycle avoidant behaviour
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/thin_models/struct.rb', line 115
def inspect
str = "#<#{self.class}"
ids = (Thread.current[:__inspect_key__] ||= [])
if ids.include?(object_id)
return str << ' ...>'
end
ids << object_id
begin
first = true
for k,v in @values
str << "," unless first
first = false
str << " #{k}=#{v.inspect}"
end
if @lazy_values
str << "," unless first
str << " ..."
end
return str << '>'
ensure
ids.pop
end
end
|
#loaded_attributes ⇒ Object
Also known as:
keys
64
65
66
|
# File 'lib/thin_models/struct.rb', line 64
def loaded_attributes
@values.keys
end
|
#loaded_values ⇒ Object
Also known as:
to_hash
37
38
39
|
# File 'lib/thin_models/struct.rb', line 37
def loaded_values
@values.dup
end
|
#merge(updated_values) ⇒ Object
98
99
100
|
# File 'lib/thin_models/struct.rb', line 98
def merge(updated_values)
dup.merge!(updated_values)
end
|
#merge!(updated_values) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/thin_models/struct.rb', line 102
def merge!(updated_values)
updated_values.to_hash.each_key do |attribute|
raise NameError, "no attribute #{attribute.inspect} in #{self.class}" unless attributes.include?(attribute)
end
@values.merge!(updated_values)
self
end
|
#remove_lazy_values ⇒ Object
52
53
54
|
# File 'lib/thin_models/struct.rb', line 52
def remove_lazy_values
remove_instance_variable(:@lazy_values) if instance_variable_defined?(:@lazy_values)
end
|
#to_json(*p) ⇒ Object
142
143
144
|
# File 'lib/thin_models/struct.rb', line 142
def to_json(*p)
@values.merge(:json_class => self.class).to_json(*p)
end
|