Sha256: 4f90f1d8b8f8684365fd000982fa3a1bc406b22d0abb0bfb93f6bf60504e3cda
Contents?: true
Size: 1.26 KB
Versions: 2
Compression:
Stored size: 1.26 KB
Contents
# frozen_string_literal: true require 'mitake/model/attributes' require 'mitake/model/accessor' module Mitake # Provide attributes accessor # # @since 0.1.0 # @api private module Model # @since 0.1.0 # @api private def self.included(base) base.class_eval do include Attributes extend Accessor end end # Assign attribute by hash # # @see Mitake::Model::Attributes#assign_attributes # # @param attributes [Hash] the attributes to assignment # # @since 0.1.0 def assign_attributes(attributes = {}) attributes.each do |key, value| next unless self.class.attribute_names.include?(key.to_s) next send("#{key}=", value) if respond_to?("#{key}=") type = self.class.attributes[key.to_s] instance_variable_set("@#{key}", self.class.cast(value, type)) end end # Get attributes as hash # # @return [Hash] the object attributes # # @since 0.1.0 def attributes self .class .attribute_names .map { |attr| [attr, send(attr)] } .reject { |_, value| value.nil? } .map do |attr, value| [attr, value.respond_to?(:attributes) ? value.attributes : value] end .to_h end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
mitake-0.1.1 | lib/mitake/model.rb |
mitake-0.1.0 | lib/mitake/model.rb |