simple_helpers 2.0.0.beta → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3760bdc12e9255f34b9063c8cd29e114ccf6575d
4
- data.tar.gz: 78da44fe321d6a08c0e2ad04e1efc06ff790f1ee
3
+ metadata.gz: ad5106d5e8ba4e9bb0e42eef4e4a105734d9933f
4
+ data.tar.gz: 095713110ea60c96ec9d0fa8eea86b73ce323021
5
5
  SHA512:
6
- metadata.gz: 8913a3406650bb585131a0e6b0fa36fac915d1c8f2059284ac47617177e1896a7c6d432f07232656c5b05068b7b35909ed884d5039fe3d8091a548c4528c360c
7
- data.tar.gz: ae85af3ed3457fec3ca431e88b679a627cd55eb799483b286775d6aca8aea33e2654fcfd5a5853798afc35bc472fc4e80fee486fdd0ddc59d0281e87cebe1479
6
+ metadata.gz: 4fa4003a2dd23424a253b7c160e85142dd2bb6bff361bd6fc065ee40e6509e07e89205f652e7a5613df3be6fd6c3008b7da9572b37abb3d5f83a1ed6a0450ea8
7
+ data.tar.gz: aa0a33dece45792fa98a62137c181848aee560be67b5a7e35dbe9d712b1cbc27afe8b0100c9ed5a73904a58a3ebc7045a3486ad3025093c640cfa3759c348088
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
data/README.md CHANGED
@@ -45,7 +45,7 @@ require "simple_helpers"
45
45
 
46
46
  SimpleHelpers.configure do |config|
47
47
  config.helpers = {
48
- page_title: { prefix: "MyBlog", separator: " - " }
48
+ page_title: { prefix: "MyBlog" } # other options: suffix, separator
49
49
  }
50
50
  end
51
51
  ```
@@ -1,5 +1,7 @@
1
1
  module SimpleHelpers
2
2
  class Config
3
+ DEFAULT_SEPARATOR = " · "
4
+
3
5
  def self.helpers
4
6
  @helpers ||= {}
5
7
  end
@@ -19,7 +19,6 @@ module SimpleHelpers
19
19
  initialize_method.bind(self).call(*args)
20
20
  end
21
21
  end
22
- ::I18n.load_path += Dir[File.dirname(__FILE__) + "/../../locales/*.yml"]
23
22
  end
24
23
  end
25
24
  end
@@ -24,27 +24,10 @@ module SimpleHelpers
24
24
  }
25
25
  end
26
26
 
27
- def self.template(controller, context)
28
- context.scan(/{{.*?}}/i).each do |key|
29
- parts = key.delete("{}").split(".")
30
-
27
+ def self.parser(controller, context)
28
+ entries(context).each do |key|
31
29
  begin
32
- first_argument = parts.shift
33
- if first_argument.start_with? "@"
34
- instance_variable = controller.instance_variable_get(first_argument)
35
-
36
- value = if parts.empty?
37
- instance_variable
38
- else
39
- thing = parts.shift.to_sym
40
- instance_variable[thing] if instance_variable.respond_to?(thing)
41
- end
42
- else
43
- value = controller.send first_argument
44
- end
45
- value = "" unless value
46
- rescue
47
- value = ""
30
+ value = simple_helper_value(controller, key)
48
31
  ensure
49
32
  context.sub!(key, value)
50
33
  end
@@ -61,7 +44,9 @@ module SimpleHelpers
61
44
  end
62
45
  end
63
46
 
64
- def self.get_content(scopes, options)
47
+ def self.get_i18n_content(controller, key, options)
48
+ scopes = SimpleHelpers::Support.scopes(controller, key)
49
+
65
50
  result = SimpleHelpers::Support.translate(scopes[:first], options)
66
51
  if result.empty?
67
52
  result = SimpleHelpers::Support.translate(scopes[:second], options)
@@ -84,23 +69,64 @@ module SimpleHelpers
84
69
  end
85
70
 
86
71
  define_method("#{name}_get") do |*args|
87
- scopes = SimpleHelpers::Support.scopes(controller, name)
88
- options_hash = instance_variable_get("@#{name}_options")
89
- result = SimpleHelpers::Support.get_content(scopes, options_hash)
90
- helper_options = SimpleHelpers::Config.helpers[name.to_sym]
91
-
92
- prefix = helper_options.fetch(:prefix, "")
93
- content = SimpleHelpers::Support.template(controller, result)
94
- separator = if prefix.empty? || content.empty?
95
- ""
96
- else
97
- helper_options.fetch(:separator, " - ")
98
- end
99
-
100
- "#{prefix}#{separator}#{content}"
72
+ "%{prefix}%{prefix_separator}%{content}%{suffix_separator}%{suffix}" %
73
+ SimpleHelpers::Support.simple_helper_parts(controller, name)
101
74
  end
102
75
  end
103
76
  end
104
77
 
78
+ private
79
+
80
+ def self.entries(context)
81
+ context.scan(/{{.*?}}/i)
82
+ end
83
+
84
+ def self.simple_helper_value(controller, key)
85
+ tag = key.delete("{}")
86
+ if tag.start_with? "@"
87
+ get_instance_variable(controller, tag)
88
+ else
89
+ get_from_controller(controller, tag)
90
+ end || ""
91
+ end
92
+
93
+ def self.get_instance_variable(controller, key)
94
+ parts = key.split(".")
95
+ first_argument = parts.shift
96
+ instance_variable = controller.instance_variable_get(first_argument)
97
+
98
+ if parts.empty?
99
+ instance_variable
100
+ else
101
+ thing = parts.shift.to_sym
102
+ instance_variable[thing] if instance_variable.respond_to?(thing)
103
+ end
104
+ end
105
+
106
+ def self.get_from_controller(controller, key)
107
+ controller.send key
108
+ end
109
+
110
+ def self.simple_helper_parts(controller, name)
111
+ name_options = controller.instance_variable_get("@#{name}_options") || {}
112
+ text_raw = SimpleHelpers::Support.get_i18n_content(controller, name, name_options)
113
+ content = SimpleHelpers::Support.parser(controller, text_raw)
114
+
115
+ helper_options = SimpleHelpers::Config.helpers[name.to_sym].merge(name_options)
116
+ prefix = helper_options.fetch(:prefix, "")
117
+ suffix = helper_options.fetch(:suffix, "")
118
+ separator = helper_options.fetch(:separator, SimpleHelpers::Config::DEFAULT_SEPARATOR)
119
+
120
+ {
121
+ prefix: prefix,
122
+ suffix: suffix,
123
+ separator: separator,
124
+ content: content,
125
+ prefix_separator: prefix.empty? || content.empty? ? "" : separator,
126
+ suffix_separator: suffix.empty? || content.empty? ? "" : separator
127
+ }
128
+ end
129
+
105
130
  end
131
+
106
132
  end
@@ -3,6 +3,6 @@ module SimpleHelpers
3
3
  MAJOR = 2
4
4
  MINOR = 0
5
5
  PATCH = 0
6
- STRING = "#{MAJOR}.#{MINOR}.#{PATCH}.beta"
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitor Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-01 00:00:00.000000000 Z
11
+ date: 2013-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -114,9 +114,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
114
  version: '0'
115
115
  required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - '>'
117
+ - - '>='
118
118
  - !ruby/object:Gem::Version
119
- version: 1.3.1
119
+ version: '0'
120
120
  requirements: []
121
121
  rubyforge_project:
122
122
  rubygems_version: 2.0.3