type_attributes 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/.gitignore +2 -0
 - data/.rspec +1 -0
 - data/.travis.yml +5 -1
 - data/Appraisals +11 -0
 - data/gemfiles/4.2_stable.gemfile +7 -0
 - data/gemfiles/5.0_stable.gemfile +7 -0
 - data/gemfiles/5.1_stable.gemfile +7 -0
 - data/lib/type_attributes/type.rb +39 -0
 - data/lib/type_attributes/version.rb +1 -1
 - data/lib/type_attributes.rb +18 -43
 - data/type_attributes.gemspec +1 -0
 - metadata +22 -3
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA1:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 568c6c18b2a0e5f149b01461d504d615bf5b8275
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: bd21de7e8dbeab31240e679b3bbe2eb53628fde5
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: d22f85713cdbc87dc838ee9b3fa9a0c4767f065087150e6426e4335f01f82b35158ce0bcb3acd3844e62a0b4e74c9d092efc5883cce835eeb37310ab54aa4c50
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 8b7e5dc5b6e227c662665265bd1346ffded0650cafd49a39282ab09d1fdad5e41239da96258cdc7a0a1c10343aa15e1b0ef7630d9d71f1f6009d5bf9a7a674cf
         
     | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/.rspec
    CHANGED
    
    
    
        data/.travis.yml
    CHANGED
    
    
    
        data/Appraisals
    ADDED
    
    | 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            appraise '4.2-stable' do
         
     | 
| 
      
 2 
     | 
    
         
            +
              gem 'rails', git: 'https://github.com/rails/rails', branch: '4-2-stable'
         
     | 
| 
      
 3 
     | 
    
         
            +
            end
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            appraise '5.0-stable' do
         
     | 
| 
      
 6 
     | 
    
         
            +
              gem 'rails', git: 'https://github.com/rails/rails', branch: '5-0-stable'
         
     | 
| 
      
 7 
     | 
    
         
            +
            end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
            appraise '5.1-stable' do
         
     | 
| 
      
 10 
     | 
    
         
            +
              gem 'rails', git: 'https://github.com/rails/rails', branch: '5-1-stable'
         
     | 
| 
      
 11 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,39 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'active_model'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module TypeAttributes
         
     | 
| 
      
 4 
     | 
    
         
            +
              module Type
         
     | 
| 
      
 5 
     | 
    
         
            +
                IS_RAILS_5 = defined?(ActiveModel) && ActiveModel.gem_version >= Gem::Version.new('5.0.0')
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                # Change type name for DateTime in Rails5
         
     | 
| 
      
 8 
     | 
    
         
            +
                # related: https://github.com/rails/rails/pull/24079
         
     | 
| 
      
 9 
     | 
    
         
            +
                DATETIME_TYPE = IS_RAILS_5 ? :datetime : :date_time
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                def self.flexible_type_name(type)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  if %i(date_time datetime).include?(type)
         
     | 
| 
      
 13 
     | 
    
         
            +
                    DATETIME_TYPE
         
     | 
| 
      
 14 
     | 
    
         
            +
                  else
         
     | 
| 
      
 15 
     | 
    
         
            +
                    type
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
                end
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                if IS_RAILS_5
         
     | 
| 
      
 20 
     | 
    
         
            +
                  # Rails 5
         
     | 
| 
      
 21 
     | 
    
         
            +
                  require 'active_model/type'
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  def self.cast_value(cast_type, value)
         
     | 
| 
      
 24 
     | 
    
         
            +
                    ActiveModel::Type.lookup(flexible_type_name(cast_type)).cast(value)
         
     | 
| 
      
 25 
     | 
    
         
            +
                  end
         
     | 
| 
      
 26 
     | 
    
         
            +
                else
         
     | 
| 
      
 27 
     | 
    
         
            +
                  # Rails 4.2
         
     | 
| 
      
 28 
     | 
    
         
            +
                  require 'active_record'
         
     | 
| 
      
 29 
     | 
    
         
            +
                  require 'active_support/core_ext/hash/keys'
         
     | 
| 
      
 30 
     | 
    
         
            +
                  require 'active_record/type'
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
                  def self.cast_value(cast_type, value)
         
     | 
| 
      
 33 
     | 
    
         
            +
                    classified = ActiveSupport::Inflector.classify(flexible_type_name(cast_type).to_s)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    type = ActiveRecord::Type.const_get(classified)
         
     | 
