Module: QbwcRequests::OrderedFields::ClassMethods

Defined in:
lib/qbwc_requests/ordered_fields.rb

Instance Method Summary collapse

Instance Method Details

#attr_orderObject



38
39
40
# File 'lib/qbwc_requests/ordered_fields.rb', line 38

def attr_order
  @attr_order
end

#field(attribute_name) ⇒ Object



4
5
6
7
8
# File 'lib/qbwc_requests/ordered_fields.rb', line 4

def field attribute_name
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
end

#has_one(attribute_name, klass) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/qbwc_requests/ordered_fields.rb', line 9

def has_one attribute_name, klass
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
  validates_each attribute_name do |record, attribute, value|
    if !value.blank? and value.class != klass
      record.errors.add(attribute, "must be of type #{klass}")
    end
  end
end

#ref_to(attribute_prefix, name_length = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/qbwc_requests/ordered_fields.rb', line 19

def ref_to attribute_prefix, name_length=nil
  attribute_name = "#{attribute_prefix}_ref".to_sym
  @attr_order ||= Set.new
  @attr_order << attribute_name
  attr_accessor attribute_name
  validates_each attribute_name do |record, attribute, value|
    if value.present?
      if value.is_a?(Hash)
        if !(value[:list_id].present? ^ value[:full_name].present?)
          record.errors.add(attribute, "Must have list_id or full_name")
        elsif name_length && value.fetch(:full_name, "").length > name_length
          record.errors.add(attribute, "- maximum 'full name' length is: #{name_length}")
        end
      else
        record.errors.add(attribute, "Must have the format {list_id: 'value'} or {full_name: 'value'}")
      end
    end
  end
end