0

I have a class that has instance variables made of hashes.

require 'hashie'

class CardBrandFees < Hashie::Trash
  property :assessments, from: :assessmentss
  attr_reader :discover, :visa, :mastercard, :mc_high_ticket, :assessments, :APF

  def initialize(ticket)
    @ticket          = @ticket
    @discover        = {:assessments => 0.00105, :APF => 0.0195}
    @visa            = {:assessments => 0.00110, :APF => 0.0195}
    @mastercard      = {:assessments => 0.00110, :APF => 0.0195}
    @mc_high_ticket  = {:assessments => 0.00010}
  end
end

I would like to make a card brand object and be able to access these hashes via dot notation so:

cardbrand = CardBrandFees.new(ticket)
cardbrand.discover.assessments => 0.00105

I am having considerable difficulty accomplishing this, and not sure its even the most effective way to set this simple class up, I have tried hashie, but cant get that working, does anyone have a simple solution for this situation?

1
  • OpenStruct can do this for you. You could also do it with a simple class that provides assessments and APF attr_readers Commented Nov 29, 2014 at 18:34

1 Answer 1

1
require 'ostruct'

class CardBrandFees
  attr_reader :discover, :visa, :mastercard, :mc_high_ticket, :assessments, :APF

  def initialize(ticket)
    @ticket         = ticket
    @discover       = OpenStruct.new(:assessments => 0.00105, :APF => 0.0195)
    @visa           = OpenStruct.new(:assessments => 0.00110, :APF => 0.0195)
    @mastercard     = OpenStruct.new(:assessments => 0.00110, :APF => 0.0195)
    @mc_high_ticket = OpenStruct.new(:assessments => 0.00010)
  end
end


p CardBrandFees.new("ticket").discover.assessments # => 0.00105
p CardBrandFees.new("ticket").mastercard.APF # => 0.0195
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.