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
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/bound.rb', line 148 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
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/bound.rb', line 232 def self.define_delegate(attr, prefix = '') code = " def \#{prefix}\#{attr}\n return @o[:\#{attr}] if @o && @o.key?(:\#{attr})\n return @t.kind_of?(Hash)? @t[:\#{attr}] : @t.\#{attr} if @t\n nil\n end\n EOR\n class_eval code\nend\n" |
.define_equality(attr) ⇒ Object
217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bound.rb', line 217 def self.define_equality(attr) @equality ||= [] @equality << attr code = " def==(other)\n return false unless other\n %w{\#{@equality.join(' ')}}.all? do |attr|\n other.respond_to?(attr) &&\n other.send(attr) == send(attr)\n end\n end\n EOR\n class_eval code\nend\n" |
.define_initializer(after_init = 'validate!') ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/bound.rb', line 132 def self.define_initializer(after_init = 'validate!') code = " def initialize(target = nil, overwrite = nil)\n @t, @o = target, overwrite\n %s\n end\n EOR\n if after_init\n code = code % \" \#{after_init}\"\n else\n code = code % ''\n end\n\n class_eval code\nend\n" |
.define_initializer_without_validation ⇒ Object
128 129 130 |
# File 'lib/bound.rb', line 128 def self.define_initializer_without_validation define_initializer(nil) end |
.define_nested_delegate(attr, nested_class) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/bound.rb', line 243 def self.define_nested_delegate(attr, nested_class) define_delegate attr, 'get_' code = " class << self\n def get_\#{attr}_class\n @\#{attr}_class\n end\n def set_\#{attr}_class(arg)\n @\#{attr}_class = arg\n end\n private :set_\#{attr}_class\n end\n EOR\n\n if nested_class.kind_of? Array\n nested_class = nested_class.first\n code += <<-EOR\n def \#{attr}\n return @\#{attr} if defined? @\#{attr}\n return [] unless val = get_\#{attr}\n @\#{attr} ||= val.map{|t| self.class.get_\#{attr}_class.new t}\n end\n private :get_\#{attr}\n EOR\n else\n code += <<-EOR\n def \#{attr}\n return @\#{attr} if defined? @\#{attr}\n return nil unless val = get_\#{attr}\n @\#{attr} ||= self.class.get_\#{attr}_class.new(val)\n end\n private :get_\#{attr}\n EOR\n end\n class_eval code\n self.send :\"set_\#{attr}_class\", nested_class\nend\n" |
.define_validator ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/bound.rb', line 176 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 = " def validate!\n v = Bound::BoundValidator.new(@t, @o)\n v.attributes = [\#{attributes}]\n v.optional_attributes = [\#{optional_attributes}]\n v.nested_array_attributes = [\#{nested_array_attributes}]\n v.validate!\n end\n private :validate!\n EOR\n class_eval code\nend\n" |
.optional(*attributes) ⇒ Object
296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/bound.rb', line 296 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
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/bound.rb', line 281 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
208 209 210 211 212 213 214 215 |
# File 'lib/bound.rb', line 208 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
199 200 201 202 203 204 205 206 |
# File 'lib/bound.rb', line 199 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
120 121 122 123 |
# File 'lib/bound.rb', line 120 def ==(other) false unless other true end |
#validate! ⇒ Object
125 126 |
# File 'lib/bound.rb', line 125 def validate! end |