Sha256: 524d269e0c009d454bae9d029434849cd1af36e2e2cd3b3f4e94680dd9cedb84

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

module HashieModel
  class Base < Hashie::Dash
    extend ActiveModel::Naming
    
    include Hashie::Extensions::Coercion
    
    include ActiveModel::AttributeMethods
    include ActiveModel::Conversion
    include ActiveModel::Validations
    
    attribute_method_suffix '?', '_before_type_cast'
    
    class << self
      def prop(name, type = nil, options = {})
        if type.is_a?(Hash)
          options = type
          type    = nil
        end
        
        property(name, options)
        
        if type
          coerce_key(name.to_sym, type)
          coerce_key(name.to_s, type)
        end
        
        define_attribute_methods [name]
        alias_method :"#{name}_without_type_cast=", :"#{name}="
        
        define_method(:"#{name}=") do |value|
          attributes_before_type_cast[name.to_s] = value
          send(:"#{name}_without_type_cast=", value)
        end
      end
      
      def from_json(json)
        new(ActiveSupport::JSON.decode(json))
      end
      
      def validates_associated(*attr_names)
        validates_with HashieModel::AssociatedValidator, _merge_attributes(attr_names)
      end
    end
    
    def attributes_before_type_cast
      @attributes_before_type_cast ||= {}
    end
    
    def attributes
      {}.tap do |attrs|
        self.class.properties.each do |key|
          attrs[key] = self[key]
        end
      end
    end
    
    def persisted?
      false
    end
    
    protected
      def attribute?(key)
        self[key].present?
      end
      
      def reset_attribute(key)
        self[key] = self.class.defaults[key.to_sym]
      end
      
      def attribute_before_type_cast(key)
        attributes_before_type_cast[key.to_s]
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
hashie-model-1.2.3 lib/hashie_model/base.rb
hashie-model-1.2.1 lib/hashie_model/base.rb
hashie-model-1.2.0 lib/hashie_model/base.rb
hashie-model-1.1.0 lib/hashie_model/base.rb
hashie-model-1.0.1 lib/hashie_model/base.rb
hashie-model-1.0.0.alpha lib/hashie_model/base.rb