nagare 0.1.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 +7 -0
- data/lib/nagare.rb +131 -0
- data/lib/nagare/railtie.rb +43 -0
- metadata +74 -0
    
        checksums.yaml
    ADDED
    
    | @@ -0,0 +1,7 @@ | |
| 1 | 
            +
            ---
         | 
| 2 | 
            +
            SHA1:
         | 
| 3 | 
            +
              metadata.gz: 4cb3d508860caa3dc32dc42d5576fc1dc3319d7d
         | 
| 4 | 
            +
              data.tar.gz: a2bf374a731001a116227af14d9e1d06719a0f3c
         | 
| 5 | 
            +
            SHA512:
         | 
| 6 | 
            +
              metadata.gz: 35770de8b7dfec7e9d0d5513034888d124891baffe6a4dfbd978bb1413d903f6351bb2c7703c658131151b55cd399f36207882178214750b429ca094d59efb0c
         | 
| 7 | 
            +
              data.tar.gz: 0b9d7cbf9cc86d5f528c7b75c521afd2fa2b04ff71d170b04c486ba766d14cbb05433c29a7cba6f944484bb50808053f3fb11d78c28b1b4be337eb5ba78adcf1
         | 
    
        data/lib/nagare.rb
    ADDED
    
    | @@ -0,0 +1,131 @@ | |
| 1 | 
            +
            require 'active_support'
         | 
| 2 | 
            +
            require 'active_support/core_ext'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            module Nagare
         | 
| 5 | 
            +
              class Item
         | 
| 6 | 
            +
                def self._attributes
         | 
| 7 | 
            +
                  @attributes ||= []
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def self.attributes(*attributes)
         | 
| 11 | 
            +
                  @attributes = _attributes.concat(attributes)
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  attributes.each do |attribute|
         | 
| 14 | 
            +
                    define_method(attribute) do
         | 
| 15 | 
            +
                      if object.respond_to?(attribute)
         | 
| 16 | 
            +
                        object.send(attribute)
         | 
| 17 | 
            +
                      end
         | 
| 18 | 
            +
                    end
         | 
| 19 | 
            +
                  end
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                def initialize(object, context)
         | 
| 23 | 
            +
                  @object = object
         | 
| 24 | 
            +
                  @context = context
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                def attributes
         | 
| 28 | 
            +
                  self.class._attributes.inject({}) do |hash, attribute|
         | 
| 29 | 
            +
                    hash[attribute.to_s] = send(attribute)
         | 
| 30 | 
            +
                    hash
         | 
| 31 | 
            +
                  end.compact
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                def method_missing(key, *args)
         | 
| 35 | 
            +
                  if context.respond_to?(key)
         | 
| 36 | 
            +
                    context.send(key, *args)
         | 
| 37 | 
            +
                  else
         | 
| 38 | 
            +
                    super
         | 
| 39 | 
            +
                  end
         | 
| 40 | 
            +
                end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                def as_json(options = nil)
         | 
| 43 | 
            +
                  attributes
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                private
         | 
| 47 | 
            +
             | 
| 48 | 
            +
                attr_reader :object, :context
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              class Collection < Item
         | 
| 52 | 
            +
                include Enumerable
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                def self._key
         | 
| 55 | 
            +
                  @key || "items"
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                def self.key(key)
         | 
| 59 | 
            +
                  @key = key
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                def self.attributes(*attributes)
         | 
| 63 | 
            +
                  @attributes = _attributes.concat(attributes)
         | 
| 64 | 
            +
                end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                def initialize(collection, context, serializer:)
         | 
| 67 | 
            +
                  @collection = collection
         | 
| 68 | 
            +
                  @context = context
         | 
| 69 | 
            +
                  @serializer = serializer
         | 
| 70 | 
            +
                end
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                def each
         | 
| 73 | 
            +
                  collection.map do |item|
         | 
| 74 | 
            +
                    yield serializer.new(item, context)
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
                end
         | 
| 77 | 
            +
             | 
| 78 | 
            +
                def as_json(options = nil)
         | 
| 79 | 
            +
                  items = collection.map do |item|
         | 
| 80 | 
            +
                    serializer.new(item, context).as_json
         | 
| 81 | 
            +
                  end
         | 
| 82 | 
            +
             | 
| 83 | 
            +
                  attributes.merge({
         | 
| 84 | 
            +
                    self.class._key => items,
         | 
| 85 | 
            +
                  })
         | 
| 86 | 
            +
                end
         | 
| 87 | 
            +
             | 
| 88 | 
            +
                private
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                undef :object
         | 
| 91 | 
            +
                attr_reader :collection, :serializer
         | 
| 92 | 
            +
              end
         | 
| 93 | 
            +
             | 
| 94 | 
            +
              class Adapter
         | 
| 95 | 
            +
                def initialize(serializer)
         | 
| 96 | 
            +
                  @serializer = serializer
         | 
| 97 | 
            +
                end
         | 
| 98 | 
            +
             | 
| 99 | 
            +
                def as_json(options = nil)
         | 
| 100 | 
            +
                  serializer.as_json(options)
         | 
| 101 | 
            +
                end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                private
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                attr_reader :serializer
         | 
| 106 | 
            +
              end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
              class Context
         | 
| 109 | 
            +
                def initialize(attributes = {})
         | 
| 110 | 
            +
                  @attributes = attributes
         | 
| 111 | 
            +
                end
         | 
| 112 | 
            +
             | 
| 113 | 
            +
                def extend(attributes)
         | 
| 114 | 
            +
                  Context.new(@attributes.merge(attributes))
         | 
| 115 | 
            +
                end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
                def respond_to?(key, private_methods = false)
         | 
