Module: Kangaroo::Attributes::ClassMethods
- Defined in:
- lib/kangaroo/model/attributes.rb
Instance Method Summary collapse
-
#attribute_names ⇒ Array
Get a list of available attributes.
-
#define_accessors(attribute_name) ⇒ Object
Define getter and setter for an attribute.
-
#define_getter(attribute_name) ⇒ Object
Define getter.
-
#define_getters(*attribute_names) ⇒ Object
Define getters for attributes.
-
#define_multiple_accessors(*attribute_names) ⇒ Object
Define getters and setters for attributes.
-
#extend_attribute_methods(*attributes) ⇒ Object
If you need to customize your models, e.g.
Instance Method Details
#attribute_names ⇒ Array
Get a list of available attributes
93 94 95 |
# File 'lib/kangaroo/model/attributes.rb', line 93 def attribute_names @attribute_names ||= [] end |
#define_accessors(attribute_name) ⇒ Object
Define getter and setter for an attribute
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kangaroo/model/attributes.rb', line 100 def define_accessors attribute_name define_method attribute_name do read_attribute attribute_name end define_method "#{attribute_name}=" do |value| write_attribute attribute_name, value end attribute_names << attribute_name.to_s end |
#define_getter(attribute_name) ⇒ Object
Define getter
84 85 86 87 88 |
# File 'lib/kangaroo/model/attributes.rb', line 84 def define_getter attribute_name define_method attribute_name do read_attribute attribute_name end end |
#define_getters(*attribute_names) ⇒ Object
Define getters for attributes
75 76 77 78 79 |
# File 'lib/kangaroo/model/attributes.rb', line 75 def define_getters *attribute_names attribute_names.flatten.each do |name| define_getter name end end |
#define_multiple_accessors(*attribute_names) ⇒ Object
Define getters and setters for attributes
115 116 117 118 119 120 121 |
# File 'lib/kangaroo/model/attributes.rb', line 115 def define_multiple_accessors *attribute_names define_attribute_methods attribute_names.map(&:to_s) attribute_names.each do |attribute_name| define_accessors attribute_name end end |
#extend_attribute_methods(*attributes) ⇒ Object
If you need to customize your models, e.g. add attributes not covered by fields_get, you can call #extend_attribute_methods
65 66 67 68 69 70 |
# File 'lib/kangaroo/model/attributes.rb', line 65 def extend_attribute_methods *attributes attributes.flatten.each do |attr| next if attribute_names.include?(attr.to_s) define_accessors attr end end |