tag_options 1.3.0 → 1.4.0
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/CHANGELOG.md +11 -1
- data/MIT-LICENSE +1 -1
- data/README.md +19 -1
- data/lib/tag_options/hash.rb +36 -5
- data/lib/tag_options/hash_at.rb +9 -5
- data/lib/tag_options/version.rb +1 -1
- metadata +3 -17
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: '0819a2033aab945266136d790144f8163007dd481fc85bd29176b5d2efa34101'
         | 
| 4 | 
            +
              data.tar.gz: 3acd88c4e4b52a851e3e36802bf6133d34261cf4412794595be6b85c98aac2ba
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 5e9b1a016760f0c0027f67bc6193f07b5817207b3a1ec78482e74fad59e962203ff8706bbd3d42f532a787d36a5bbd529cb7d40c6babe17aefb4f4868380e708
         | 
| 7 | 
            +
              data.tar.gz: bb99a982289d72a5ed72902cb97b8926c964ca7ea48c275d70d63dbb1e6ac4344e01bdb01f6627454f09d238a6c52652182f4a6eddad44ad68d3d6342cd487e2
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -2,6 +2,16 @@ | |
| 2 2 |  | 
| 3 3 | 
             
            ## [Unreleased]
         | 
| 4 4 |  | 
| 5 | 
            +
            ## [1.4.0] - 2023-04-15
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            - Removed support for Ruby 2.7.
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ## [1.3.1] - 2023-03-06
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            - Fixed a bug where keys with empty values were being populated when using
         | 
| 12 | 
            +
              `at()` against a non-existant key and the values passed to `combine!`, `set!`,
         | 
| 13 | 
            +
              or `default` resolved to no values or where `remove!` removed all values.
         | 
| 14 | 
            +
             | 
| 5 15 | 
             
            ## [1.3.0] - 2023-03-03
         | 
| 6 16 |  | 
| 7 17 | 
             
            - Added `at().remove!` option for removing values.
         | 
| @@ -16,7 +26,7 @@ for more information. | |
| 16 26 | 
             
            - Fixed bug introduced when switching to
         | 
| 17 27 | 
             
              `ActiveSupport::HashWithIndifferentAccess` that prevented a `TagOptions::Hash`
         | 
| 18 28 | 
             
              from being passed to a method using double splat, e.g. `some_method
         | 
| 19 | 
            -
             | 
| 29 | 
            +
            **options`.
         | 
| 20 30 |  | 
| 21 31 | 
             
            ## [1.2.0] - 2023-03-02
         | 
| 22 32 |  | 
    
        data/MIT-LICENSE
    CHANGED
    
    
    
        data/README.md
    CHANGED
    
    | @@ -65,7 +65,7 @@ bundle install | |
| 65 65 |  | 
| 66 66 | 
             
            ## General Usage
         | 
| 67 67 |  | 
| 68 | 
            -
             | 
| 68 | 
            +
            Instantiate a `TagOptions::Hash` directly or by passing an existing `Hash`.
         | 
| 69 69 |  | 
| 70 70 | 
             
            ```ruby
         | 
| 71 71 | 
             
            TagOptions::Hash.new
         | 
| @@ -76,6 +76,24 @@ TagOptions::Hash.new(hash) | |
| 76 76 | 
             
            => {:class=>"flex"}
         | 
| 77 77 | 
             
            ```
         | 
| 78 78 |  | 
| 79 | 
            +
            Similar to `Array()`, you can also instantiate a new `TagOptions::Hash` by
         | 
| 80 | 
            +
            passing a has to `TagOptions::Hash()`.
         | 
| 81 | 
            +
             | 
| 82 | 
            +
            ```ruby
         | 
| 83 | 
            +
            hash = {class: "flex"}
         | 
| 84 | 
            +
            TagOptions::Hash(hash)
         | 
| 85 | 
            +
            => {:class=>"flex"}
         | 
| 86 | 
            +
            ```
         | 
| 87 | 
            +
             | 
| 88 | 
            +
            The values of the hash passed to `TagOptions::Hash.new` or `TagOptions::Hash()`
         | 
| 89 | 
            +
            are automatically converted to strings.
         | 
| 90 | 
            +
             | 
| 91 | 
            +
            ```ruby
         | 
| 92 | 
            +
            hash = {disabled: true}
         | 
| 93 | 
            +
            TagOptions::Hash.new(hash)
         | 
| 94 | 
            +
            => {:disabled=>"true"}
         | 
| 95 | 
            +
            ```
         | 
| 96 | 
            +
             | 
| 79 97 | 
             
            `TagOptions::Hash` inherits from `ActiveSupport::HashWithIndifferentAccess`,
         | 
| 80 98 | 
             
            implementing a hash where keys `:foo` and `"foo"` are considered to be the same.
         | 
| 81 99 | 
             
            It differs from `ActiveSupport::HashWithIndifferentAccess`, however, by storing
         | 
    
        data/lib/tag_options/hash.rb
    CHANGED
    
    | @@ -1,5 +1,6 @@ | |
