Module: OldApiResource::Attributes::ClassMethods

Defined in:
lib/old_api_resource/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/old_api_resource/attributes.rb', line 89

def attribute?(name)
  self.attribute_names.include?(name.to_sym)
end

#clear_attributesObject



97
98
99
100
101
# File 'lib/old_api_resource/attributes.rb', line 97

def clear_attributes
  self.attribute_names.clear
  self.public_attribute_names.clear
  self.protected_attribute_names.clear
end

#define_attributes(*args) ⇒ Object



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
61
# File 'lib/old_api_resource/attributes.rb', line 35

def define_attributes(*args)
  # This is provided by ActiveModel::AttributeMethods, it should define the basic methods
  # but we need to override all the setters so we do dirty tracking
  define_attribute_methods args
  args.each do |arg|
    self.attribute_names << arg.to_sym
    self.public_attribute_names << arg.to_sym
    
    # Override the setter for dirty tracking
    self.class_eval <<-EOE, __FILE__, __LINE__ + 1
      def #{arg}
        self.attributes[:#{arg}]
      end
    
      def #{arg}=(val)
        #{arg}_will_change! unless self.#{arg} == val
        self.attributes[:#{arg}] = val
      end
      
      def #{arg}?
        self.attributes[:#{arg}].present?
      end
    EOE
  end
  self.attribute_names.uniq!
  self.public_attribute_names.uniq!
end

#define_protected_attributes(*args) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/old_api_resource/attributes.rb', line 63

def define_protected_attributes(*args)
  define_attribute_methods args
  args.each do |arg|
    self.attribute_names << arg.to_sym
    self.protected_attribute_names << arg.to_sym
    
    # These attributes cannot be set, throw an error if you try
    self.class_eval <<-EOE, __FILE__, __LINE__ + 1

      def #{arg}
        self.attributes[:#{arg}]
      end
    
      def #{arg}=(val)
        raise "#{arg} is a protected attribute and cannot be set"
      end
      
      def #{arg}?
        self.attributes[:#{arg}].present?
      end
    EOE
  end
  self.attribute_names.uniq!
  self.protected_attribute_names.uniq!
end

#protected_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/old_api_resource/attributes.rb', line 93

def protected_attribute?(name)
  self.protected_attribute_names.include?(name.to_sym)
end