Class: Bound::StaticBoundClass

Inherits:
Object
  • Object
show all
Defined in:
lib/bound.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_attributes(*attributes) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/bound.rb', line 153

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 }
    message = "Invalid list of attributes: #{nested_attributes.inspect}"
    raise ArgumentError, message
  end

  if attributes.any? { |a| !a.kind_of? Symbol }
    message = "Invalid list of attributes: #{attributes.inspect}"
    raise ArgumentError, message
  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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bound.rb', line 239

def self.define_delegate(attr, prefix = '')
  code = "    def \#{prefix}\#{attr}\n      if @o && @o.key?(:\#{attr})\n        @o[:\#{attr}]\n      elsif @t\n        if @t.kind_of?(Hash)\n          @t[:\#{attr}]\n        elsif @t.respond_to?(:\#{attr})\n          @t.\#{attr}\n        end\n      end\n    end\n  EOR\n  class_eval code\nend\n"

.define_equality(attr) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/bound.rb', line 223

def self.define_equality(attr)
  @equality ||= []
  @equality << attr
  undef_method :==
  code = "    def ==(other)\n      return false unless other\n      \#{@equality.inspect}.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



142
143
144
145
146
147
148
149
150
151
# File 'lib/bound.rb', line 142

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\n  class_eval code % after_init\nend\n"

.define_initializer_without_validationObject



138
139
140
# File 'lib/bound.rb', line 138

def self.define_initializer_without_validation
  define_initializer(nil)
end

.define_nested_delegate(attr, nested_class) ⇒ Object



256
257
258
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
# File 'lib/bound.rb', line 256

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_validatorObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/bound.rb', line 181

def self.define_validator
  attributes = symbolize_attributes(@attributes ||= [])
  optional_attributes = symbolize_attributes(@optional_attributes ||= [])
  nested_array_attributes = symbolize_attributes(@nested_array_attributes ||= [])

  undef_method :validate!

  code = "    def validate!\n      v = Bound::BoundValidator.new(self, @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



298
299
300
# File 'lib/bound.rb', line 298

def self.optional(*attributes)
  set_attributes(:set_optional_attributes, attributes)
end

.required(*attributes) ⇒ Object



294
295
296
# File 'lib/bound.rb', line 294

def self.required(*attributes)
  set_attributes(:set_required_attributes, attributes)
end

.set_attributes(type, attributes) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/bound.rb', line 302

def self.set_attributes(type, 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.send(type, attributes, array_attributes)
  self
end

.set_optional_attributes(attributes, nested_array_attributes) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/bound.rb', line 214

def self.set_optional_attributes(attributes, nested_array_attributes)
  @optional_attributes ||= []
  @optional_attributes.concat attributes
  @optional_attributes.concat nested_array_attributes
  @nested_array_attributes ||= []
  @nested_array_attributes.concat nested_array_attributes
  define_validator
end

.set_required_attributes(attributes, nested_array_attributes) ⇒ Object



205
206
207
208
209
210
211
212
# File 'lib/bound.rb', line 205

def self.set_required_attributes(attributes, nested_array_attributes)
  @attributes ||= []
  @attributes.concat attributes
  @attributes.concat nested_array_attributes
  @nested_array_attributes ||= []
  @nested_array_attributes.concat nested_array_attributes
  define_validator
end

.symbolize_attributes(attributes) ⇒ Object



201
202
203
# File 'lib/bound.rb', line 201

def self.symbolize_attributes(attributes)
  attributes.map { |attr| ":#{attr}" }.join(", ")
end

Instance Method Details

#==(other) ⇒ Object



130
131
132
133
# File 'lib/bound.rb', line 130

def ==(other)
  false unless other
  true
end

#validate!Object



135
136
# File 'lib/bound.rb', line 135

def validate!
end