Module: QuickTravel::InitFromHash
- Included in:
- Adapter, Address, Adjustment, BackgroundCheck, BedConfiguration, BedRequirement, Client, Contact, Passenger, PassengerPriceBreak, PassengerSplit, PropertyFacility, ResourceGroup, RoomFacility, RouteStop, Search, Service, Stop
- Defined in:
- lib/quick_travel/init_from_hash.rb
Instance Method Summary collapse
Instance Method Details
#define_readers(keys) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/quick_travel/init_from_hash.rb', line 17 def define_readers(keys) keys.each do |key| next if respond_to?(key) define_singleton_method(key) { instance_variable_get("@#{key}") } if key.to_s.ends_with? '_cents' name = key.to_s.gsub(/_in_cents$/, '') define_singleton_method(name) { cents = instance_variable_get("@#{key}") return nil unless cents Money.new(cents) } end end end |
#initialize(hash = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/quick_travel/init_from_hash.rb', line 3 def initialize(hash = {}) return nil if hash.blank? define_readers(hash.keys) Parser.new(hash).parsed_attributes.each do |attr, val| # set datamember of the object using hash key and value if respond_to?("#{attr}=") send("#{attr}=", val) else instance_variable_set("@#{attr}".to_sym, val) end end end |
#to_hash ⇒ Object
32 33 34 |
# File 'lib/quick_travel/init_from_hash.rb', line 32 def to_hash instance_values end |