Module: ApiResource::Attributes::ClassMethods

Defined in:
lib/api_resource/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/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/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/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 "      def \#{arg}\n        self.attributes[:\#{arg}]\n      end\n    \n      def \#{arg}=(val)\n        \#{arg}_will_change! unless self.\#{arg} == val\n        self.attributes[:\#{arg}] = val\n      end\n      \n      def \#{arg}?\n        self.attributes[:\#{arg}].present?\n      end\n    EOE\n  end\n  self.attribute_names.uniq!\n  self.public_attribute_names.uniq!\nend\n", __FILE__, __LINE__ + 1

#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/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 "\n      def \#{arg}\n        self.attributes[:\#{arg}]\n      end\n    \n      def \#{arg}=(val)\n        raise \"\#{arg} is a protected attribute and cannot be set\"\n      end\n      \n      def \#{arg}?\n        self.attributes[:\#{arg}].present?\n      end\n    EOE\n  end\n  self.attribute_names.uniq!\n  self.protected_attribute_names.uniq!\nend\n", __FILE__, __LINE__ + 1

#protected_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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