| 118 | 
            +
                  @attributes.has_key?(key) || super
         | 
| 119 | 
            +
                end
         | 
| 120 | 
            +
             | 
| 121 | 
            +
                def method_missing(method)
         | 
| 122 | 
            +
                  if @attributes.has_key?(method)
         | 
| 123 | 
            +
                    @attributes.fetch(method)
         | 
| 124 | 
            +
                  else
         | 
| 125 | 
            +
                    super
         | 
| 126 | 
            +
                  end
         | 
| 127 | 
            +
                end
         | 
| 128 | 
            +
              end
         | 
| 129 | 
            +
            end
         | 
| 130 | 
            +
             | 
| 131 | 
            +
            require 'nagare/railtie' if defined?(Rails)
         | 
| @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            module Nagare
         | 
| 2 | 
            +
              module Controller
         | 
| 3 | 
            +
                private
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                def nagare_adapter
         | 
| 6 | 
            +
                  Nagare::Adapter
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def nagare_context
         | 
| 10 | 
            +
                  @nagare_context ||= Nagare::Context.new
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def _render_with_renderer_json(resource, options)
         | 
| 14 | 
            +
                  serializers = options.fetch(:serializers, nil)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  if serializers.nil?
         | 
| 17 | 
            +
                    return super(resource, options)
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                  if resource.respond_to?(:each)
         | 
| 21 | 
            +
                    collection_serializer = serializers.fetch(:collection)
         | 
| 22 | 
            +
                    item_serializer = serializers.fetch(:item)
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    serializer = collection_serializer.
         | 
| 25 | 
            +
                      new(resource, nagare_context.extend(options.fetch(:context, {})), serializer: item_serializer)
         | 
| 26 | 
            +
                  else
         | 
| 27 | 
            +
                    item_serializer = serializers.fetch(:item)
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    serializer = item_serializer.new(resource, nagare_context.extend(options.fetch(:context, {})))
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  super(nagare_adapter.new(serializer), options)
         | 
| 33 | 
            +
                end
         | 
| 34 | 
            +
              end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
              class Railtie < Rails::Railtie
         | 
| 37 | 
            +
                initializer "nagare.action_controller" do
         | 
| 38 | 
            +
                  ActiveSupport.on_load(:action_controller) do
         | 
| 39 | 
            +
                    include Nagare::Controller
         | 
| 40 | 
            +
                  end
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
              end
         | 
| 43 | 
            +
            end
         | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,74 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: nagare
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 0.1.0
         | 
| 5 | 
            +
            platform: ruby
         | 
| 6 | 
            +
            authors:
         | 
| 7 | 
            +
            - Eric Oestrich
         | 
| 8 | 
            +
            autorequire: 
         | 
| 9 | 
            +
            bindir: bin
         | 
| 10 | 
            +
            cert_chain: []
         | 
| 11 | 
            +
            date: 2016-03-01 00:00:00.000000000 Z
         | 
| 12 | 
            +
            dependencies:
         | 
| 13 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 14 | 
            +
              name: activesupport
         | 
| 15 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 | 
            +
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '4.0'
         | 
| 20 | 
            +
              type: :runtime
         | 
| 21 | 
            +
              prerelease: false
         | 
| 22 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 | 
            +
                requirements:
         | 
| 24 | 
            +
                - - "~>"
         | 
| 25 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 26 | 
            +
                    version: '4.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: '3.0'
         | 
| 34 | 
            +
              type: :development
         | 
| 35 | 
            +
              prerelease: false
         | 
| 36 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 37 | 
            +
                requirements:
         | 
| 38 | 
            +
                - - "~>"
         | 
| 39 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 40 | 
            +
                    version: '3.0'
         | 
| 41 | 
            +
            description: Serialize your ruby objects
         | 
| 42 | 
            +
            email:
         | 
| 43 | 
            +
            - eric@oestrich.org
         | 
| 44 | 
            +
            executables: []
         | 
| 45 | 
            +
            extensions: []
         | 
| 46 | 
            +
            extra_rdoc_files: []
         | 
| 47 | 
            +
            files:
         | 
| 48 | 
            +
            - lib/nagare.rb
         | 
| 49 | 
            +
            - lib/nagare/railtie.rb
         | 
| 50 | 
            +
            homepage: http://github.com/oestrich/nagare
         | 
| 51 | 
            +
            licenses:
         | 
| 52 | 
            +
            - MIT
         | 
| 53 | 
            +
            metadata: {}
         | 
| 54 | 
            +
            post_install_message: 
         | 
| 55 | 
            +
            rdoc_options: []
         | 
| 56 | 
            +
            require_paths:
         | 
| 57 | 
            +
            - lib
         | 
| 58 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 59 | 
            +
              requirements:
         | 
| 60 | 
            +
              - - ">="
         | 
| 61 | 
            +
                - !ruby/object:Gem::Version
         | 
| 62 | 
            +
                  version: '0'
         | 
| 63 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 64 | 
            +
              requirements:
         | 
| 65 | 
            +
              - - ">="
         | 
| 66 | 
            +
                - !ruby/object:Gem::Version
         | 
| 67 | 
            +
                  version: 1.3.6
         | 
| 68 | 
            +
            requirements: []
         | 
| 69 | 
            +
            rubyforge_project: 
         | 
| 70 | 
            +
            rubygems_version: 2.5.1
         | 
| 71 | 
            +
            signing_key: 
         | 
| 72 | 
            +
            specification_version: 4
         | 
| 73 | 
            +
            summary: Serialize your ruby objects
         | 
| 74 | 
            +
            test_files: []
         |