setsy 0.1.0

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: 210367dcac6e27b4fb49e9a491107c4ad88fd737
4
+ data.tar.gz: e71804965a62e65dd52f0f49accf28ed61de66f9
5
+ SHA512:
6
+ metadata.gz: 343d21de9e910dcd54187542504e813bda10814a17c9c69c248f463f2a5cd3862e43f6e23f12d1b5e0adea46ac65f1d7a35a3529eed2bf67399fa91aefc23490
7
+ data.tar.gz: 15cf4707ef8fccdb99fc6b247c053fff760d813022daff1cd8e79b0f4563baf4b82029cd6f657c6f52fa07681e90e477587288abb362aec30e8169f222961cdc
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+ .DS_Store
12
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in setsy.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # Setsy
2
+
3
+ Settings for your classes. Depends on ActiveRecord, ActiveModel, and ActiveSupport 5.1 (okay so Rails 5.1+), but a PR can take care of this easily!
4
+
5
+ ## Note
6
+
7
+ This is not very tidy as it is. But it's battle-tested.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'setsy'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install setsy
24
+
25
+ ## Usage
26
+
27
+ First, create a migration for a JSONB column. Call it `settings_data`, or something. Just don't call it `settings` if you want to be able to call `settings` on an instance. Default of `{}`
28
+
29
+ In `app/models/user.rb`:
30
+
31
+ ```ruby
32
+ class User < ApplicationRecord
33
+
34
+ include ::Setsy::DSL
35
+
36
+ # each setting can be a hash, like posts_limit, or just
37
+ DEFAULT_SETTINGS = {
38
+ posts_limit: { value: 10 },
39
+ favorite_color: 'blue'
40
+ }.freeze
41
+
42
+ # do some stuff.
43
+ # setsy <attribute_name>, <options>, <block of readers>
44
+ setsy :settings, column: :settings_data, defaults: DEFAULT_SETTINGS do |conf|
45
+ conf.reader :posts_limit_and_color do
46
+ "posts limit is #{posts_limit} and color is #{favorite_color}"
47
+ end
48
+ end
49
+ end
50
+ ```
51
+
52
+ These attributes will become available on `User#settings`, and will be backed by the `settings_data` column which you created as a JSONB column.
53
+
54
+ Then in your controller or view or something,
55
+
56
+ ```erb
57
+ <% @user = User.first %>
58
+ <% if @user.settings.posts_limit.default? %>
59
+ Posts limit is default.
60
+ <% else %>
61
+ Posts limit is <%= @user.settings.posts_limit %>
62
+ <% end %>
63
+ <%= @user.settings.posts_limit_and_color %>
64
+ ```
65
+
66
+ ## Testing
67
+
68
+ See `spec`
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshmn/setsy.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "setsy"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/setsy.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'setsy/attribute'
2
+ require 'setsy/configuration'
3
+ require 'setsy/dsl'
4
+ require 'setsy/version'
5
+
6
+ module Setsy
7
+ end
@@ -0,0 +1,49 @@
1
+ module Setsy
2
+ class Attribute
3
+ KLASS_MAP = {
4
+ 'true' => 'Boolean',
5
+ 'false' => 'Boolean',
6
+ 'trueclass' => 'Boolean',
7
+ 'falseclass' => 'Boolean',
8
+ 'fixnum' => 'Integer'
9
+ }.freeze
10
+
11
+ delegate_missing_to :to_s
12
+ def initialize(options)
13
+ @attribute_value = options[:value]
14
+ @default = options[:default]
15
+ @options = options
16
+ end
17
+
18
+ def to_s
19
+ @to_s ||= cast(@attribute_value).to_s
20
+ end
21
+
22
+ def value
23
+ @value ||= cast(@attribute_value)
24
+ end
25
+ alias as_json value
26
+
27
+ def default
28
+ @default ||= cast(@default)
29
+ end
30
+
31
+ def default?
32
+ value == default
33
+ end
34
+
35
+ private
36
+
37
+ def klass
38
+ @klass ||= begin
39
+ klass = @default.class.to_s
40
+ klass = KLASS_MAP[klass.to_s.downcase] || klass
41
+ klass.downcase.to_sym
42
+ end
43
+ end
44
+
45
+ def cast(val)
46
+ ActiveModel::Type.lookup(klass).cast(val)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,74 @@
1
+ module Setsy
2
+ class Configuration
3
+ extend ActiveModel::Naming
4
+ include ActiveModel::Model
5
+ include ActiveModel::Serializers::JSON
6
+ SETTING_PREFIX = 'setting__'.freeze
7
+
8
+ def self.readers
9
+ @readers ||= {}
10
+ end
11
+
12
+ def self.reader(name, &block)
13
+ readers[name] = block
14
+ readers
15
+ end
16
+
17
+ def self.from_set(record, set, readers = {})
18
+ settings = {}
19
+ set = set.dup.with_indifferent_access
20
+ record.class.setsy_default.each do |k, v|
21
+ result = {}
22
+ result[:value] = v.is_a?(Hash) ? v[:value] : v
23
+ result[:default] = result[:value]
24
+ result[:value] = set[k] if set[k]
25
+ settings[k] = Attribute.new(result)
26
+ end
27
+ new(record, settings, readers)
28
+ end
29
+
30
+ def initialize(record, settings, readers = {})
31
+ @record = record
32
+ @settings = settings
33
+ write_readers(readers)
34
+ end
35
+
36
+ def method_missing(m, *args, &block)
37
+ if @settings.key?(m)
38
+ @settings[m]
39
+ elsif respond_to?("#{SETTING_PREFIX}#{m}")
40
+ send("#{SETTING_PREFIX}#{m}")
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ def respond_to_missing?(m, include_private = false)
47
+ @settings.key?(m) || "#{SETTING_PREFIX}#{m}" || super
48
+ end
49
+
50
+ def attributes
51
+ keys = @settings.keys
52
+ keys.push(*methods.select { |m| m.to_s.starts_with?(SETTING_PREFIX) }.map { |m| m.to_s.gsub(SETTING_PREFIX, '') })
53
+ h = {}
54
+ keys.each do |k|
55
+ h[k.to_sym] = k
56
+ end
57
+ h
58
+ end
59
+
60
+ private
61
+
62
+ def write_readers(readers)
63
+ readers.each { |k,v| write_reader(k, v) }
64
+ end
65
+
66
+ def write_reader(k, v)
67
+ class_eval do
68
+ define_method("#{SETTING_PREFIX}#{k}") do
69
+ instance_eval &v
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
data/lib/setsy/dsl.rb ADDED
@@ -0,0 +1,37 @@
1
+ module Setsy
2
+ module DSL
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def setsy(attribute_name, options = {})
9
+ readers = {}
10
+ readers = yield(Setsy::Configuration) if block_given?
11
+ class_eval do
12
+ define_method(:setsy_configuration) do
13
+ options[:defaults] || {}
14
+ end
15
+ define_singleton_method(:setsy_default) do
16
+ options[:defaults] || {}
17
+ end
18
+ end
19
+ instance_eval do
20
+ define_method(:setsy_configuration) do
21
+ if send(options[:column]).try(:empty?)
22
+ self.class.setsy_default
23
+ else
24
+ send(options[:column])
25
+ end
26
+ end
27
+ define_method(:setsy_default) do
28
+ self.class.setsy_default
29
+ end
30
+ define_method(attribute_name) do
31
+ Setsy::Configuration.from_set(self, setsy_configuration, readers)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Setsy
2
+ VERSION = "0.1.0"
3
+ end
data/setsy.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'setsy/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'setsy'
7
+ spec.version = Setsy::VERSION
8
+ spec.authors = ['Josh Brody']
9
+ spec.email = ['josh@josh.mn']
10
+ spec.licenses = ['MIT']
11
+
12
+ spec.required_ruby_version = '>= 2.2'
13
+ spec.summary = 'Settings for your classes'
14
+ spec.description = 'Settings for your classes'
15
+ spec.homepage = 'https://github.com/joshmn/setsy'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+ spec.files += Dir['lib/**/*.rb']
24
+
25
+ spec.add_dependency 'rails', '>= 5.1.0'
26
+ spec.add_development_dependency 'sqlite3'
27
+ spec.add_development_dependency 'bundler', '~> 1.15'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'pry', '~> 0.11.3'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: setsy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.11.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.11.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Settings for your classes
98
+ email:
99
+ - josh@josh.mn
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - README.md
107
+ - Rakefile
108
+ - bin/console
109
+ - bin/setup
110
+ - lib/setsy.rb
111
+ - lib/setsy/attribute.rb
112
+ - lib/setsy/configuration.rb
113
+ - lib/setsy/dsl.rb
114
+ - lib/setsy/version.rb
115
+ - setsy.gemspec
116
+ homepage: https://github.com/joshmn/setsy
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '2.2'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.13
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Settings for your classes
140
+ test_files: []