class Booker::Model
Constants
- CONSTANTIZE_MODULE
Public Class Methods
constantize(key)
click to toggle source
# File lib/booker/model.rb, line 62 def self.constantize(key) begin self::CONSTANTIZE_MODULE.const_get key.to_s.camelize.singularize rescue NameError nil end end
from_hash(hash)
click to toggle source
# File lib/booker/model.rb, line 34 def self.from_hash(hash) model = self.new hash.each do |k, v| if model.respond_to?(:"#{k}") constantized = self.constantize(k) if constantized if v.is_a?(Array) && v.first.is_a?(Hash) model.send(:"#{k}=", constantized.from_list(v)) next elsif v.is_a? Hash model.send(:"#{k}=", constantized.from_hash(v)) next end end if v.is_a?(String) && v.start_with?('/Date(') model.send(:"#{k}=", try(:time_from_booker_datetime, v) || v) next end model.send(:"#{k}=", v) end end model end
from_list(array)
click to toggle source
# File lib/booker/model.rb, line 60 def self.from_list(array); array.map { |item| self.from_hash(item) }; end
new(options = {})
click to toggle source
# File lib/booker/model.rb, line 5 def initialize(options = {}) @attributes = [] options.each do |k, v| send(:"#{k}=", v) @attributes << k.to_sym end end
Protected Class Methods
response_results_key()
click to toggle source
# File lib/booker/model.rb, line 72 def self.response_results_key self.to_s.demodulize end
Public Instance Methods
to_hash()
click to toggle source
# File lib/booker/model.rb, line 13 def to_hash hash = {} @attributes.each do |attr| value = self.send(attr) if value.is_a? Array new_value = hash_list(value) elsif value.is_a? Booker::Model new_value = value.to_hash elsif value.is_a? Time new_value = self.class.try(:time_to_booker_datetime, value) || value elsif value.is_a? Date time = value.in_time_zone new_value = self.class.try(:time_to_booker_datetime, time) || value else new_value = value end hash[attr] = new_value end hash end
to_json()
click to toggle source
# File lib/booker/model.rb, line 58 def to_json; Oj.dump(to_hash, mode: :compat); end
Private Instance Methods
hash_list(array)
click to toggle source
# File lib/booker/model.rb, line 78 def hash_list(array) array.map do |item| if item.is_a? Array hash_list(item) elsif item.is_a? Booker::Model item.to_hash else item end end end