Module: OpenidRegistrable::ClassMethods

Defined in:
lib/openid_registrable/openid_registrable.rb

Instance Method Summary collapse

Instance Method Details

#openid_registrable(options) ⇒ Object

Using:

class User < ActiveRecord::Base

openid_registrable :required_fields => {
    :email => 'http://axschema.org/contact/email',
  },
  :optional_fields => {
    :first_name => 'http://axschema.org/namePerson/first',
    :last_name => 'http://axschema.org/namePerson/last',
  }

end



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/openid_registrable/openid_registrable.rb', line 23

def openid_registrable(options)
  [:required_fields, :optional_fields].each do |fields|
    unless options[fields].empty?
      (class << self; self; end).instance_eval do
        # define methods openid_require_fields and openid_optional_fields
        # methods returns array generated from options given in #openid_registrable, e.g.:
        # => ['http://axschema.org/namePerson/first', 'http://axschema.org/namePerson/last']
        define_method "openid_#{fields}" do
          options[fields].values
        end
      end
    end
  end

  # define method :openid_fields_to_model_fields_mapping
  # method returns hash genered from options given in #openid_registrable, e.g.:
  # => {:first_name=>"http://axschema.org/namePerson/first",
  #     :last_name=>"http://axschema.org/namePerson/last"}

  if options[:required_fields] or options[:optional_fields]
    (class << self; self; end).instance_eval do
      define_method :openid_fields_to_model_fields_mapping do
        mapping = {}
        mapping.merge!(options[:required_fields]) if options[:required_fields]
        mapping.merge!(options[:optional_fields]) if options[:optional_fields]
      end
    end
  end

  send :include, InstanceMethods
end