| 
      
 35 
     | 
    
         
            +
                    type.new.type_cast_from_user(value)
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                end
         
     | 
| 
      
 38 
     | 
    
         
            +
              end
         
     | 
| 
      
 39 
     | 
    
         
            +
            end
         
     | 
    
        data/lib/type_attributes.rb
    CHANGED
    
    | 
         @@ -1,58 +1,33 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            require 'type_attributes/version'
         
     | 
| 
       2 
1 
     | 
    
         
             
            require 'active_support/concern'
         
     | 
| 
       3 
     | 
    
         
            -
             
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require 'type_attributes/version'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'type_attributes/type'
         
     | 
| 
       4 
5 
     | 
    
         | 
| 
       5 
6 
     | 
    
         
             
            module TypeAttributes
         
     | 
| 
       6 
7 
     | 
    
         
             
              extend ActiveSupport::Concern
         
     | 
| 
       7 
8 
     | 
    
         | 
| 
       8 
     | 
    
         
            -
               
     | 
| 
      
 9 
     | 
    
         
            +
              included do
         
     | 
| 
      
 10 
     | 
    
         
            +
                @_generated_type_attribute_methods ||= begin
         
     | 
| 
      
 11 
     | 
    
         
            +
                  mod = const_set(:GeneratedTypeAttributeMethods, Module.new)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  include mod
         
     | 
| 
      
 13 
     | 
    
         
            +
                  mod
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
       9 
16 
     | 
    
         | 
| 
       10 
17 
     | 
    
         
             
              module ClassMethods
         
     | 
| 
       11 
18 
     | 
    
         
             
                def type_attribute(name, cast_type)
         
     | 
| 
       12 
19 
     | 
    
         
             
                  name = name.to_sym
         
     | 
| 
       13 
20 
     | 
    
         
             
                  cast_type = cast_type.to_sym
         
     | 
| 
       14 
21 
     | 
    
         | 
| 
       15 
     | 
    
         
            -
                   
     | 
| 
       16 
     | 
    
         
            -
                     
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
       18 
     | 
    
         
            -
             
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                  define_method "#{name}=" do |decibels|
         
     | 
| 
       21 
     | 
    
         
            -
                    value = TypeAttributes.cast_value(cast_type, decibels)
         
     | 
| 
       22 
     | 
    
         
            -
                    instance_variable_set(:"@#{name}", value)
         
     | 
| 
       23 
     | 
    
         
            -
                  end
         
     | 
| 
       24 
     | 
    
         
            -
                end
         
     | 
| 
       25 
     | 
    
         
            -
              end
         
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
              def self.flexible_type_name(type)
         
     | 
| 
       28 
     | 
    
         
            -
                return type unless %i(date_time datetime).include?(type)
         
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
       30 
     | 
    
         
            -
                if IS_RAILS_5
         
     | 
| 
       31 
     | 
    
         
            -
                  # Change type name for DateTime in Rails5
         
     | 
| 
       32 
     | 
    
         
            -
                  # related: https://github.com/rails/rails/pull/24079
         
     | 
| 
       33 
     | 
    
         
            -
                  :datetime
         
     | 
| 
       34 
     | 
    
         
            -
                else
         
     | 
| 
       35 
     | 
    
         
            -
                  :date_time
         
     | 
| 
       36 
     | 
    
         
            -
                end
         
     | 
| 
       37 
     | 
    
         
            -
              end
         
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
              if IS_RAILS_5
         
     | 
| 
       40 
     | 
    
         
            -
                # Rails 5
         
     | 
| 
       41 
     | 
    
         
            -
                require 'active_model/type'
         
     | 
| 
       42 
     | 
    
         
            -
             
     | 
| 
       43 
     | 
    
         
            -
                def self.cast_value(cast_type, value)
         
     | 
| 
       44 
     | 
    
         
            -
                  ActiveModel::Type.lookup(flexible_type_name(cast_type)).cast(value)
         
     | 
| 
       45 
     | 
    
         
            -
                end
         
     | 
| 
       46 
     | 
    
         
            -
              else
         
     | 
| 
       47 
     | 
    
         
            -
                # Rails 4.2
         
     | 
| 
       48 
     | 
    
         
            -
                require 'active_record'
         
     | 
| 
       49 
     | 
    
         
            -
                require 'active_support/core_ext/hash/keys'
         
     | 
| 
       50 
     | 
    
         
            -
                require 'active_record/type'
         
     | 
