shamu 0.0.20 → 0.0.21
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/Gemfile.lock +2 -2
- data/lib/shamu/entities.rb +1 -0
- data/lib/shamu/entities/static_repository.rb +83 -0
- data/lib/shamu/services/service.rb +0 -8
- data/lib/shamu/to_model_id_extension.rb +9 -0
- data/lib/shamu/version.rb +1 -1
- data/spec/lib/shamu/entities/static_repository_spec.rb +129 -0
- metadata +5 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 6cae4307a05211be596027da64335d8e7ea8a873
         | 
| 4 | 
            +
              data.tar.gz: 11c2070a6bab4b24e9770dbb6c92bd00f39b10ca
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 626bd61e2a680af882d36bea800ce9229213e92dd0b1c474dd81aba7834768ea5659b87ec3294cdd59b077885a24a7869fa848b77eade04fa0971394f56a76fe
         | 
| 7 | 
            +
              data.tar.gz: 9b3bab05830afd1b09bb9662348a6e404d7b03e2e817407afb5d0b70e035a20a3f3e1358bbab4bdbadbe1e072c755382b933bcd2122a6a4ff296e85964501ace
         | 
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                shamu (0.0. | 
| 4 | 
            +
                shamu (0.0.21)
         | 
| 5 5 | 
             
                  activemodel (>= 5.0)
         | 
| 6 6 | 
             
                  activesupport (>= 5.0)
         | 
| 7 7 | 
             
                  crc32 (~> 1)
         | 
| @@ -104,7 +104,7 @@ GEM | |
| 104 104 | 
             
                loofah (2.0.3)
         | 
| 105 105 | 
             
                  nokogiri (>= 1.5.9)
         | 
| 106 106 | 
             
                lumberjack (1.0.12)
         | 
| 107 | 
            -
                mail (2.6. | 
| 107 | 
            +
                mail (2.6.6)
         | 
| 108 108 | 
             
                  mime-types (>= 1.16, < 4)
         | 
| 109 109 | 
             
                method_source (0.8.2)
         | 
| 110 110 | 
             
                mime-types (3.1)
         | 
    
        data/lib/shamu/entities.rb
    CHANGED
    
    | @@ -9,6 +9,7 @@ module Shamu | |
| 9 9 | 
             
                require "shamu/entities/identity_cache"
         | 
| 10 10 | 
             
                require "shamu/entities/entity_path"
         | 
| 11 11 | 
             
                require "shamu/entities/html_sanitation"
         | 
| 12 | 
            +
                require "shamu/entities/static_repository"
         | 
| 12 13 | 
             
                require "shamu/entities/entity_lookup_service"
         | 
| 13 14 | 
             
                require "shamu/entities/opaque_id"
         | 
| 14 15 | 
             
                require "shamu/entities/opaque_entity_lookup_service"
         | 
| @@ -0,0 +1,83 @@ | |
| 1 | 
            +
            module Shamu
         | 
| 2 | 
            +
              module Entities
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                # Implements an in-memory store of entities for static entities (rich enum
         | 
| 5 | 
            +
                # types) offering standard read methods #find, #lookup and #list.
         | 
| 6 | 
            +
                class StaticRepository
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def initialize( entities, missing_entity_class: nil )
         | 
| 9 | 
            +
                    raise ArgumentError, :entities if entities.map( &:id ).count != entities.map( &:id ).uniq.count
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    entities = entities.dup.freeze unless entities.frozen?
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    @entities             = entities
         | 
| 14 | 
            +
                    @missing_entity_class = missing_entity_class || NullEntity.for( entities.first.class )
         | 
| 15 | 
            +
                    @lookup_cache         = {}
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  # Find an entity with the given value on the named attribute.
         | 
| 19 | 
            +
                  #
         | 
| 20 | 
            +
                  # @param [Object] value to look for.
         | 
| 21 | 
            +
                  # @param [Symbol] attribute to interrogate.
         | 
| 22 | 
            +
                  # @return [Entity] the entity if found.
         | 
| 23 | 
            +
                  # @raise [Shamu::NotFoundError] if the entity could not be found.
         | 
| 24 | 
            +
                  def find_by( attribute, value )
         | 
| 25 | 
            +
                    cache = attribute_cache( attribute )
         | 
| 26 | 
            +
                    cache.fetch value do
         | 
| 27 | 
            +
                      cache[ value ] = find_by_attribute( attribute, value )
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                  # Find an entity with the given id.
         | 
| 32 | 
            +
                  #
         | 
| 33 | 
            +
                  # @return [Entity] the entity if found.
         | 
| 34 | 
            +
                  # @raise [Shamu::NotFoundError] if the entity could not be found.
         | 
| 35 | 
            +
                  def find( id = :not_set, &block )
         | 
| 36 | 
            +
                    raise ArgumentError, :id if id == :not_set && !block_given?
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    value = block_given? ? yield : find_by( :id, id )
         | 
| 39 | 
            +
                    value || not_found!
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                  # Lookup all the entities in the repository with the given ids.
         | 
| 43 | 
            +
                  # @param [Array<Integer>] ids
         | 
| 44 | 
            +
                  # @return [List<Entity>] the matching entities.
         | 
| 45 | 
            +
                  def lookup( *ids )
         | 
| 46 | 
            +
                    cache = attribute_cache( :id )
         | 
| 47 | 
            +
                    matching = ids.map do |id|
         | 
| 48 | 
            +
                      entity = cache.fetch( id ) do
         | 
| 49 | 
            +
                        entities.find { |e| e.id == id }
         | 
| 50 | 
            +
                      end
         | 
| 51 | 
            +
             | 
| 52 | 
            +
                      entity || missing_entity_class.new
         | 
| 53 | 
            +
                    end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    List.new matching
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                  # @return [List<Entity>] all the entities in the repository.
         | 
| 59 | 
            +
                  def list
         | 
| 60 | 
            +
                    List.new entities
         | 
| 61 | 
            +
                  end
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                  private
         | 
| 64 | 
            +
             | 
| 65 | 
            +
                    attr_reader :entities, :missing_entity_class
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                    def not_found!
         | 
| 68 | 
            +
                      raise Shamu::NotFoundError
         | 
| 69 | 
            +
                    end
         | 
| 70 | 
            +
             | 
| 71 | 
            +
                    def attribute_cache( attribute )
         | 
| 72 | 
            +
                      @lookup_cache.fetch( attribute ) do
         | 
| 73 | 
            +
                        @lookup_cache[ attribute ] = {}
         | 
| 74 | 
            +
                      end
         | 
| 75 | 
            +
                    end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                    def find_by_attribute( attribute, value )
         | 
| 78 | 
            +
                      entities.find { |e| e.send( attribute ) == value } || not_found!
         | 
| 79 | 
            +
                    end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
                end
         | 
| 82 | 
            +
              end
         | 
| 83 | 
            +
            end
         | 
| @@ -340,14 +340,6 @@ module Shamu | |
| 340 340 | 
             
                      end
         | 
| 341 341 | 
             
                    end
         | 
| 342 342 |  | 
| 343 | 
            -
                    # @param [String,Integer,#to_model_id] value
         | 
| 344 | 
            -
                    # @return [Boolean] true if the value looks like an ID.
         | 
| 345 | 
            -
                    def model_id?( value )
         | 
| 346 | 
            -
                      case Array( value ).first
         | 
| 347 | 
            -
                      when Integer then true
         | 
| 348 | 
            -
                      when String  then ToModelIdExtension::Strings::NUMERIC_PATTERN =~ value
         | 
| 349 | 
            -
                      end
         | 
| 350 | 
            -
                    end
         | 
| 351 343 | 
             
                end
         | 
| 352 344 | 
             
              end
         | 
| 353 345 | 
             
            end
         | 
| @@ -6,6 +6,15 @@ module Shamu | |
| 6 6 | 
             
              # models by id.
         | 
| 7 7 | 
             
              module ToModelIdExtension
         | 
| 8 8 |  | 
| 9 | 
            +
                # @param [String,Integer,#to_model_id] value
         | 
| 10 | 
            +
                # @return [Boolean] true if the value looks like an ID.
         | 
| 11 | 
            +
                def self.model_id?( value )
         | 
| 12 | 
            +
                  case Array( value ).first
         | 
| 13 | 
            +
                  when Integer then true
         | 
| 14 | 
            +
                  when String  then ToModelIdExtension::Strings::NUMERIC_PATTERN =~ value
         | 
| 15 | 
            +
                  end
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 9 18 | 
             
                # Extend common classes to add `to_model_id` method.
         | 
| 10 19 | 
             
                def self.extend!
         | 
| 11 20 | 
             
                  Integer.include Integers
         | 
    
        data/lib/shamu/version.rb
    CHANGED
    
    
| @@ -0,0 +1,129 @@ | |
| 1 | 
            +
            require "spec_helper"
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module StaticRepositorySpec
         | 
| 4 | 
            +
              class Entity < Shamu::Entities::Entity
         | 
| 5 | 
            +
                attribute :id
         | 
| 6 | 
            +
                attribute :name
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
            end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            describe Shamu::Entities::StaticRepository do
         | 
| 11 | 
            +
              let( :entities ) do
         | 
| 12 | 
            +
                [
         | 
| 13 | 
            +
                  scorpion.fetch( StaticRepositorySpec::Entity, { id: 10, name: "First" }, {} ),
         | 
| 14 | 
            +
                  scorpion.fetch( StaticRepositorySpec::Entity, { id: 20, name: "Last" }, {} ),
         | 
| 15 | 
            +
                ]
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
              let( :repository ) do
         | 
| 18 | 
            +
                Shamu::Entities::StaticRepository.new entities
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              it "does not allow duplicate keys" do
         | 
| 22 | 
            +
                expect do
         | 
| 23 | 
            +
                  Shamu::Entities::StaticRepository.new [
         | 
| 24 | 
            +
                    StaticRepositorySpec::Entity.new( id: 1 ),
         | 
| 25 | 
            +
                    StaticRepositorySpec::Entity.new( id: 1 ),
         | 
| 26 | 
            +
                  ]
         | 
| 27 | 
            +
                end.to raise_exception ArgumentError
         | 
| 28 | 
            +
              end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              it "requires at least one entity" do
         | 
| 31 | 
            +
                expect do
         | 
| 32 | 
            +
                  Shamu::Entities::StaticRepository.new
         | 
| 33 | 
            +
                end.to raise_exception ArgumentError
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              describe "#find" do
         | 
| 37 | 
            +
                it "finds by id" do
         | 
| 38 | 
            +
                  expect( repository.find( 10 ) ).to be entities.first
         | 
| 39 | 
            +
                end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                it "raises when not found" do
         | 
| 42 | 
            +
                  expect do
         | 
| 43 | 
            +
                    repository.find( 0 )
         | 
| 44 | 
            +
                  end.to raise_exception Shamu::NotFoundError
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                it "yields to block" do
         | 
| 48 | 
            +
                  expect do |b|
         | 
| 49 | 
            +
                    repository.find do
         | 
| 50 | 
            +
                      b.to_proc.call
         | 
| 51 | 
            +
                      true
         | 
| 52 | 
            +
                    end
         | 
| 53 | 
            +
                  end.to yield_control
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
             | 
| 56 | 
            +
                it "raises if no argument and no block" do
         | 
| 57 | 
            +
                  expect do
         | 
| 58 | 
            +
                    repository.find
         | 
| 59 | 
            +
                  end.to raise_exception ArgumentError
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                it "raises when not found by block" do
         | 
| 63 | 
            +
                  expect do
         | 
| 64 | 
            +
                    repository.find { false }
         | 
| 65 | 
            +
                  end.to raise_exception Shamu::NotFoundError
         | 
| 66 | 
            +
                end
         | 
| 67 | 
            +
              end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
              describe "#find_by" do
         | 
| 70 | 
            +
                it "finds by id" do
         | 
| 71 | 
            +
                  expect( repository.find_by( :id, 10 ) ).to be entities.first
         | 
| 72 | 
            +
                end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
                it "raises when not found" do
         | 
| 75 | 
            +
                  expect do
         | 
| 76 | 
            +
                    repository.find_by :id, 0
         | 
| 77 | 
            +
                  end.to raise_exception Shamu::NotFoundError
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                it "caches lookup" do
         | 
| 81 | 
            +
                  expect( repository ).to receive( :find_by_attribute ).and_call_original
         | 
| 82 | 
            +
                  repository.find_by :id, 10
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                  expect( repository ).not_to receive( :find_by_attribute )
         | 
| 85 | 
            +
                  repository.find_by :id, 10
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                it "finds by name" do
         | 
| 89 | 
            +
                  expect( repository.find_by( :name, "Last" ) ).to be entities.last
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                it "caches lookup by name" do
         | 
| 93 | 
            +
                  expect( repository ).to receive( :find_by_attribute ).and_call_original
         | 
| 94 | 
            +
                  repository.find_by( :name, "First" )
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                  expect( repository ).not_to receive( :find_by_attribute )
         | 
| 97 | 
            +
                  repository.find_by( :name, "First" )
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
              end
         | 
| 100 | 
            +
             | 
| 101 | 
            +
              describe "#lookup" do
         | 
| 102 | 
            +
                it "finds all ids" do
         | 
| 103 | 
            +
                  list = repository.lookup 10, 20
         | 
| 104 | 
            +
                  expect( list.to_a ).to eq entities
         | 
| 105 | 
            +
                end
         | 
| 106 | 
            +
             | 
| 107 | 
            +
                it "uses missing entity if not found" do
         | 
| 108 | 
            +
                  list = repository.lookup 20, 30
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                  expect( list.first ).to be entities.last
         | 
| 111 | 
            +
                  expect( list.last ).not_to be_present
         | 
| 112 | 
            +
                end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                it "returns an Entities::List" do
         | 
| 115 | 
            +
                  expect( repository.lookup ).to be_a Shamu::Entities::List
         | 
| 116 | 
            +
                end
         | 
| 117 | 
            +
              end
         | 
| 118 | 
            +
             | 
| 119 | 
            +
              describe "#list" do
         | 
| 120 | 
            +
                it "lists all records" do
         | 
| 121 | 
            +
                  expect( repository.list.to_a ).to eq entities
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                it "returns an Entities::List" do
         | 
| 125 | 
            +
                  expect( repository.list ).to be_a Shamu::Entities::List
         | 
| 126 | 
            +
                end
         | 
| 127 | 
            +
              end
         | 
| 128 | 
            +
             | 
| 129 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: shamu
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.21
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Paul Alexander
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2017-06- | 
| 11 | 
            +
            date: 2017-06-20 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activemodel
         | 
| @@ -265,6 +265,7 @@ files: | |
| 265 265 | 
             
            - lib/shamu/entities/opaque_entity_lookup_service.rb
         | 
| 266 266 | 
             
            - lib/shamu/entities/opaque_id.rb
         | 
| 267 267 | 
             
            - lib/shamu/entities/paged_list.rb
         | 
| 268 | 
            +
            - lib/shamu/entities/static_repository.rb
         | 
| 268 269 | 
             
            - lib/shamu/error.rb
         | 
| 269 270 | 
             
            - lib/shamu/events.rb
         | 
| 270 271 | 
             
            - lib/shamu/events/README.md
         | 
| @@ -404,6 +405,7 @@ files: | |
| 404 405 | 
             
            - spec/lib/shamu/entities/opaque_entity_lookup_service_spec.rb
         | 
| 405 406 | 
             
            - spec/lib/shamu/entities/opaque_id_spec.rb
         | 
| 406 407 | 
             
            - spec/lib/shamu/entities/paged_list_spec.rb
         | 
| 408 | 
            +
            - spec/lib/shamu/entities/static_repository_spec.rb
         | 
| 407 409 | 
             
            - spec/lib/shamu/events/active_record/migration_spec.rb
         | 
| 408 410 | 
             
            - spec/lib/shamu/events/active_record/service_spec.rb
         | 
| 409 411 | 
             
            - spec/lib/shamu/events/events_service_spec.rb
         | 
| @@ -526,6 +528,7 @@ test_files: | |
| 526 528 | 
             
            - spec/lib/shamu/entities/opaque_entity_lookup_service_spec.rb
         | 
| 527 529 | 
             
            - spec/lib/shamu/entities/opaque_id_spec.rb
         | 
| 528 530 | 
             
            - spec/lib/shamu/entities/paged_list_spec.rb
         | 
| 531 | 
            +
            - spec/lib/shamu/entities/static_repository_spec.rb
         | 
| 529 532 | 
             
            - spec/lib/shamu/events/active_record/migration_spec.rb
         | 
| 530 533 | 
             
            - spec/lib/shamu/events/active_record/service_spec.rb
         | 
| 531 534 | 
             
            - spec/lib/shamu/events/events_service_spec.rb
         |