| 1 1 | 
             
            require "active_support/callbacks"
         | 
| 2 2 | 
             
            require "active_support/core_ext/hash/indifferent_access"
         | 
| 3 | 
            +
            require "active_support/core_ext/object/blank"
         | 
| 3 4 | 
             
            require "tag_options/convert_key"
         | 
| 4 5 | 
             
            require "tag_options/hash_at"
         | 
| 5 6 | 
             
            require "tag_options/errors/not_hash_error"
         | 
| @@ -14,8 +15,9 @@ module TagOptions | |
| 14 15 | 
             
                def initialize(hash = {})
         | 
| 15 16 | 
             
                  run_callbacks :initialize do
         | 
| 16 17 | 
             
                    hash.each do |key, value|
         | 
| 17 | 
            -
                      self[convert_key(key)] = value.is_a?(::Hash) ? self.class.new(value) : value
         | 
| 18 | 
            +
                      self[convert_key(key)] = value.is_a?(::Hash) ? self.class.new(value) : value.to_s
         | 
| 18 19 | 
             
                    end
         | 
| 20 | 
            +
                    remove_blank!
         | 
| 19 21 | 
             
                  end
         | 
| 20 22 | 
             
                end
         | 
| 21 23 |  | 
| @@ -24,21 +26,50 @@ module TagOptions | |
| 24 26 | 
             
                end
         | 
| 25 27 |  | 
| 26 28 | 
             
                def dig(*keys)
         | 
| 27 | 
            -
                   | 
| 28 | 
            -
             | 
| 29 | 
            +
                  return self if keys.size.zero?
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  dug_keys = []
         | 
| 32 | 
            +
                  data = self
         | 
| 33 | 
            +
                  keys.each_with_index do |key, index|
         | 
| 34 | 
            +
                    key = convert_key(key)
         | 
| 35 | 
            +
                    return nil unless data.key?(key)
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    data = data[key]
         | 
| 38 | 
            +
                    dug_keys << key
         | 
| 39 | 
            +
                    last_key = index == keys.size - 1
         | 
| 40 | 
            +
                    unless last_key || data.is_a?(self.class)
         | 
| 41 | 
            +
                      raise TagOptions::Errors::NotHashError.new(dug_keys, type: data.class)
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                  data
         | 
| 29 45 | 
             
                end
         | 
| 30 46 |  | 
| 31 47 | 
             
                def populate!(*keys)
         | 
| 32 48 | 
             
                  populated_keys = []
         | 
| 33 49 | 
             
                  data = self
         | 
| 34 50 | 
             
                  keys.each do |key|
         | 
| 35 | 
            -
                     | 
| 36 | 
            -
                    data | 
| 51 | 
            +
                    key = convert_key(key)
         | 
| 52 | 
            +
                    data[key] ||= self.class.new
         | 
| 53 | 
            +
                    data = data[key]
         | 
| 54 | 
            +
                    populated_keys << key
         | 
| 37 55 | 
             
                    unless data.is_a?(self.class)
         | 
| 38 56 | 
             
                      raise TagOptions::Errors::NotHashError.new(populated_keys, type: data.class)
         | 
| 39 57 | 
             
                    end
         | 
| 40 58 | 
             
                  end
         | 
| 41 59 | 
             
                  self
         | 
| 42 60 | 
             
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                def remove_blank!(hash = self, parent_hash: nil)
         | 
| 63 | 
            +
                  hash.each do |key, value|
         | 
| 64 | 
            +
                    if value.blank?
         | 
| 65 | 
            +
                      hash.delete(key)
         | 
| 66 | 
            +
                      remove_blank!(parent_hash, parent_hash: nil) if parent_hash
         | 
| 67 | 
            +
                    elsif value.is_a?(Hash)
         | 
| 68 | 
            +
                      remove_blank!(value, parent_hash: hash)
         | 
| 69 | 
            +
                      remove_blank!(parent_hash, parent_hash: nil) if parent_hash
         | 
| 70 | 
            +
                    end
         | 
| 71 | 
            +
                  end
         | 
| 72 | 
            +
                  hash
         | 
| 73 | 
            +
                end
         | 
| 43 74 | 
             
              end
         | 
| 44 75 | 
             
            end
         | 
    
        data/lib/tag_options/hash_at.rb
    CHANGED
    
    | @@ -13,23 +13,19 @@ module TagOptions | |
| 13 13 | 
             
                end
         | 
| 14 14 |  | 
| 15 15 | 
             
                def combine!(*values, **conditions)
         | 
| 16 | 
            -
                  @opt_hash.populate!(*@keys)
         | 
| 17 16 | 
             
                  current_value = @opt_hash.dig(*@keys, @value_key)
         | 
| 18 17 | 
             
                  set_value! @resolver.call(current_value, *values, **conditions)
         | 
| 19 18 | 
             
                end
         | 
| 20 19 |  | 
| 21 20 | 
             
                def default!(*values, **conditions)
         | 
