nested 0.0.8 → 0.0.9
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 +1 -0
- data/Gemfile.lock +15 -1
- data/lib/nested.rb +29 -27
- data/nested.gemspec +2 -1
- data/test/nested_test.rb +3 -3
- metadata +15 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 27598f96705c4a9b647048ef032121d310fd3646
         | 
| 4 | 
            +
              data.tar.gz: cd15467a29499cb6bd1c648a00f0c6455c567372
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 30ec9775adca2477d587ddead84a29416d7c71f7782a1a5ae66953f4be1ebb58ecab7d5762f34c150b69c7cd598f0fa3257b099bcae821d17a623d79d2bcf46b
         | 
| 7 | 
            +
              data.tar.gz: e751dbad8babce5e2fc565293eb6b9351d36d9a7a9745b9c81a3e7a861f2b2c6c2935de72c05822263ea16df5eba85a25f85be643fdbe539f1fc470239715263
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,14 +1,26 @@ | |
| 1 1 | 
             
            GEM
         | 
| 2 2 | 
             
              remote: https://rubygems.org/
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                 | 
| 4 | 
            +
                activemodel (4.0.1)
         | 
| 5 | 
            +
                  activesupport (= 4.0.1)
         | 
| 6 | 
            +
                  builder (~> 3.1.0)
         | 
| 7 | 
            +
                activerecord (4.0.1)
         | 
| 8 | 
            +
                  activemodel (= 4.0.1)
         | 
| 9 | 
            +
                  activerecord-deprecated_finders (~> 1.0.2)
         | 
| 10 | 
            +
                  activesupport (= 4.0.1)
         | 
| 11 | 
            +
                  arel (~> 4.0.0)
         | 
| 12 | 
            +
                activerecord-deprecated_finders (1.0.3)
         | 
| 13 | 
            +
                activesupport (4.0.1)
         | 
| 5 14 | 
             
                  i18n (~> 0.6, >= 0.6.4)
         | 
| 6 15 | 
             
                  minitest (~> 4.2)
         | 
| 7 16 | 
             
                  multi_json (~> 1.3)
         | 
| 8 17 | 
             
                  thread_safe (~> 0.1)
         | 
| 9 18 | 
             
                  tzinfo (~> 0.3.37)
         | 
| 19 | 
            +
                arel (4.0.1)
         | 
| 10 20 | 
             
                atomic (1.1.14)
         | 
| 21 | 
            +
                builder (3.1.4)
         | 
| 11 22 | 
             
                i18n (0.6.5)
         | 
| 23 | 
            +
                json (1.8.1)
         | 
| 12 24 | 
             
                metaclass (0.0.1)
         | 
| 13 25 | 
             
                minitest (4.7.5)
         | 
| 14 26 | 
             
                mocha (0.14.0)
         | 
| @@ -22,5 +34,7 @@ PLATFORMS | |
| 22 34 | 
             
              ruby
         | 
| 23 35 |  | 
| 24 36 | 
             
            DEPENDENCIES
         | 
| 37 | 
            +
              activerecord
         | 
| 25 38 | 
             
              activesupport
         | 
| 39 | 
            +
              json
         | 
| 26 40 | 
             
              mocha
         | 
    
        data/lib/nested.rb
    CHANGED
    
    | @@ -15,18 +15,18 @@ module Nested | |
| 15 15 | 
             
              end
         | 
| 16 16 |  | 
| 17 17 | 
             
              class Resource
         | 
| 18 | 
            -
                FETCH = | 
| 19 | 
            -
                  raise "implement fetch for resource #{ | 
| 20 | 
            -
                  raise "implement fetch for singleton #{ | 
| 18 | 
            +
                FETCH = -> do
         | 
| 19 | 
            +
                  raise "implement fetch for resource #{@__resource.name}"  unless @__resource.parent
         | 
| 20 | 
            +
                  raise "implement fetch for singleton #{@__resource.name}" if @__resource.singleton?
         | 
| 21 21 |  | 
| 22 | 
            -
                  parent_resource =  | 
| 23 | 
            -
                  parent_obj =  | 
| 22 | 
            +
                  parent_resource = @__resource.parent
         | 
| 23 | 
            +
                  parent_obj = instance_variable_get("@#{parent_resource.instance_variable_name}")
         | 
| 24 24 |  | 
| 25 | 
            -
                  if  | 
