Sha256: 8f108c281e94d73154b0267e300f00dcbc95bcf86ec51272b40c710e09672d27

Contents?: true

Size: 924 Bytes

Versions: 4

Compression:

Stored size: 924 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
      # adding support for camel case
      if name.match?(/[A-Z]/)
        name = snake_case(name.to_s).to_sym
        retry
      elsif Config.extension_attributes.include?(name)
        extension_attributes[name] = value
      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

4 entries across 4 versions & 1 rubygems

Version Path
glossarist-2.0.3 lib/glossarist/model.rb
glossarist-2.0.2 lib/glossarist/model.rb
glossarist-2.0.1 lib/glossarist/model.rb
glossarist-2.0.0 lib/glossarist/model.rb