Module: ZTK::DSL::Core::Attributes::ClassMethods Private

Defined in:
lib/ztk/dsl/core/attributes.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Author:

Instance Method Summary (collapse)

Instance Method Details

- (Object) attribute(key, options = {})

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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