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
60 61 62 63 64 65 |
# File 'lib/pgai/resources/attributes.rb', line 60 def attribute(name, type, default: nil) Attribute.new(name, type, default) do |attribute| attributes << attribute generate_attribute_methods(attribute) end end |
#attributes ⇒ Object
56 57 58 |
# File 'lib/pgai/resources/attributes.rb', line 56 def attributes @attributes ||= [] end |
#generate_attribute_methods(attribute) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/pgai/resources/attributes.rb', line 67 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
50 51 52 53 54 |
# File 'lib/pgai/resources/attributes.rb', line 50 def inherited(subclass) attributes.each do |attr| subclass.attribute attr.name, attr.type, default: attr.default end end |