| 
      
 22 
     | 
    
         
            +
                  @_generated_type_attribute_methods.class_eval <<-METHOD, __FILE__, __LINE__ + 1
         
     | 
| 
      
 23 
     | 
    
         
            +
                    def #{name}
         
     | 
| 
      
 24 
     | 
    
         
            +
                      TypeAttributes::Type.cast_value(:#{cast_type}, @#{name})
         
     | 
| 
      
 25 
     | 
    
         
            +
                    end
         
     | 
| 
       51 
26 
     | 
    
         | 
| 
       52 
     | 
    
         
            -
             
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
                   
     | 
| 
      
 27 
     | 
    
         
            +
                    def #{name}=(decibels)
         
     | 
| 
      
 28 
     | 
    
         
            +
                      @#{name} = TypeAttributes::Type.cast_value(:#{cast_type}, decibels)
         
     | 
| 
      
 29 
     | 
    
         
            +
                    end
         
     | 
| 
      
 30 
     | 
    
         
            +
                  METHOD
         
     | 
| 
       56 
31 
     | 
    
         
             
                end
         
     | 
| 
       57 
32 
     | 
    
         
             
              end
         
     | 
| 
       58 
33 
     | 
    
         
             
            end
         
     | 
    
        data/type_attributes.gemspec
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: type_attributes
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.1.2
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - alpaca-tc
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2017- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2017-10-03 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activemodel
         
     | 
| 
         @@ -66,6 +66,20 @@ dependencies: 
     | 
|
| 
       66 
66 
     | 
    
         
             
                - - "~>"
         
     | 
| 
       67 
67 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       68 
68 
     | 
    
         
             
                    version: '3.0'
         
     | 
| 
      
 69 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 70 
     | 
    
         
            +
              name: appraisal
         
     | 
| 
      
 71 
     | 
    
         
            +
              requirement: !ruby/object:Gem::Requirement
         
     | 
| 
      
 72 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 73 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 74 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 75 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 76 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 77 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 78 
     | 
    
         
            +
              version_requirements: !ruby/object:Gem::Requirement
         
     | 
| 
      
 79 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 80 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 81 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 82 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
       69 
83 
     | 
    
         
             
            description: 
         
     | 
| 
       70 
84 
     | 
    
         
             
            email:
         
     | 
| 
       71 
85 
     | 
    
         
             
            - alpaca-tc@alpaca.tc
         
     | 
| 
         @@ -76,10 +90,15 @@ files: 
     | 
|
| 
       76 
90 
     | 
    
         
             
            - ".gitignore"
         
     | 
| 
       77 
91 
     | 
    
         
             
            - ".rspec"
         
     | 
| 
       78 
92 
     | 
    
         
             
            - ".travis.yml"
         
     | 
| 
      
 93 
     | 
    
         
            +
            - Appraisals
         
     | 
| 
       79 
94 
     | 
    
         
             
            - Gemfile
         
     | 
| 
       80 
95 
     | 
    
         
             
            - README.md
         
     | 
| 
       81 
96 
     | 
    
         
             
            - Rakefile
         
     | 
| 
      
 97 
     | 
    
         
            +
            - gemfiles/4.2_stable.gemfile
         
     | 
| 
      
 98 
     | 
    
         
            +
            - gemfiles/5.0_stable.gemfile
         
     | 
| 
      
 99 
     | 
    
         
            +
            - gemfiles/5.1_stable.gemfile
         
     | 
| 
       82 
100 
     | 
    
         
             
            - lib/type_attributes.rb
         
     | 
| 
      
 101 
     | 
    
         
            +
            - lib/type_attributes/type.rb
         
     | 
| 
       83 
102 
     | 
    
         
             
            - lib/type_attributes/version.rb
         
     | 
| 
       84 
103 
     | 
    
         
             
            - type_attributes.gemspec
         
     | 
| 
       85 
104 
     | 
    
         
             
            homepage: https://github.com/alpaca-tc/type_attributes
         
     | 
| 
         @@ -101,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       101 
120 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       102 
121 
     | 
    
         
             
            requirements: []
         
     | 
| 
       103 
122 
     | 
    
         
             
            rubyforge_project: 
         
     | 
| 
       104 
     | 
    
         
            -
            rubygems_version: 2.6. 
     | 
| 
      
 123 
     | 
    
         
            +
            rubygems_version: 2.6.13
         
     | 
| 
       105 
124 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       106 
125 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       107 
126 
     | 
    
         
             
            summary: A type cast for ActiveModel
         
     |