tram-page 0.1.4 → 0.1.5

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: 2da2c8691b77724f4c2cf404654c5ef1a3c7e574
4
- data.tar.gz: a4ad045bf0497258617b20ce47b0dfabf8e399fd
3
+ metadata.gz: b03850fffdac958f2626d16b333e0a15439ee498
4
+ data.tar.gz: 185226f1ed9ddbdb0c26b3bf1837fd741bbfb8bc
5
5
  SHA512:
6
- metadata.gz: 1019255dafb0500aa25040e48786b1463de3d0e514f63144fec24e5705128977f39137a547e21af8be92ca54c5df772bc6dbaa2ed13b035e34814cc1ae5c49dc
7
- data.tar.gz: fde7d0cc7d8a5c7b8661931fd248bb4e55f6282ed8cca52c8b789c6176212b7d214241fba779ac0c7719a40b37d8f2f78b92306a9f9eeedea74450d376676b99
6
+ metadata.gz: 5880b0e19b2b71db67a45e26329ad58ba4dd4d708ad8b909a6f2af07bb7e0e1c2dda1dab8b3d10412f157e5d96fac31b3427bfec48b9e094730b101d27dfa897
7
+ data.tar.gz: d4836017f4f9a69322f78d7d8fef248fe57b95599457e362600e3d00dd3ca2ffb8029dd2ebe7d54fa4e7ecb2bc1a45f6f1103dc9646d685af10e9a19e1eeabd3
data/lib/tram/page.rb CHANGED
@@ -1,67 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Tram::Page
4
- extend ::Dry::Initializer::Mixin
4
+ extend ::Dry::Initializer
5
+
6
+ require_relative "page/section"
5
7
 
6
8
  class << self
7
9
  attr_accessor :i18n_scope
8
10
 
9
- def section(name, options = {})
10
- @__sections ||= []
11
+ # Defines a section of the page as a reference to its public method,
12
+ # and creates/overloads the method if necessary.
13
+ #
14
+ # @param [#to_sym] name The name of the **section**
15
+ #
16
+ # @option [Boolean] :overload
17
+ # If this definition can overload a previous one
18
+ # @option [Proc] :value (nil)
19
+ # A new content of the referred method
20
+ # @option options [#to_sym] :method (name)
21
+ # The name of public method to take value from
22
+ # @option options [#to_sym] :if (nil)
23
+ # The name of public method to check if the section should be displayed
24
+ # @option options [#to_sym] :unless (nil)
25
+ # The name of public method to check if the section should be hidden
26
+ #
27
+ # @return [self] itself
28
+ #
29
+ def section(name, overload: false, value: nil, **options)
30
+ name = name.to_sym
31
+ raise "Section #{name} already exists" if !overload && sections.key?(name)
11
32
 
12
- n = name.to_sym
13
- if @__sections.map(&:first).include?(n)
14
- raise "Section #{n} already exists"
15
- end
33
+ section = Section.new(name, options)
34
+ define_method(section.source, &value) if value
16
35
 
17
- n = define_section_method(n, options)
18
- @__sections << [n, options]
36
+ sections[name] = section
37
+ self
19
38
  end
20
39
 
40
+ # Makes Rails url helper method accessible from within a page instance
41
+ #
42
+ # @param [#to_s] name The name of the helper method
43
+ #
44
+ # @return [self] itself
45
+ #
21
46
  def url_helper(name)
22
47
  raise "Rails url_helpers module is not defined" unless defined?(Rails)
23
48
  delegate name, to: :"Rails.application.routes.url_helpers"
49
+ self
24
50
  end
25
51
 
26
- private
27
-
28
- def define_section_method(n, options)
29
- return n unless options[:value]
30
- define_method(n) { instance_exec(&options[:value]) }
31
- n
52
+ # The hash of definitions for the page's sections
53
+ #
54
+ # @return [Hash]
55
+ #
56
+ def sections
57
+ @sections ||= {}
32
58
  end
33
59
  end
34
60
 
35
- def to_h(options = {})
36
- data = page_methods(options).map do |(name, opts)|
37
- value = public_send(opts[:method] || name)
38
- [name, value]
39
- end
40
- Hash[data]
61
+ def to_h(except: nil, only: nil, **)
62
+ sections = self.class.sections.dup
63
+ sections.select! { |k, _| Array(only).include? k } if only
64
+ sections.reject! { |k, _| Array(except).include? k } if except
65
+ sections.map { |_, section| section.call(self) }.reduce({}, :merge!)
41
66
  end
42
67
 
43
68
  private
44
69
 
45
- def t(key)
70
+ def t(key, **options)
46
71
  raise "I18n is not defined" unless defined?(I18n)
47
- I18n.t key, scope: [Tram::Page.i18n_scope, self.class.name.underscore]
48
- end
49
-
50
- def page_methods(options)
51
- methods = self.class.instance_variable_get(:"@__sections") || []
52
- except = Array(options[:except])
53
- only = Array(options[:only])
54
- methods.reject do |(name, opts)|
55
- (except.any? && except.include?(name)) ||
56
- (only.any? && !only.include?(name)) ||
57
- __hide?(opts)
58
- end
72
+ default_scope = [Tram::Page.i18n_scope, self.class.name.underscore]
73
+ I18n.t key, scope: default_scope, **options
59
74
  end
60
75
 
61
- def __hide?(opts)
62
- black, white = opts.values_at(:unless, :if)
63
- (black && public_send(black)) || (white && !public_send(white))
64
- end
76
+ self.i18n_scope = "pages"
65
77
  end
66
-
67
- Tram::Page.i18n_scope = "pages"
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tram::Page
4
+ #
5
+ # @private
6
+ # Contains class-level definition (name and options) for a section
7
+ # with a method [#call] that extracts the section part of hash
8
+ # from an instance of [Tram::Page]
9
+ #
10
+ class Section
11
+ extend Dry::Initializer
12
+ param :name, proc(&:to_sym)
13
+ option :method, proc(&:to_s), default: -> { name }, as: :source
14
+ option :if, proc(&:to_s), optional: true, as: :positive
15
+ option :unless, proc(&:to_s), optional: true, as: :negative
16
+
17
+ # @param [Tram::Page] page
18
+ # @return [Hash] a part of the section
19
+ def call(page)
20
+ skip_on?(page) ? {} : { name => value_at(page) }
21
+ end
22
+
23
+ private
24
+
25
+ def skip_on?(page)
26
+ return true if positive && !page.public_send(positive)
27
+ return true if negative && page.public_send(negative)
28
+ end
29
+
30
+ def value_at(page)
31
+ page.public_send(source)
32
+ end
33
+ end
34
+ end
@@ -3,6 +3,6 @@
3
3
  # rubocop:disable Style/ClassAndModuleChildren
4
4
  module Tram
5
5
  class Page
6
- VERSION = "0.1.4"
6
+ VERSION = "0.1.5"
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tram-page
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Viktor Sokolov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-17 00:00:00.000000000 Z
11
+ date: 2018-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/tram/page.rb
102
+ - lib/tram/page/section.rb
102
103
  - lib/tram/page/version.rb
103
104
  - tram-page.gemspec
104
105
  homepage: https://github.com/tram-rb/tram-page
@@ -122,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
123
  version: '0'
123
124
  requirements: []
124
125
  rubyforge_project:
125
- rubygems_version: 2.6.12
126
+ rubygems_version: 2.6.13
126
127
  signing_key:
127
128
  specification_version: 4
128
129
  summary: Page Object pattern