Class: Statinize::Attribute

Inherits:
Object
  • 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, opts) ⇒ Attribute

Returns a new instance of Attribute.



8
9
10
11
12
13
# File 'lib/statinize/attribute.rb', line 8

def initialize(klass, name, opts)
  @klass = klass
  @name = name
  @options = OptionsCollection.new
  @options << opts.clone.extend(Options) unless opts.empty?
end

Instance Attribute Details

#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

Class Method Details

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



15
16
17
# File 'lib/statinize/attribute.rb', line 15

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

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
46
47
# File 'lib/statinize/attribute.rb', line 43

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

#add_options(opts) ⇒ Object



39
40
41
# File 'lib/statinize/attribute.rb', line 39

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

#createObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/statinize/attribute.rb', line 19

def create
  if attribute?
    statinizer.attributes.find { |a| a.name == name }.options = options
  else
    statinizer.add_attribute(self)
  end
  klass.class_eval "    def \#{name}\n      @\#{name}\n    end\n\n    def \#{name}=(attr)\n      @\#{name} = attr\n      validate if respond_to?(:validate)\n      attr\n    end\n  RUBY_EVAL\n  self\nend\n", __FILE__, __LINE__ + 1