Class: Statinize::Attribute

Inherits:
Object show all
Includes:
Comparable
Defined in:
lib/statinize/attribute.rb,
lib/statinize/attribute/options.rb,
lib/statinize/attribute/options/conditions.rb,
lib/statinize/attribute/options_collection.rb

Defined Under Namespace

Modules: Options Classes: OptionsCollection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, options) ⇒ Attribute

Returns a new instance of Attribute.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/statinize/attribute.rb', line 8

def initialize(klass, name, options)
  @klass = klass
  @name = name
  @options = options
  @options_collection = OptionsCollection.new # TODO: think of a better way
  @options_collection << options.clone.extend(Options) unless options.empty?

  @default = options[:default] if options.key?(:default)
  @default_exec = options[:default_exec] if options.key?(:default_exec)
  @arg_name = name
  @name = options[:name] || name
end

Instance Attribute Details

#arg_nameObject (readonly)

Returns the value of attribute arg_name.



5
6
7
# File 'lib/statinize/attribute.rb', line 5

def arg_name
  @arg_name
end

#defaultObject (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/statinize/attribute.rb', line 5

def default
  @default
end

#default_execObject (readonly)

Returns the value of attribute default_exec.



5
6
7
# File 'lib/statinize/attribute.rb', line 5

def default_exec
  @default_exec
end

#klassObject (readonly)

Returns the value of attribute klass.



5
6
7
# File 'lib/statinize/attribute.rb', line 5

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/statinize/attribute.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/statinize/attribute.rb', line 6

def options
  @options
end

#options_collectionObject

Returns the value of attribute options_collection.



6
7
8
# File 'lib/statinize/attribute.rb', line 6

def options_collection
  @options_collection
end

Class Method Details

.create(klass, name, options = {}) ⇒ Object



21
22
23
# File 'lib/statinize/attribute.rb', line 21

def self.create(klass, name, options = {})
  new(klass, name, options).create
end

Instance Method Details

#<=>(other) ⇒ Object



46
47
48
49
50
# File 'lib/statinize/attribute.rb', line 46

def <=>(other)
  name == other.name &&
    options == other.options &&
    klass == other.klass
end

#add_options(opts) ⇒ Object



42
43
44
# File 'lib/statinize/attribute.rb', line 42

def add_options(opts)
  options_collection << opts.extend(Options)
end

#createObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/statinize/attribute.rb', line 25

def create
  attribute? ? override : add_attribute

  klass.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def #{name}
      @#{name}
    end

    def #{name}=(attr)
      @#{name} = attr
      validate if respond_to?(:validate)
      attr
    end
  RUBY_EVAL
  self
end