module Muffin::Attributes::ClassMethods
Public Instance Methods
attribute(name, type = String, default: nil, array: nil, permit: nil, permitted_values: nil, &block)
click to toggle source
# File lib/muffin/frostings/attributes.rb, line 9 def attribute(name, type = String, default: nil, array: nil, permit: nil, permitted_values: nil, &block) type = define_class name, block if block attributes[name] = Muffin::Attribute.new name: name, type: type, default: default, array: array, permit: permit, permitted_values: permitted_values, block: block define_method name do attributes && attributes[name] end define_method "#{name}=" do |value| @attributes ||= {} value = self.class.attributes[name].coercise(value) attributes[name] = value if permit_attribute!(name, value) end end
attributes()
click to toggle source
# File lib/muffin/frostings/attributes.rb, line 22 def attributes @attributes ||= {} end
introspect(name)
click to toggle source
# File lib/muffin/frostings/attributes.rb, line 26 def introspect(name) attributes[name] end
reflect_on_association(name)
click to toggle source
# File lib/muffin/frostings/attributes.rb, line 30 def reflect_on_association(name) OpenStruct.new klass: attributes[name].type end
Private Instance Methods
define_class(name, block)
click to toggle source
# File lib/muffin/frostings/attributes.rb, line 36 def define_class(name, block) class_name = name.to_s.split("_").map(&:capitalize).join return const_get class_name if const_defined? class_name klass = const_set class_name, Class.new(Muffin::NestedAttribute) klass.instance_eval(&block) klass end