Module: Cieloz::Helpers::ClassMethods

Defined in:
lib/cieloz/helpers.rb

Instance Method Summary collapse

Instance Method Details

#attrs_from(source, opts, *keys) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cieloz/helpers.rb', line 18

def attrs_from source, opts, *keys
  attrs = keys.map { |k|
    value_or_attr_name = opts[k] || k
    if value_or_attr_name.is_a? Symbol
      source.send value_or_attr_name if source.respond_to? value_or_attr_name
    else
      value_or_attr_name
    end
  }
  attrs.count == 1 ? attrs.first : attrs
end

#hattr_writer(*attrs) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/cieloz/helpers.rb', line 4

def hattr_writer *attrs
  attrs.each { |attr|
    define_method "#{attr}=" do |value|
      if value.is_a? Hash
        name = attr.to_s.camelize
        cls = self.class.const_get name
        value = cls.new(value)
      end
      instance_variable_set "@#{attr}", value
      yield(value) if block_given?
    end
  }
end