Class: Bound::StaticBoundClass
- Inherits:
-
Object
- Object
- Bound::StaticBoundClass
- Defined in:
- lib/bound.rb
Class Method Summary collapse
- .define_attributes(*attributes) ⇒ Object
- .define_delegate(attr, prefix = '') ⇒ Object
- .define_equality(attr) ⇒ Object
- .define_initializer(after_init = 'validate!') ⇒ Object
- .define_initializer_without_validation ⇒ Object
- .define_nested_delegate(attr, nested_class) ⇒ Object
- .define_validator ⇒ Object
- .optional(*attributes) ⇒ Object
- .required(*attributes) ⇒ Object
- .set_optional_attributes(attributes, nested_array_attributes) ⇒ Object
- .set_required_attributes(attributes, nested_array_attributes) ⇒ Object
Instance Method Summary collapse
Class Method Details
.define_attributes(*attributes) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/bound.rb', line 164 def self.define_attributes(*attributes) if attributes.last.kind_of? Hash nested_attributes = attributes.pop else nested_attributes = {} end if nested_attributes.keys.any? { |a| !a.kind_of? Symbol } = "Invalid list of attributes: #{nested_attributes.inspect}" raise ArgumentError, end if attributes.any? { |a| !a.kind_of? Symbol } = "Invalid list of attributes: #{attributes.inspect}" raise ArgumentError, end nested_attributes.each do |attribute, nested_class| define_nested_delegate attribute, nested_class define_equality attribute end attributes.each do |attribute| define_delegate attribute define_equality attribute end end |
.define_delegate(attr, prefix = '') ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/bound.rb', line 248 def self.define_delegate(attr, prefix = '') code = <<-EOR def #{prefix}#{attr} return @o[:#{attr}] if @o && @o.key?(:#{attr}) return @t.kind_of?(Hash)? @t[:#{attr}] : @t.#{attr} if @t nil end EOR class_eval code end |
.define_equality(attr) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/bound.rb', line 233 def self.define_equality(attr) @equality ||= [] @equality << attr code = <<-EOR def==(other) return false unless other %w{#{@equality.join(' ')}}.all? do |attr| other.respond_to?(attr) && other.send(attr) == send(attr) end end EOR class_eval code end |
.define_initializer(after_init = 'validate!') ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/bound.rb', line 148 def self.define_initializer(after_init = 'validate!') code = <<-EOR def initialize(target = nil, overwrite = nil) @t, @o = target, overwrite %s end EOR if after_init code = code % " #{after_init}" else code = code % '' end class_eval code end |
.define_initializer_without_validation ⇒ Object
144 145 146 |
# File 'lib/bound.rb', line 144 def self.define_initializer_without_validation define_initializer(nil) end |
.define_nested_delegate(attr, nested_class) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/bound.rb', line 259 def self.define_nested_delegate(attr, nested_class) define_delegate attr, 'get_' code = <<-EOR class << self def get_#{attr}_class @#{attr}_class end def set_#{attr}_class(arg) @#{attr}_class = arg end private :set_#{attr}_class end EOR if nested_class.kind_of? Array nested_class = nested_class.first code += <<-EOR def #{attr} return @#{attr} if defined? @#{attr} return [] unless val = get_#{attr} @#{attr} ||= val.map{|t| self.class.get_#{attr}_class.new t} end private :get_#{attr} EOR else code += <<-EOR def #{attr} return @#{attr} if defined? @#{attr} return nil unless val = get_#{attr} @#{attr} ||= self.class.get_#{attr}_class.new(val) end private :get_#{attr} EOR end class_eval code self.send :"set_#{attr}_class", nested_class end |
.define_validator ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/bound.rb', line 192 def self.define_validator attributes = (@attributes || []).map do |attr| ":#{attr.to_s}" end.join(',') optional_attributes = (@optional_attributes || []).map do |attr| ":#{attr.to_s}" end.join(',') nested_array_attributes = (@nested_array_attributes || []).map do |attr| ":#{attr.to_s}" end.join(',') code = <<-EOR def validate! v = Bound::BoundValidator.new(self, @t, @o) v.attributes = [#{attributes}] v.optional_attributes = [#{optional_attributes}] v.nested_array_attributes = [#{nested_array_attributes}] v.validate! end private :validate! EOR class_eval code end |
.optional(*attributes) ⇒ Object
312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/bound.rb', line 312 def self.optional(*attributes) self.define_attributes(*attributes) array_attributes = [] if attributes.last.kind_of? Hash attributes.pop.each do |attr, nested_class| array_attributes << attr if nested_class.kind_of? Array attributes << attr end end self.set_optional_attributes(attributes, array_attributes) self end |
.required(*attributes) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/bound.rb', line 297 def self.required(*attributes) self.define_attributes(*attributes) array_attributes = [] if attributes.last.kind_of? Hash attributes.pop.each do |attr, nested_class| array_attributes << attr if nested_class.kind_of? Array attributes << attr end end self.set_required_attributes(attributes, array_attributes) self end |
.set_optional_attributes(attributes, nested_array_attributes) ⇒ Object
224 225 226 227 228 229 230 231 |
# File 'lib/bound.rb', line 224 def self.set_optional_attributes(attributes, nested_array_attributes) @optional_attributes ||= [] @optional_attributes += attributes @optional_attributes += nested_array_attributes @nested_array_attributes ||= [] @nested_array_attributes += nested_array_attributes define_validator end |
.set_required_attributes(attributes, nested_array_attributes) ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/bound.rb', line 215 def self.set_required_attributes(attributes, nested_array_attributes) @attributes ||= [] @attributes += attributes @attributes += nested_array_attributes @nested_array_attributes ||= [] @nested_array_attributes += nested_array_attributes define_validator end |
Instance Method Details
#==(other) ⇒ Object
136 137 138 139 |
# File 'lib/bound.rb', line 136 def ==(other) false unless other true end |
#validate! ⇒ Object
141 142 |
# File 'lib/bound.rb', line 141 def validate! end |