| 26 | 
            -
                    scope = parent_obj.send( | 
| 27 | 
            -
                     | 
| 25 | 
            +
                  if @__resource.name
         | 
| 26 | 
            +
                    scope = parent_obj.send(@__resource.name.to_s.pluralize.to_sym)
         | 
| 27 | 
            +
                    @__resource.collection? ? scope : scope.where(id: params["#{@__resource.name.to_s.singularize}_id"]).first
         | 
| 28 28 | 
             
                  else
         | 
| 29 | 
            -
                    parent_obj.where(id:  | 
| 29 | 
            +
                    parent_obj.where(id: params["#{parent_resource.name.to_s.singularize}_id"]).first
         | 
| 30 30 | 
             
                  end
         | 
| 31 31 | 
             
                end
         | 
| 32 32 |  | 
| @@ -44,14 +44,14 @@ module Nested | |
| 44 44 | 
             
                  @resources = []
         | 
| 45 45 | 
             
                  @actions = []
         | 
| 46 46 |  | 
| 47 | 
            -
                  init &-> | 
| 48 | 
            -
                    fetched = FETCH | 
| 47 | 
            +
                  init &-> do
         | 
| 48 | 
            +
                    fetched = instance_exec(&FETCH)
         | 
| 49 49 |  | 
| 50 | 
            -
                    puts "set @#{ | 
| 51 | 
            -
                    self.instance_variable_set("@#{ | 
| 50 | 
            +
                    puts "set @#{@__resource.instance_variable_name} to #{fetched.inspect} for #{self}"
         | 
| 51 | 
            +
                    self.instance_variable_set("@#{@__resource.instance_variable_name}", fetched)
         | 
| 52 52 | 
             
                  end
         | 
| 53 53 |  | 
| 54 | 
            -
                  serialize &->(obj | 
| 54 | 
            +
                  serialize &->(obj) do
         | 
| 55 55 | 
             
                    obj
         | 
| 56 56 | 
             
                  end
         | 
| 57 57 | 
             
                end
         | 
| @@ -71,8 +71,8 @@ module Nested | |
| 71 71 | 
             
                def serialize(*args, &block)
         | 
| 72 72 | 
             
                  raise "pass either *args or &block" if args.empty? && !block
         | 
| 73 73 |  | 
| 74 | 
            -
                  @__serialize = ->(obj | 
| 75 | 
            -
                    obj = self.instance_exec(obj,  | 
| 74 | 
            +
                  @__serialize = ->(obj) do
         | 
| 75 | 
            +
                    obj = self.instance_exec(obj, &block) if block
         | 
| 76 76 | 
             
                    obj = obj.attributes if obj.is_a?(ActiveRecord::Base)
         | 
| 77 77 | 
             
                    obj = obj.symbolize_keys.slice(*args) unless args.empty?
         | 
| 78 78 | 
             
                    obj
         | 
| @@ -166,30 +166,32 @@ module Nested | |
| 166 166 | 
             
                # --------------------------
         | 
| 167 167 |  | 
| 168 168 | 
             
                def sinatra_init(sinatra)
         | 
| 169 | 
            -
                  sinatra. | 
| 169 | 
            +
                  sinatra.instance_variable_set("@__resource", self)
         | 
| 170 | 
            +
                  sinatra.instance_exec(&@__init)
         | 
| 170 171 | 
             
                end
         | 
| 171 172 |  | 
| 172 173 | 
             
                def sinatra_exec_get_block(sinatra, &block)
         | 
| 173 | 
            -
                  sinatra.instance_exec( | 
| 174 | 
            +
                  sinatra.instance_exec(&block)
         | 
| 174 175 | 
             
                end
         | 
| 175 176 |  | 
| 176 177 | 
             
                def sinatra_exec_delete_block(sinatra, &block)
         | 
| 177 | 
            -
                  sinatra.instance_exec( | 
| 178 | 
            +
                  sinatra.instance_exec(&block)
         | 
| 178 179 | 
             
                end
         | 
| 179 180 |  | 
| 180 | 
            -
                def  | 
| 181 | 
            +
                def sinatra_init_data(sinatra, &block)
         | 
| 181 182 | 
             
                  sinatra.request.body.rewind
         | 
| 182 | 
            -
                  HashWithIndifferentAccess.new | 
| 183 | 
            +
                  @__raw_data = HashWithIndifferentAccess.new(JSON.parse(sinatra.request.body.read))
         | 
| 184 | 
            +
                  @__data = @__raw_data.values_at(*block.parameters.map(&:last))
         | 
| 183 185 | 
             
                end
         | 
| 184 186 |  | 
| 185 187 | 
             
                def sinatra_exec_put_block(sinatra, &block)
         | 
| 186 | 
            -
                   | 
| 187 | 
            -
                  sinatra.instance_exec( | 
| 188 | 
            +
                  sinatra_init_data(sinatra, &block)
         | 
| 189 | 
            +
                  sinatra.instance_exec(*@__data, &block)
         | 
| 188 190 | 
             
                end
         | 
| 189 191 |  | 
| 190 192 | 
             
                def sinatra_exec_post_block(sinatra, &block)
         | 
| 191 | 
            -
                   | 
| 192 | 
            -
                  res = sinatra.instance_exec( | 
| 193 | 
            +
                  sinatra_init_data(sinatra, &block)
         | 
| 194 | 
            +
                  res = sinatra.instance_exec(*@__data, &block)
         | 
| 193 195 | 
             
                  sinatra.instance_variable_set("@#{self.instance_variable_name}", res)
         | 
| 194 196 | 
             
                end
         | 
| 195 197 |  | 
| @@ -209,9 +211,9 @@ module Nested | |
| 209 211 |  | 
| 210 212 | 
             
                def sinatra_response_create_data(sinatra, response)
         | 
| 211 213 | 
             
                  data = if response && collection?
         | 
| 212 | 
            -
                    response.to_a.map{|e| sinatra.instance_exec(e,  | 
| 214 | 
            +
                    response.to_a.map{|e| sinatra.instance_exec(e, &@__serialize) }
         | 
| 213 215 | 
             
                  else
         | 
| 214 | 
            -
                    sinatra.instance_exec(response,  | 
| 216 | 
            +
                    sinatra.instance_exec(response, &@__serialize)
         | 
| 215 217 | 
             
                  end
         | 
| 216 218 |  | 
| 217 219 | 
             
                  {data: data, ok: true}
         | 
    
        data/nested.gemspec
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            Gem::Specification.new do |s|
         | 
| 2 2 | 
             
              s.name               = "nested"
         | 
| 3 | 
            -
              s.version            = "0.0. | 
| 3 | 
            +
              s.version            = "0.0.9"
         | 
| 4 4 |  | 
| 5 5 | 
             
              s.authors = ["Jan Zimmek"]
         | 
| 6 6 | 
             
              s.email = %q{jan.zimmek@web.de}
         | 
| @@ -15,6 +15,7 @@ Gem::Specification.new do |s| | |
| 15 15 | 
             
              s.require_paths = ["lib"]
         | 
| 16 16 |  | 
| 17 17 | 
             
              s.add_runtime_dependency "activesupport"
         | 
| 18 | 
            +
              s.add_runtime_dependency "activerecord"
         | 
| 18 19 | 
             
              s.add_runtime_dependency "sinatra"
         | 
| 19 20 | 
             
              s.add_runtime_dependency "json"
         | 
| 20 21 | 
             
            end
         | 
    
        data/test/nested_test.rb
    CHANGED
    
    | @@ -74,7 +74,7 @@ class NestedTest < Test::Unit::TestCase | |
| 74 74 |  | 
| 75 75 | 
             
                @r.serialize :name
         | 
| 76 76 |  | 
| 77 | 
            -
                assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true} | 
| 77 | 
            +
                assert_equal({name: :joe}, @r.instance_variable_get("@__serialize").call({name: :joe, test: true}))
         | 
| 78 78 | 
             
              end
         | 
| 79 79 |  | 
| 80 80 | 
             
              def test_route
         | 
| @@ -314,10 +314,10 @@ class NestedTest < Test::Unit::TestCase | |
| 314 314 | 
             
                singleton!
         | 
| 315 315 | 
             
                # assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
         | 
| 316 316 |  | 
| 317 | 
            -
                ser = ->(obj | 
| 317 | 
            +
                ser = ->(obj) { obj }
         | 
| 318 318 | 
             
                @r.serialize &ser
         | 
| 319 319 |  | 
| 320 | 
            -
                assert_equal  | 
| 320 | 
            +
                assert_equal 1, @r.instance_variable_get("@__serialize").call(1)
         | 
| 321 321 | 
             
              end
         | 
| 322 322 |  | 
| 323 323 | 
             
              # ----
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: nested
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.9
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Jan Zimmek
         | 
| @@ -24,6 +24,20 @@ dependencies: | |
| 24 24 | 
             
                - - '>='
         | 
| 25 25 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 26 | 
             
                    version: '0'
         | 
| 27 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 28 | 
            +
              name: activerecord
         | 
| 29 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 30 | 
            +
                requirements:
         | 
| 31 | 
            +
                - - '>='
         | 
| 32 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 33 | 
            +
                    version: '0'
         | 
| 34 | 
            +
              type: :runtime
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - '>='
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '0'
         | 
| 27 41 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 28 42 | 
             
              name: sinatra
         | 
| 29 43 | 
             
              requirement: !ruby/object:Gem::Requirement
         |