Sha256: 7da1f16b48f0580507bb750d2430f88e681ea52f2d05e75a6a5ec348947567c4

Contents?: true

Size: 918 Bytes

Versions: 11

Compression:

Stored size: 918 Bytes

Contents

# frozen_string_literal: true

# (c) Copyright 2021 Ribose Inc.
#

module Glossarist
  class Model
    def self.new(params = {})
      return params if params.is_a?(self)

      super
    end

    def initialize(attributes = {})
      attributes.each_pair { |k, v| set_attribute(k, v) }
    end

    def set_attribute(name, value)
      public_send("#{name}=", value)
    rescue NoMethodError
      if Config.extension_attributes.include?(name)
        extension_attributes[name] = value
      elsif name.match?(/[A-Z]/) # adding support for camel case
        name = snake_case(name.to_s).to_sym
        retry
      else
        raise ArgumentError, "#{self.class.name} does not have " +
          "attribute #{name} defined or the attribute is read only."
      end
    end

    def self.from_h(hash)
      new(hash)
    end

    def snake_case(str)
      str.gsub(/([A-Z])/) { "_#{$1.downcase}" }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
glossarist-2.3.0 lib/glossarist/model.rb
glossarist-2.2.1 lib/glossarist/model.rb
glossarist-2.2.0 lib/glossarist/model.rb
glossarist-2.1.0 lib/glossarist/model.rb
glossarist-2.0.10 lib/glossarist/model.rb
glossarist-2.0.9 lib/glossarist/model.rb
glossarist-2.0.8 lib/glossarist/model.rb
glossarist-2.0.7 lib/glossarist/model.rb
glossarist-2.0.6 lib/glossarist/model.rb
glossarist-2.0.5 lib/glossarist/model.rb
glossarist-2.0.4 lib/glossarist/model.rb