looksy 0.0.2 → 0.0.3
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.
- data/lib/looksy/cacheable.rb +31 -32
- data/lib/looksy/version.rb +1 -1
- data/spec/support/record.rb +25 -9
- metadata +3 -3
    
        data/lib/looksy/cacheable.rb
    CHANGED
    
    | @@ -1,49 +1,48 @@ | |
| 1 1 | 
             
            module Looksy
         | 
| 2 2 | 
             
              module Cacheable
         | 
| 3 | 
            -
                def  | 
| 4 | 
            -
                   | 
| 5 | 
            -
             | 
| 3 | 
            +
                def self.included(base)
         | 
| 4 | 
            +
                  base.extend(ClassMethods)
         | 
| 5 | 
            +
                  base.class_eval do
         | 
| 6 | 
            +
                    def self.cache_key
         | 
| 7 | 
            +
                      @cache_key ||= [self.name.downcase, 'all'].join('/')
         | 
| 8 | 
            +
                    end
         | 
| 6 9 |  | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            +
                    def self.cache_key=(key)
         | 
| 11 | 
            +
                      @cache_key = key
         | 
| 12 | 
            +
                    end
         | 
| 10 13 |  | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            +
                    def self.cache_options
         | 
| 15 | 
            +
                      @cache_options ||= {}
         | 
| 16 | 
            +
                    end
         | 
| 14 17 |  | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            +
                    def self.cache_options=(options = {})
         | 
| 19 | 
            +
                      @cache_options = options
         | 
| 20 | 
            +
                    end
         | 
| 18 21 |  | 
| 19 | 
            -
             | 
| 20 | 
            -
                  lookup.all
         | 
| 21 | 
            -
                end
         | 
| 22 | 
            +
                    private
         | 
| 22 23 |  | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 24 | 
            +
                    def self.lookup
         | 
| 25 | 
            +
                      @lookup ||= Looksy::Lookup.new(self, defined?(Rails) ? Rails.cache : Looksy::NullCache.new)
         | 
| 26 | 
            +
                    end
         | 
| 27 | 
            +
                  end
         | 
| 25 28 | 
             
                end
         | 
| 26 29 |  | 
| 27 | 
            -
                 | 
| 28 | 
            -
                   | 
| 29 | 
            -
                     | 
| 30 | 
            -
                    lookup.send(method, *args, &block)
         | 
| 31 | 
            -
                  else
         | 
| 32 | 
            -
                    super
         | 
| 30 | 
            +
                module ClassMethods
         | 
| 31 | 
            +
                  def fetch_all
         | 
| 32 | 
            +
                    lookup.all
         | 
| 33 33 | 
             
                  end
         | 
| 34 | 
            -
                end
         | 
| 35 34 |  | 
| 36 | 
            -
             | 
| 35 | 
            +
                  def fetch_by_id(id)
         | 
| 36 | 
            +
                    lookup.find_by_id(id)
         | 
| 37 | 
            +
                  end
         | 
| 37 38 |  | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 40 | 
            -
             | 
| 41 | 
            -
                       | 
| 39 | 
            +
                  def method_missing(method, *args, &block)
         | 
| 40 | 
            +
                    if method.match(/^fetch/)
         | 
| 41 | 
            +
                      method = method.to_s.gsub(/^fetch/, 'find')
         | 
| 42 | 
            +
                      lookup.send(method, *args, &block)
         | 
| 42 43 | 
             
                    else
         | 
| 43 | 
            -
                       | 
| 44 | 
            +
                      super
         | 
| 44 45 | 
             
                    end
         | 
| 45 | 
            -
             | 
| 46 | 
            -
                    Looksy::Lookup.new(self, cache)
         | 
| 47 46 | 
             
                  end
         | 
| 48 47 | 
             
                end
         | 
| 49 48 | 
             
              end
         | 
    
        data/lib/looksy/version.rb
    CHANGED
    
    
    
        data/spec/support/record.rb
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            class Record < Struct.new(:id, :name, :type)
         | 
| 2 | 
            -
               | 
| 2 | 
            +
              include Looksy::Cacheable
         | 
| 3 3 |  | 
| 4 4 | 
             
              def self.all
         | 
| 5 5 | 
             
                @all ||= [
         | 
| @@ -14,12 +14,29 @@ class Record < Struct.new(:id, :name, :type) | |
| 14 14 | 
             
                all.first
         | 
| 15 15 | 
             
              end
         | 
| 16 16 |  | 
| 17 | 
            -
              def self. | 
| 18 | 
            -
                all | 
| 17 | 
            +
              def self.last
         | 
| 18 | 
            +
                all.last
         | 
| 19 19 | 
             
              end
         | 
| 20 20 |  | 
| 21 | 
            -
              def  | 
| 22 | 
            -
                 | 
| 21 | 
            +
              def attributes
         | 
| 22 | 
            +
                {
         | 
| 23 | 
            +
                  "id" => id,
         | 
| 24 | 
            +
                  "name" => name,
         | 
| 25 | 
            +
                  "type" => type
         | 
| 26 | 
            +
                }
         | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
            end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            =begin
         | 
| 31 | 
            +
            class OtherRecord < Struct.new(:id)
         | 
| 32 | 
            +
              include Looksy::Cacheable
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def self.all
         | 
| 35 | 
            +
                @all ||= [new(1), new(2), new(3)]
         | 
| 36 | 
            +
              end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
              def self.first
         | 
| 39 | 
            +
                all.first
         | 
| 23 40 | 
             
              end
         | 
| 24 41 |  | 
| 25 42 | 
             
              def self.last
         | 
| @@ -28,9 +45,8 @@ class Record < Struct.new(:id, :name, :type) | |
| 28 45 |  | 
| 29 46 | 
             
              def attributes
         | 
| 30 47 | 
             
                {
         | 
| 31 | 
            -
                  "id" => id | 
| 32 | 
            -
                  "name" => name,
         | 
| 33 | 
            -
                  "type" => type
         | 
| 48 | 
            +
                  "id" => id
         | 
| 34 49 | 
             
                }
         | 
| 35 50 | 
             
              end
         | 
| 36 | 
            -
            end
         | 
| 51 | 
            +
            end
         | 
| 52 | 
            +
            =end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: looksy
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.3
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -14,7 +14,7 @@ default_executable: | |
| 14 14 | 
             
            dependencies:
         | 
| 15 15 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 16 16 | 
             
              name: rspec
         | 
| 17 | 
            -
              requirement: & | 
| 17 | 
            +
              requirement: &17430820 !ruby/object:Gem::Requirement
         | 
| 18 18 | 
             
                none: false
         | 
| 19 19 | 
             
                requirements:
         | 
| 20 20 | 
             
                - - ! '>='
         | 
| @@ -22,7 +22,7 @@ dependencies: | |
| 22 22 | 
             
                    version: '0'
         | 
| 23 23 | 
             
              type: :development
         | 
| 24 24 | 
             
              prerelease: false
         | 
| 25 | 
            -
              version_requirements: * | 
| 25 | 
            +
              version_requirements: *17430820
         | 
| 26 26 | 
             
            description: Add a caching layer to your ActiveRecord models that represent look up
         | 
| 27 27 | 
             
              tables
         | 
| 28 28 | 
             
            email:
         |