module ZTK::DSL::Core::Attributes::ClassMethods
@author Zachary Patten <zpatten AT jovelabs DOT io>
Public Instance Methods
attribute(key, options={})
click to toggle source
# File lib/ztk/dsl/core/attributes.rb, line 22 def attribute(key, options={}) klass = self.to_s.split('::').last.downcase option_key = "#{klass}_#{key}" attribute_options[option_key] = options send(:define_method, key) do |*args| if (attributes[key].nil? && !self.class.attribute_options[option_key][:default].nil?) default_value = (self.class.attribute_options[option_key][:default].dup rescue self.class.attribute_options[option_key][:default]) attributes[key] ||= default_value end if args.count == 0 attributes[key] else send("#{key}=", *args) end end send(:define_method, "#{key}=") do |value| attributes[key] = value value end self.class.send(:define_method, "find_by_#{key}") do |value| all.select{ |object| (object.send(key) == value) } end end