Class: InBedWith::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/in_bed_with/base.rb

Direct Known Subclasses

Adsense, Analytics, Gauges

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/in_bed_with/base.rb', line 19

def initialize(attributes={})
  attributes.symbolize_keys!

  self.class.attributes.each do |attribute, options|
    value = attributes[attribute] || InBedWith.send(self.class.module_attr(attribute))

    raise ArgumentError, missing(attribute) unless value || options[:optional]

    self.send "#{attribute}=", value
  end
end

Class Attribute Details

.attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/in_bed_with/base.rb', line 4

def attributes
  @attributes
end

Class Method Details

.module_attr(attribute) ⇒ Object



14
15
16
# File 'lib/in_bed_with/base.rb', line 14

def module_attr(attribute)
  "#{name.demodulize.underscore}_#{attribute}"
end

.register(attribute, options = {}) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/in_bed_with/base.rb', line 6

def register(attribute, options={})
  InBedWith.mattr_accessor module_attr(attribute)
  attr_accessor attribute

  @attributes ||= {}
  @attributes.merge!({ attribute.to_sym => options })
end