3

I have two classes; customer and reservation. And my project is consist of only ruby code, not rails project.

Class reservation reads bulk json file line by line which includes customer hash.

From that hash, I create customer object.

Here's the code within the reservation class.

    def parse_json
        File.open(@filename, "r" ).each do |line|    
            @customers << Customer.new(JSON.parse(line), @coordinates)
        end

        return @customers
    end

And in customer.rb I have the following;

require 'active_model' 

class Customer 

    include ActiveModel::Validations

    validates_presence_of :hash, :coordinates
    attr_reader :name, :userid, :latitude, :longitude, :distance  

    def initialize(hash, coordinates)
        @userid = hash['user_id']
        @name = hash['name']
        @latitude = convert_degree_to_radian(hash['latitude'].to_f)
        @longitude = convert_degree_to_radian(hash['longitude'].to_f)
        @distance = calculate_distance(coordinates['latitude'], coordinates['longitude'])
    end

I have 2 problems while creating Customer object in Reservation class;

The first one is, I'm having nomethod error for "validates_presence_of" method;

`block in validate': undefined method `coordinates' for #<Customer:0x007ff6631d6698> (NoMethodError)

The second one is, since I'm creating new objects with new method. It does not check the validations. For example, if I send nil objects, in initialize method, I'll get no method error for nil class.

How should I handle validations while creating those objects in pure ruby code?

Thanks.

CanCeylan
  • 2,890
  • 8
  • 41
  • 51
  • Why are you validating presence of coordinates? By including it as an argument to initialize, it must be present. – margo Aug 20 '15 at 21:19

1 Answers1

1

I think this may be because you don't have a method called coordinates in your Customer class.

Try adding it to your attr_reader

attr_reader :name, :userid, :latitude, :longitude, :distance, :coordinates
AJFaraday
  • 2,411
  • 1
  • 16
  • 39
  • but I don't have a field called coordinates, plus it doesn't give error for 'hash' even though it's not included in attr_reader as well – CanCeylan Aug 20 '15 at 15:19
  • My guess would be that it's simply reached co-ordinates first, and not continued validations after throwing the error. If you remove 'coordinates' from the validates_presence_of you'll probably find you get the undefined method error for hash instead. – AJFaraday Aug 20 '15 at 15:21
  • Nope, unfortunately, I tried that one too, but I'm not getting undefined method error for hash. – CanCeylan Aug 20 '15 at 15:22
  • 2
    My mistake, every object in Ruby has a method called hash, and it's not what you think (http://ruby-doc.org/core-2.2.2/Object.html#method-i-hash). I would recommend you name hash something different (such as arguments, or options). The fact that there is a method called hash is misleading you into thinking that validations are checking for the presence of the hash attribute you've created. – AJFaraday Aug 20 '15 at 15:25