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



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
180
# File 'lib/bound.rb', line 154

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



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

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

.define_equality(attr) ⇒ Object



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

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



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

def self.define_initializer(after_init = 'validate!')
  code = <<-EOR
    def initialize(target = nil, overwrite = nil)
      @t, @o = target, overwrite
      %s
    end
  EOR

  class_eval code % after_init
end

.define_initializer_without_validationObject



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

def self.define_initializer_without_validation
  define_initializer(nil)
end

.define_nested_delegate(attr, nested_class) ⇒ Object



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

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_validatorObject



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

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 = <<-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



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

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

.required(*attributes) ⇒ Object



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

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

.set_attributes(type, attributes) ⇒ Object



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

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



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

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



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

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



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

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

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  false unless other
  true
end

#validate!Object



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

def validate!
end