Module: AttrHelper::ClassMethods

Defined in:
lib/attr_helper.rb

Instance Method Summary collapse

Instance Method Details

#attr_optional(name, options = {}) ⇒ Object



42
43
44
45
# File 'lib/attr_helper.rb', line 42

def attr_optional(name, options = {})
  optional_attributes << BaseAttr.new(name.to_sym, options)
  attr_accessor name.to_sym
end

#attr_required(name, options = {}) ⇒ Object



51
52
53
54
# File 'lib/attr_helper.rb', line 51

def attr_required(name, options = {})
  required_attributes << RequiredAttr.new(name.to_sym, options)
  attr_accessor name.to_sym
end

#inherited(klass) ⇒ Object



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/attr_helper.rb', line 11

def inherited(klass)
  super

  # we might want to undef here so
  #  that child classes can override

  unless required_attributes.empty?
    required_attributes.each do |attribute|
      klass.attr_required attribute.name, {
        :name => attribute.name,
        :key => attribute.key,
        :default => attribute.default,
        :serialize => attribute.serialize,
        :if => attribute.if_cond,
        :unless => attribute.unless_cond
      }
    end
  end

  unless optional_attributes.empty?
    optional_attributes.each do |attribute|
      klass.attr_optional attribute.name, {
        :name => attribute.name,
        :key => attribute.key,
        :default => attribute.default,
        :serialize => attribute.serialize
      }
    end
  end
end

#optional_attributesObject



47
48
49
# File 'lib/attr_helper.rb', line 47

def optional_attributes
  @optional_attributes ||= []
end

#required_attributesObject



56
57
58
# File 'lib/attr_helper.rb', line 56

def required_attributes
  @required_attributes ||= []
end