| 22 | 
            -
                  @opt_hash.populate!(*@keys)
         | 
| 23 21 | 
             
                  set_default! @resolver.call(*values, **conditions)
         | 
| 24 22 | 
             
                end
         | 
| 25 23 |  | 
| 26 24 | 
             
                def set!(*values, **conditions)
         | 
| 27 | 
            -
                  @opt_hash.populate!(*@keys)
         | 
| 28 25 | 
             
                  set_value! @resolver.call(*values, **conditions)
         | 
| 29 26 | 
             
                end
         | 
| 30 27 |  | 
| 31 28 | 
             
                def remove!(*values, **conditions)
         | 
| 32 | 
            -
                  @opt_hash.populate!(*@keys)
         | 
| 33 29 | 
             
                  regex_values, values = values.flatten.partition { |v| v.is_a?(Regexp) }
         | 
| 34 30 | 
             
                  remove_values!(*regex_values, *@resolver.values(*values, **conditions))
         | 
| 35 31 | 
             
                end
         | 
| @@ -37,7 +33,7 @@ module TagOptions | |
| 37 33 | 
             
                private
         | 
| 38 34 |  | 
| 39 35 | 
             
                def remove_values!(*values_to_remove)
         | 
| 40 | 
            -
                  values = @resolver.values(@opt_hash.dig(*@keys)[@value_key | 
| 36 | 
            +
                  values = @resolver.values(@opt_hash.dig(*@keys)&.[](@value_key))
         | 
| 41 37 | 
             
                  values_to_remove.each do |value|
         | 
| 42 38 | 
             
                    if value.is_a?(Regexp)
         | 
| 43 39 | 
             
                      values.reject! { |current_value| value.match?(current_value) }
         | 
| @@ -45,17 +41,25 @@ module TagOptions | |
| 45 41 | 
             
                      values.reject! { |current_value| value == current_value }
         | 
| 46 42 | 
             
                    end
         | 
| 47 43 | 
             
                  end
         | 
| 44 | 
            +
                  @opt_hash.populate!(*@keys)
         | 
| 48 45 | 
             
                  @opt_hash.dig(*@keys)[@value_key] = @resolver.call(*values)
         | 
| 46 | 
            +
                  @opt_hash.remove_blank!
         | 
| 49 47 | 
             
                  @opt_hash
         | 
| 50 48 | 
             
                end
         | 
| 51 49 |  | 
| 52 50 | 
             
                def set_default!(value)
         | 
| 51 | 
            +
                  return @opt_hash if value.blank?
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  @opt_hash.populate!(*@keys)
         | 
| 53 54 | 
             
                  root = @opt_hash.dig(*@keys)
         | 
| 54 55 | 
             
                  root[@value_key] = value unless root.key?(@value_key)
         | 
| 55 56 | 
             
                  @opt_hash
         | 
| 56 57 | 
             
                end
         | 
| 57 58 |  | 
| 58 59 | 
             
                def set_value!(value)
         | 
| 60 | 
            +
                  return @opt_hash if value.blank?
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                  @opt_hash.populate!(*@keys)
         | 
| 59 63 | 
             
                  @opt_hash.dig(*@keys)[@value_key] = value
         | 
| 60 64 | 
             
                  @opt_hash
         | 
| 61 65 | 
             
                end
         | 
    
        data/lib/tag_options/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: tag_options
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 1. | 
| 4 | 
            +
              version: 1.4.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Alex Monroe
         | 
| 8 8 | 
             
            autorequire:
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2023- | 
| 11 | 
            +
            date: 2023-04-16 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activesupport
         | 
| @@ -24,20 +24,6 @@ dependencies: | |
| 24 24 | 
             
                - - ">="
         | 
| 25 25 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 26 | 
             
                    version: '0'
         | 
| 27 | 
            -
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            -
              name: rspec
         | 
| 29 | 
            -
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            -
                requirements:
         | 
| 31 | 
            -
                - - ">="
         | 
| 32 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            -
                    version: '0'
         | 
| 34 | 
            -
              type: :development
         | 
| 35 | 
            -
              prerelease: false
         | 
| 36 | 
            -
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            -
                requirements:
         | 
| 38 | 
            -
                - - ">="
         | 
| 39 | 
            -
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            -
                    version: '0'
         | 
| 41 27 | 
             
            description: Simple library for manipulating options passed to various Rails tag helpers.
         | 
| 42 28 | 
             
            email:
         | 
| 43 29 | 
             
            - alex@monroepost.com
         | 
| @@ -76,7 +62,7 @@ required_ruby_version: !ruby/object:Gem::Requirement | |
| 76 62 | 
             
              requirements:
         | 
| 77 63 | 
             
              - - ">="
         | 
| 78 64 | 
             
                - !ruby/object:Gem::Version
         | 
| 79 | 
            -
                  version:  | 
| 65 | 
            +
                  version: 3.0.0
         | 
| 80 66 | 
             
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 81 67 | 
             
              requirements:
         | 
| 82 68 | 
             
              - - ">="
         |