whizcreed
whizcreed

Reputation: 2762

Unable to extend faker gem

I am following instructions from: http://cloudspace.com/blog/2013/10/18/extending-faker/#.VLdumx9sY8o

My /config/locales/faker.en.yml looks like:

en:
  faker:
    girls: 
      first_name: ["priyanka", "Tanya", "aditi", "Tanvi"]
      last_name: ["Acharya", "Agarwal", "Agate", "Aggarwal"]
      name: 
       - "#{first_name} #{last_name}"

And I have following: /lib/faker/girls.rb looks like:

module Faker
  class Girl < Base
    class << self
      def first_name
        parse('girls.first_name')
      end

      def last_name
        parse('girls.last_name')
      end

      def name
        fetch('girls.name')
      end
    end
  end
end

Right after starting rails console I run: require Rails.root.join 'lib/faker/girls' to which a true is returned.

After that running following commands do not work as expected.

Output:

2.1.1 :004 > Faker::Girl.first_name  => "" 
2.1.1 :005 > Faker::Girl.last_name  => "" 
2.1.1 :006 > Faker::Girl.name  => "\#{first_name} \#{last_name}"

Please help me find where I went wrong..

Upvotes: 1

Views: 290

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You mixed parse and fetch up: simple properties are to be fetched while composed are to be parsed. Another glitch is that your class name should correspond the yml (by convention):

#         ⇓
class Girls < Base
  class << self
    def first_name
     #⇓⇓⇓⇓⇓ it is a simple property 
      fetch('girls.first_name')
    end

    def last_name
     #⇓⇓⇓⇓⇓ it is a simple property 
      fetch('girls.last_name')
    end

    def name
     #⇓⇓⇓⇓⇓ it is a composed property 
      parse('girls.name')
    end
  ...

Hope it helps.

Upvotes: 1

Related Questions