vinber 0.2.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4a239685dad6d433c85b7f7bc324eed89635ec6d
4
- data.tar.gz: 586e58c4dbe8ae46b22a6951bb9ab147d0ab9ef5
2
+ SHA256:
3
+ metadata.gz: 7d88a36583dc665923d7501eb8d362d2ec5b82097cb0e17a48a048573c25460b
4
+ data.tar.gz: cd68c15583f3ef1e6387f01e1588dbc1752cf3df31072f46e75b1a20d5be0171
5
5
  SHA512:
6
- metadata.gz: da51f69910a0343affc046b921f302df20efac1a0c56b2dbd3e39df2d89ff99ea7d60cf9c83c5685d2dc18ec37443f422be4273f31e1c4921319fcd8ceb69913
7
- data.tar.gz: 01b7e272785dbbe2adf28d3d48cfd9e69eb6cc055ed317a677ac2774b4fbbd16eaec5d45b8344bb886ffce36ef017374000a18395b45e80d8d62bda18c8aa1bf
6
+ metadata.gz: d31db2f637cacacf108a40b3e9a6ec3e478b7dc779f745d54b81eafb9f2ccc05391b52781549d59d1f238a758aec283a1fcb17e2ea099de9accdf05edc77cee7
7
+ data.tar.gz: ef89d9a27bc7aa608dc62d7bf85f05a2a1fa72c0af4d7a604911ce1705879f75e291aae747c5ff2fb296ab17626a99f8d2d247a0c506235d10addc01a4e278f0
data/lib/vinber.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require_relative 'vinber/version'
2
2
  require_relative 'vinber/translate'
3
- require_relative 'vinber/vinber_list'
4
- require_relative 'vinber/vinber_value'
3
+ require_relative 'vinber/vinber_klass'
4
+ require_relative 'vinber/vinber_instance'
5
5
  require_relative 'vinber/current_vinbers'
6
6
 
7
7
  module Vinber
@@ -19,9 +19,11 @@ module Vinber
19
19
 
20
20
  def vinber(definitions)
21
21
  need_validates = definitions.delete(:validates)
22
+ need_scope = definitions.delete(:scope)
22
23
  definitions.each do |name, values|
23
24
  detect_vinber_conflict! name
24
25
  validates_from_vinber name, values if need_validates
26
+ scope_from_vinber name, values if need_scope
25
27
  defined_vinbers[name.to_s] = case
26
28
  when values.is_a?(Hash)
27
29
  values.with_indifferent_access
@@ -59,6 +61,14 @@ module Vinber
59
61
  class_eval { validates name.to_sym, inclusion: {in: val} }
60
62
  end
61
63
 
64
+ def scope_from_vinber(name, val)
65
+ if val.is_a?(Hash)
66
+ val.each do |key, value|
67
+ class_eval { scope key, -> { where(name => value) } }
68
+ end
69
+ end
70
+ end
71
+
62
72
  def raise_vinber_conflict_error(name, source)
63
73
  raise ArgumentError, VINBER_CONFLICT_MESSAGE % {
64
74
  name: name,
@@ -72,6 +82,6 @@ end
72
82
  # Extend/Include to ActiveRecord::Base
73
83
  ActiveRecord::Base.class_eval do
74
84
  extend Vinber
75
- extend Vinber::List
76
- include Vinber::Value
85
+ extend Vinber::Klass
86
+ include Vinber::Instance
77
87
  end
@@ -1,6 +1,8 @@
1
1
  module Vinber
2
2
  class Translate
3
3
 
4
+ VALUE_BOOL_LABLE = "vinber.%{key}"
5
+ VALUE_COMMON_LABLE = "vinber.%{attribute}_%{key}"
4
6
  VALUE_LABLE = "vinber.%{klass}.%{attribute}_%{key}"
5
7
 
6
8
  def initialize(klass, attribute, attribute_vinber_key)
@@ -16,7 +18,9 @@ module Vinber
16
18
  end
17
19
 
18
20
  def text
19
- @text ||= I18n.t (VALUE_LABLE % label), default: label[:key].to_s.humanize
21
+ @text ||= I18n.t((VALUE_LABLE % label), default: nil) ||
22
+ I18n.t((VALUE_COMMON_LABLE % label), default: nil) ||
23
+ I18n.t((VALUE_BOOL_LABLE % label), default: label[:key].to_s.humanize)
20
24
  end
21
25
 
22
26
  alias_method :to_s, :text
@@ -1,3 +1,3 @@
1
1
  module Vinber
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -1,5 +1,5 @@
1
1
  module Vinber
2
- module Value
2
+ module Instance
3
3
 
4
4
  def vinber_value(attribute, options = {})
5
5
  obj = self
@@ -12,6 +12,8 @@ module Vinber
12
12
 
13
13
  attribute_vinber_key = attribute_vinber.key val
14
14
  t ? Vinber::Translate.new(klass, attribute, attribute_vinber_key).to_s : attribute_vinber_key.to_s
15
+ elsif [true, false].include?(val)
16
+ Vinber::Translate.new(klass, attribute, val).to_s
15
17
  else
16
18
  val
17
19
  end
@@ -1,5 +1,5 @@
1
1
  module Vinber
2
- module List
2
+ module Klass
3
3
 
4
4
  def vinber_list(attribute, options = {}, &block)
5
5
  klass = self
@@ -17,5 +17,19 @@ module Vinber
17
17
  end.to_a
18
18
  end
19
19
 
20
+ def vinber_value(attribute, key_or_value)
21
+ klass = self
22
+ raise VinberUndefined, "Vinber was undefined in #{klass.name}" unless klass.vinber_defined?
23
+
24
+ if key_or_value.is_a?(Symbol)
25
+ key = key_or_value
26
+ else
27
+ attribute_vinber = vinbers.defined_vinbers || {}
28
+ key = attribute_vinber.key key_or_value
29
+ end
30
+
31
+ Vinber::Translate.new(klass, attribute, key).to_s
32
+ end
33
+
20
34
  end
21
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vinber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cenxky
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-07-17 00:00:00.000000000 Z
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,8 +58,8 @@ files:
58
58
  - lib/vinber/current_vinbers.rb
59
59
  - lib/vinber/translate.rb
60
60
  - lib/vinber/version.rb
61
- - lib/vinber/vinber_list.rb
62
- - lib/vinber/vinber_value.rb
61
+ - lib/vinber/vinber_instance.rb
62
+ - lib/vinber/vinber_klass.rb
63
63
  - vinber.gemspec
64
64
  homepage: https://github.com/cenxky/vinber
65
65
  licenses:
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.6.6
84
+ rubygems_version: 2.7.3
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Vinber is a rails enumeration tool, much easier for i18n.