Module: Attributes

Included in:
Module
Defined in:
lib/alib-0.5.1/attributes.rb,
lib/alib-0.5.1/attributes-3.2.0.rb

Constant Summary collapse

VERSION =
'3.2.0'

Instance Method Summary collapse

Instance Method Details

#attribute(*a, &b) ⇒ Object



42
# File 'lib/alib-0.5.1/attributes.rb', line 42

def attribute(*a, &b) attributes *a, &b end

#attributes(*a, &b) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/alib-0.5.1/attributes.rb', line 6

def attributes *a, &b
  unless a.empty?
    hashes, names = a.partition{|x| Hash === x}

    names_and_defaults = {}
    hashes.each{|h| names_and_defaults.update h}
    names.flatten.compact.each{|name| names_and_defaults.update name => nil}

    names_and_defaults.each do |name, default|
      init = b || lambda { default }
      ivar, getter, setter, query = "@#{ name }", "#{ name }", "#{ name }=", "#{ name }?"

      define_method(setter) do |value|
        instance_variable_set ivar, value
      end

      define_method(getter) do |*value|
        unless value.empty?
          send setter, value.shift
        else
          defined = instance_eval "defined? #{ ivar }"
          send setter, instance_eval(&init) unless defined
          instance_variable_get ivar
        end
      end

      alias_method query, getter

      (attributes << name).uniq!
      attributes
    end
  else
    @attributes ||= [] 
  end
end

#versionObject



4
# File 'lib/alib-0.5.1/attributes.rb', line 4

def version() VERSION end