essay 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c397065af85bfaa45635847b7a96c1ec7e75a7b4
4
+ data.tar.gz: a4ab9c5a04548f6cccea372596e468bf9fde4cdb
5
+ SHA512:
6
+ metadata.gz: 58772dc8270cfb20289db16e7fee4969fed6e834ba67dfef46c86056bb71ad11a15ab00d0e0af02714eacca0944436c917f34979917281b9d27873a4f2ad4991
7
+ data.tar.gz: 8e36d383b357f52fa25835217f2657ec7da439e9639e9826dd9efb9a1730bc72e7fb570cbdc47af8297445b18fe484abf7edcbfd5d5962a5e8acd893327cbf64
data/.gitignore ADDED
@@ -0,0 +1,15 @@
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
15
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in confo.gemspec
4
+ gemspec
5
+
6
+ gem 'activerecord-traits', path: '~/Development/activerecord-traits'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Yaroslav Konoplov
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,29 @@
1
+ # Essay
2
+
3
+ ##### This library provides a series of classes to obtain plugin information of ActiveRecord models.
4
+ This library is focused only on ***custom*** ActiveRecord features and will not provide information for default ActiveRecord features.
5
+
6
+ ### The problem
7
+ There are a lot of plugins for ActiveRecord. For example:
8
+
9
+ - Plugins for translation (Globalize)
10
+ - Plugins for file attachments (CarrierWave, PaperClip)
11
+ - Plugins for safe deleting (Paranoid)
12
+ - Plugins for taggings
13
+ - Plugins for geocoding
14
+ - Plugins for social networks (likes, shares etc)
15
+ - and others...
16
+
17
+ Each of this plugins adds and/or removes and/or modifies behaviours of model. For example: adds callbacks, validations, methods, adds some imaginary meaning for attributes (good example: `deleted_at` when using paranoid). Each plugin uses different way of storing its options.
18
+
19
+ So, what if we need to know what plugins model is using and plugin options? ActiveRecord does not provide standardized way of developing and storing plugin options. **This library provides a series of classes and utilities for creating essay writers (classes which provide information about specific ActiveRecord plugin) with unified API design.**
20
+
21
+ ### Example for Globalize
22
+ ```ruby
23
+ # TODO
24
+ ```
25
+
26
+ ## Gemfile
27
+ ```ruby
28
+ gem 'essay', github: 'yivo/essay'
29
+ ```
data/essay.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require File.expand_path('../lib/essay/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'essay'
6
+ s.version = Essay::VERSION
7
+ s.authors = ['Yaroslav Konoplov']
8
+ s.email = ['eahome00@gmail.com']
9
+ s.summary = 'Feature information of activerecord models'
10
+ s.description = 'Feature information of activerecord models, attributes and associations'
11
+ s.homepage = 'http://github.com/yivo/essay'
12
+ s.license = 'MIT'
13
+
14
+ s.executables = `git ls-files -z -- bin/*`.split("\x0").map{ |f| File.basename(f) }
15
+ s.files = `git ls-files -z`.split("\x0")
16
+ s.test_files = `git ls-files -z -- {test,spec,features}/*`.split("\x0")
17
+ s.require_paths = ['lib']
18
+
19
+ s.add_dependency 'activesupport', '>= 3.0', '< 6.0'
20
+ s.add_dependency 'activerecord', '>= 3.0', '< 6.0'
21
+ s.add_dependency 'traits', '~> 1.0'
22
+ end
data/lib/essay.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+ require 'active_support/all'
3
+ require 'active_record'
4
+
5
+ require 'essay/model/model_features'
6
+ require 'essay/model/base_feature'
7
+
8
+ require 'essay/attribute/attribute_collection'
9
+ require 'essay/attribute/attribute_features'
10
+ require 'essay/attribute/base_feature'
11
+
12
+ require 'essay/association/association_collection'
13
+ require 'essay/association/association_features'
14
+ require 'essay/association/base_feature'
15
+
16
+ class ActiveRecord::Base
17
+ class << self
18
+ def features
19
+ @features_for_model ||= Essay::ModelFeatures.new(model_class: self)
20
+ end
21
+
22
+ def attribute_features(attr_name = nil)
23
+ @features_for_attrs ||= Essay::AttributeCollection.new(model_class: self)
24
+ attr_name ? @features_for_attrs[attr_name] : @features_for_attrs
25
+ end
26
+
27
+ def association_features(assoc_name = nil)
28
+ @features_for_assocs ||= Essay::AssociationCollection.new(model_class: self)
29
+ assoc_name ? @features_for_assocs[assoc_name] : @features_for_assocs
30
+ end
31
+
32
+ def essay_for(attr_or_assoc)
33
+ raise NotImplmentedError
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/helpers/model_helper'
3
+
4
+ module Essay
5
+ class AbstractCollection
6
+ include ModelHelper
7
+
8
+ def initialize(env)
9
+ @env = env
10
+ @model_class = env.fetch(:model_class)
11
+ @features_for = {}
12
+ end
13
+
14
+ def [](name)
15
+ @features_for[convert_key(name)] ||= construct_features(name)
16
+ end
17
+
18
+ def load_contents
19
+
20
+ end
21
+
22
+ def to_hash
23
+ @features_for.each_with_object({}) do |(k, v), memo|
24
+ memo[k] = v.to_hash
25
+ end
26
+ end
27
+
28
+ protected
29
+ def construct_features(name)
30
+
31
+ end
32
+
33
+ def convert_key(key)
34
+ key.kind_of?(Symbol) ? key : key.to_sym
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/helpers/model_helper'
3
+ require 'essay/helpers/serialize_helper'
4
+ require 'essay/helpers/description_helper'
5
+
6
+ module Essay
7
+ class AbstractFeature
8
+ include ModelHelper
9
+ include SerializeHelper
10
+ include DescriptionHelper
11
+
12
+ def initialize(env)
13
+ @env = env
14
+ @model_class = env.fetch(:model_class)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/helpers/model_helper'
3
+ require 'essay/helpers/feature_helper'
4
+ require 'essay/helpers/serialize_helper'
5
+
6
+ module Essay
7
+ class AbstractFeatures
8
+ include ModelHelper
9
+ include FeatureHelper
10
+ include SerializeHelper
11
+
12
+ attr_reader :env
13
+
14
+ def initialize(env)
15
+ @env = env
16
+ @model_class = env.fetch(:model_class)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_collection'
3
+
4
+ module Essay
5
+ class AssociationCollection < AbstractCollection
6
+ def construct_features(assoc_name)
7
+ Essay::AssociationFeatures.new(@env.merge(association_name: assoc_name))
8
+ end
9
+
10
+ protected :construct_features
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_features'
3
+ require 'essay/helpers/association_helper'
4
+
5
+ module Essay
6
+ class AssociationFeatures < AbstractFeatures
7
+ include AssociationHelper
8
+
9
+ def initialize(env)
10
+ super
11
+ @association_name = env.fetch(:association_name)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_feature'
3
+ require 'essay/helpers/association_helper'
4
+
5
+ module Essay
6
+ class AssociationFeatures::Base < AbstractFeature
7
+ include AssociationHelper
8
+
9
+ def initialize(env)
10
+ super
11
+ @association_name = env.fetch(:association_name)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_collection'
3
+
4
+ module Essay
5
+ class AttributeCollection < AbstractCollection
6
+ def construct_features(attr_name)
7
+ Essay::AttributeFeatures.new(@env.merge(attribute_name: attr_name))
8
+ end
9
+
10
+ protected :construct_features
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_features'
3
+ require 'essay/helpers/attribute_helper'
4
+
5
+ module Essay
6
+ class AttributeFeatures < AbstractFeatures
7
+ include AttributeHelper
8
+
9
+ def initialize(env)
10
+ super
11
+ @attribute_name = env.fetch(:attribute_name)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_feature'
3
+ require 'essay/helpers/attribute_helper'
4
+
5
+ module Essay
6
+ class AttributeFeatures::Base < AbstractFeature
7
+ include AttributeHelper
8
+
9
+ def initialize(env)
10
+ super
11
+ @attribute_name = env.fetch(:attribute_name)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module AssociationHelper
4
+ def association_name
5
+ @association_name
6
+ end
7
+
8
+ def association_traits
9
+ model_traits.associations[association_name]
10
+ end
11
+
12
+ alias association association_traits
13
+ alias this_association association
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module AttributeHelper
4
+ def attribute_name
5
+ @attribute_name
6
+ end
7
+
8
+ def attribute_traits
9
+ model_traits.attributes[attribute_name]
10
+ end
11
+
12
+ alias attribute attribute_traits
13
+ alias this_attribute attribute
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module DescriptionHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :description, instance_predicate: false, instance_writer: false
8
+ end
9
+
10
+ module ClassMethods
11
+ def describe(description)
12
+ self.description = description
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module FeatureHelper
4
+ def is(thing)
5
+ send(thing)
6
+ end
7
+
8
+ def not(thing)
9
+ send(thing)
10
+ end
11
+
12
+ def with(thing, &block)
13
+ obj = send(thing)
14
+ block.call(obj) if obj
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module ModelHelper
4
+ def model_class
5
+ @model_class
6
+ end
7
+
8
+ def model_traits
9
+ model_class.traits
10
+ end
11
+
12
+ def model_associations
13
+ model_traits.associations
14
+ end
15
+
16
+ alias all_associations model_associations
17
+
18
+ def model_attributes
19
+ model_traits.attributes
20
+ end
21
+
22
+ alias all_attributes model_attributes
23
+
24
+ def model_features
25
+ model_class.features
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ module SerializeHelper
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def serialize(options = {}, &block)
8
+ serialize_steps << { block: block, options: options }
9
+ end
10
+
11
+ def serialize_steps
12
+ @serialize_steps ||= []
13
+ end
14
+ end
15
+
16
+ def to_hash
17
+ serialize_steps.each_with_object({}) do |step, result|
18
+ condition = step.fetch(:options)[:if]
19
+ if !condition || try(condition)
20
+ result.merge!(instance_exec(&step.fetch(:block)))
21
+ end
22
+ end
23
+ end
24
+
25
+ def serialize_steps
26
+ self.class.serialize_steps
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_feature'
3
+
4
+ module Essay
5
+ class ModelFeatures::Base < AbstractFeature
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'essay/abstract_features'
3
+
4
+ module Essay
5
+ class ModelFeatures < AbstractFeatures
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Essay
3
+ VERSION = '1.0.1'
4
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: essay
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yaroslav Konoplov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-20 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: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: activerecord
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6.0'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '3.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: traits
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.0'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '1.0'
67
+ description: Feature information of activerecord models, attributes and associations
68
+ email:
69
+ - eahome00@gmail.com
70
+ executables: []
71
+ extensions: []
72
+ extra_rdoc_files: []
73
+ files:
74
+ - ".gitignore"
75
+ - Gemfile
76
+ - LICENSE.txt
77
+ - README.md
78
+ - essay.gemspec
79
+ - lib/essay.rb
80
+ - lib/essay/abstract_collection.rb
81
+ - lib/essay/abstract_feature.rb
82
+ - lib/essay/abstract_features.rb
83
+ - lib/essay/association/association_collection.rb
84
+ - lib/essay/association/association_features.rb
85
+ - lib/essay/association/base_feature.rb
86
+ - lib/essay/attribute/attribute_collection.rb
87
+ - lib/essay/attribute/attribute_features.rb
88
+ - lib/essay/attribute/base_feature.rb
89
+ - lib/essay/helpers/association_helper.rb
90
+ - lib/essay/helpers/attribute_helper.rb
91
+ - lib/essay/helpers/description_helper.rb
92
+ - lib/essay/helpers/feature_helper.rb
93
+ - lib/essay/helpers/model_helper.rb
94
+ - lib/essay/helpers/serialize_helper.rb
95
+ - lib/essay/model/base_feature.rb
96
+ - lib/essay/model/model_features.rb
97
+ - lib/essay/version.rb
98
+ homepage: http://github.com/yivo/essay
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubyforge_project:
118
+ rubygems_version: 2.5.1
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Feature information of activerecord models
122
+ test_files: []