Module: Aliyun::Log::Record::Field::ClassMethods

Defined in:
lib/aliyun/log/record/field.rb

Instance Method Summary collapse

Instance Method Details

#field(name, options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aliyun/log/record/field.rb', line 28

def field(name, options = {})
  type = options[:type] || :text
  unless PERMITTED_KEY_TYPES.include?(type)
    raise ArgumentError, "Field #{name} type(#{type}) error, key type only support text/long/double/json"
  end

  named = name.to_s
  if options[:log_tag]
    self.tag_attributes = tag_attributes.merge(name => { type: type }.merge(options))
  else
    self.attributes = attributes.merge(name => { type: type }.merge(options))
  end

  warn_about_method_overriding(name, name)
  warn_about_method_overriding("#{named}=", name)
  warn_about_method_overriding("#{named}?", name)

  define_attribute_method(name)

  generated_methods.module_eval do
    define_method(named) { read_attribute(named) }
    define_method("#{named}?") do
      value = read_attribute(named)
      case value
      when true        then true
      when false, nil  then false
      else
        !value.nil?
      end
    end
    define_method("#{named}=") { |value| write_attribute(named, value) }
  end
end

#remove_field(field) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/aliyun/log/record/field.rb', line 62

def remove_field(field)
  field = field.to_sym
  attributes.delete(field) || raise('No such field')

  undefine_attribute_methods
  define_attribute_methods attributes.keys

  generated_methods.module_eval do
    remove_method field
    remove_method :"#{field}="
    remove_method :"#{field}?"
  end
end