greenin 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4fcec74c431a69a9192f8ee8f7436e7c48b17d40
4
+ data.tar.gz: 16e62b0207b720d947f5d8a5c6142def7355b9dc
5
+ SHA512:
6
+ metadata.gz: de5a60367ba83a4c2426a8165e49e1c190b958fa2c0711a6b13f658c09ad30312ebe194585d711e50974640e8c596a72d2351f39faf59492cf06c7baf2a9b30e
7
+ data.tar.gz: f3a65a127430d94abcd9419c1b5b420509356b16b154364d84b60bd10f58224b7459bb72fb33e7c118d6d04ebe52e2f01822ee26324ebc724b83b0283c8d338a
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in greenin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Russkikh Artem
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # Greenin
2
+
3
+ Describe your Grape Entities in Rails Active Record models
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'greenin'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install greenin
20
+
21
+ Then run
22
+
23
+ $ rails g greenin:install
24
+
25
+ This generator creates an initializer with settings at `config/initializers/greenin_initializer.rb`. You can change method name and class name:
26
+ ```ruby
27
+ Greenin.setup do |config|
28
+ config.entity_method_name = 'entity'
29
+ config.entity_class_name = 'Entity'
30
+ end
31
+
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ Example describtion in Post model:
37
+
38
+ ```ruby
39
+ class Post < ActiveRecord::Base
40
+
41
+ # ...
42
+
43
+ class Entity < Grape::Entity
44
+ format_with(:unix) { |dt| dt.to_i }
45
+ root 'posts'
46
+
47
+ expose :id
48
+ with_options(format_with: :unix) do
49
+ expose :created_at
50
+ expose :updated_at
51
+ end
52
+ expose :title
53
+ expose :description
54
+ end
55
+
56
+ end
57
+ ```
58
+
59
+ So, now you can call entity like that:
60
+ ```ruby
61
+ Post::Entity
62
+ # => Post::Entity
63
+
64
+ Post.entity
65
+ # => Post::Entity
66
+
67
+ Post.all.entity
68
+ # => #<Post::Entity:0x007fb4610d8a00
69
+ # @object=#<ActiveRecord::Relation [...]>, @options={}>
70
+
71
+ Post.first.entity
72
+ # => #<Post::Entity:0x007fb462263090 @object=
73
+ # #<Post id: nil, ..., created_at: nil, updated_at: nil>, @options={}>
74
+ ```
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it ( https://github.com/itbeaver/greenin/fork )
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/greenin.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'greenin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'greenin'
8
+ spec.version = Greenin::VERSION
9
+ spec.authors = ['Russkikh Artem']
10
+ spec.email = ['russkikhartem@itbeaver.co']
11
+ spec.summary = 'Grape Entities in Rails models'
12
+ spec.description = 'Describe your Grape Entities in Rails Active Record models'
13
+ spec.homepage = 'http://itbeaver.co'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.7'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ end
@@ -0,0 +1,50 @@
1
+ module ActiveRecord
2
+ # Public: Add entity method to ActiveRecord::Relation class
3
+ #
4
+ # Examples
5
+ #
6
+ # User.all.entity
7
+ # # => #<User::Entity:0x007fb4610d8a00
8
+ # @object=#<ActiveRecord::Relation [...]>, @options={}>
9
+ class Relation
10
+ define_method(Greenin.entity_method_name) do
11
+ begin
12
+ entity = %(#{model}::#{Greenin.entity_class_name}).constantize
13
+ rescue NameError
14
+ entity = nil
15
+ end
16
+ entity.new(self) if entity
17
+ end
18
+ end
19
+
20
+ # Public: Add entity methods to ActiveRecord::Base class
21
+ #
22
+ # Examples
23
+ #
24
+ # User.entity
25
+ # # => User::Entity
26
+ #
27
+ # User.new.entity
28
+ # # => #<User::Entity:0x007fb462263090 @object=
29
+ # #<User id: nil, ..., created_at: nil, updated_at: nil>, @options={}>
30
+ class Base
31
+ define_singleton_method Greenin.entity_method_name do
32
+ begin
33
+ entity = %(#{self}::#{Greenin.entity_class_name}).constantize
34
+ rescue NameError
35
+ entity = nil
36
+ end
37
+ entity.new(self) if entity
38
+ end
39
+
40
+ define_method(Greenin.entity_method_name) do
41
+ begin
42
+ entity = %(#{self.class}::#{Greenin.entity_class_name})
43
+ .constantize
44
+ rescue NameError
45
+ entity = nil
46
+ end
47
+ entity.new(self) if entity
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ module Greenin
2
+ # Public: Add generator to project.
3
+ # Generator copies initializer to app
4
+ #
5
+ # Example:
6
+ #
7
+ # rails g greenin:install
8
+ class InstallGenerator < Rails::Generators::Base
9
+ source_root File.expand_path('../templates', __FILE__)
10
+ desc 'This generator creates an initializer file at config/initializers'
11
+ def create_initializer_file
12
+ copy_file 'initializer.rb', 'config/initializers/greenin_initializer.rb'
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ Greenin.setup do |config|
2
+ config.entity_method_name = 'entity'
3
+ config.entity_class_name = 'Entity'
4
+ end
@@ -0,0 +1,3 @@
1
+ module Greenin
2
+ VERSION = '1.0.0'
3
+ end
data/lib/greenin.rb ADDED
@@ -0,0 +1,39 @@
1
+ require 'greenin/version'
2
+
3
+ # Public: Greenin settings module
4
+ # You can customize entity method name and entity class name
5
+ #
6
+ # Examples
7
+ #
8
+ # Greenin.entity_method_name = 'form_ent'
9
+ # Greenin.entity_class_name = 'Ent'
10
+ #
11
+ # User::Ent
12
+ # # => User::Ent
13
+ # User.form_ent
14
+ # # => User::Entity
15
+ # User.all.form_ent
16
+ # # => #<User::Entity:0x007fb4610d8a00
17
+ # @object=#<ActiveRecord::Relation [...]>, @options={}>
18
+ # User.first.form_ent
19
+ # # => #<User::Entity:0x007fb462263090 @object=
20
+ # #<User id: nil, ..., created_at: nil, updated_at: nil>, @options={}>
21
+ module Greenin
22
+ mattr_accessor :entity_method_name
23
+ @@entity_method_name = 'entity'
24
+
25
+ mattr_accessor :entity_class_name
26
+ @@entity_class_name = 'Entity'
27
+
28
+ def self.setup
29
+ yield self
30
+ end
31
+ end
32
+
33
+ # Public: Generates methods after loading initializer
34
+ class Engine < ::Rails::Engine
35
+ initializer 'set_settings', after: :load_config_initializers,
36
+ before: :build_middleware_stack do
37
+ require 'active_record_overrides'
38
+ end
39
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: greenin
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Russkikh Artem
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Describe your Grape Entities in Rails Active Record models
42
+ email:
43
+ - russkikhartem@itbeaver.co
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - greenin.gemspec
54
+ - lib/active_record_overrides.rb
55
+ - lib/generators/greenin/install/install_generator.rb
56
+ - lib/generators/greenin/install/templates/initializer.rb
57
+ - lib/greenin.rb
58
+ - lib/greenin/version.rb
59
+ homepage: http://itbeaver.co
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.2
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Grape Entities in Rails models
83
+ test_files: []
84
+ has_rdoc: