Module: Pgai::Resources::Attributes::ClassMethods
- Defined in:
- lib/pgai/resources/attributes.rb
Instance Method Summary collapse
- #attribute(name, type, default: nil) ⇒ Object
- #attributes ⇒ Object
- #generate_attribute_methods(attribute) ⇒ Object
- #inherited(subclass) ⇒ Object
Instance Method Details
#attribute(name, type, default: nil) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/pgai/resources/attributes.rb', line 62 def attribute(name, type, default: nil) Attribute.new(name, type, default) do |attribute| attributes << attribute generate_attribute_methods(attribute) end end |
#attributes ⇒ Object
58 59 60 |
# File 'lib/pgai/resources/attributes.rb', line 58 def attributes @attributes ||= [] end |
#generate_attribute_methods(attribute) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pgai/resources/attributes.rb', line 69 def generate_attribute_methods(attribute) define_method(attribute.name) do instance_variable_get(:"@#{attribute.name}") end define_method("#{attribute.name}=") do |value| instance_variable_set(:"@#{attribute.name}", value) attributes[attribute.name.to_sym] = value end end |
#inherited(subclass) ⇒ Object
52 53 54 55 56 |
# File 'lib/pgai/resources/attributes.rb', line 52 def inherited(subclass) attributes.each do |attr| subclass.attribute attr.name, attr.type, default: attr.default end end |