Sha256: c6c28bb38a4308be57f7f21c15510959c48b278b546f3304f5d78e3ca1fcdbf0

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

module EWayClient
  class Province

    DISTRICT_KEY_FORMAT = /districts\d/
    TIME_KEY_FORMAT = /time\d/
    TYPES = %i[districts time]
    TYPES_COUNT = 5

    include Virtus.model
    attribute :data, Hash
    attribute :province_id, Integer, lazy: true, default: :default_province_id
    attribute(:province_name_eng, String, {
      lazy: true,
      default: :default_province_name_eng,
    })
    attribute(:province_name_viet, String, {
      lazy: true,
      default: :default_province_name_viet,
    })
    attribute(:response_code, String, {
      lazy: true,
      default: :default_response_code,
    })
    attribute :districts, Array[String], lazy: true, default: :default_districts
    TYPES_COUNT.times do |n|
      count = n+1
      attribute(:"districts#{count}", String, {
        lazy: true,
        default: :"default_districts#{count}",
      })

      attribute(:"time#{count}", String, {
        lazy: true,
        default: :"default_time#{count}",
      })
    end

    private

    def default_province_id
      data[:province_id]
    end

    def default_response_code
      data[:response_code]
    end

    def default_province_name_eng
      data[:province_name_eng]
    end

    def default_province_name_viet
      data[:province_name_viet]
    end

    def default_districts
      (1..TYPES_COUNT).each_with_object([]) do |n, array|
        array.concat(self.send("districts#{n}"))
      end
    end

    TYPES_COUNT.times do |n|
      count = n+1
      define_method "default_districts#{count}" do
        districts_str = data[:"districts#{count}"]
        if districts_str.blank?
          []
        else
          districts_str.split(",").map do |district_name|
            district_name.strip.gsub(/\.$/, "")
          end
        end
      end

      define_method "default_time#{count}" do
        data[:"time#{count}"]
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
e_way_client-2.1.0 lib/e_way_client/models/province.rb
e_way_client-2.0.0 lib/e_way_client/models/province.rb
e_way_client-1.2.1 lib/e_way_client/models/province.rb
e_way_client-1.2.0 lib/e_way_client/models/province.rb
e_way_client-1.1.0 lib/e_way_client/models/province.rb
e_way_client-1.0.1 lib/e_way_client/models/province.rb
e_way_client-1.0.0 lib/e_way_client/models/province.rb
e_way_client-0.2.0 lib/e_way_client/models/province.rb
e_way_client-0.1.1 lib/e_way_client/models/province.rb