plant 0.4.0 → 0.5.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: b085b89c69b0137fd5b74f50203e21c2612a9ed4
4
- data.tar.gz: 1c3ce450bc05c8fdc5ce09e6f4fc85d258915592
3
+ metadata.gz: d767dab70ab52f596d2604b3e3b1a9ff2ed1d831
4
+ data.tar.gz: 17aa9c9bded036c02a9431f6196163d150137efc
5
5
  SHA512:
6
- metadata.gz: 7dae13d4a453ba0527b578fbcef132a7057a50bec1c852e66d060b1372cb6f1d98d5ecd8b4979fe1728fa49af9d4ccc6b65304079c3cc51b084d0ebd57ab143f
7
- data.tar.gz: e5435b6e7fc84c40dfbc2bc45f2c594f6b4cb117c638ae45ca3ef099dd7f7bac86da959eca712742e16d98c425fb4ed4c0ff75d3363e8ee06d55f710dc9892b3
6
+ metadata.gz: 1471762fe46fa15424003009d6845145b87a8a76321a3c21003a0e9dcf12508cb2fec685b4808aaeafb3cfa5682060d4409038b5fe1053639c25594b347491f0
7
+ data.tar.gz: c3da7fe7ba433d531ccc5b8deeeb402c29343c2a3028295d71ae22480355f92e5ec96b487115c220c6b05f3c4117c47267ac8df04de34422e87663257414836a
@@ -1,30 +1,70 @@
1
1
  module Plant
2
2
  # Model for content
3
3
  class Content < ActiveRecord::Base
4
- # Gets the content for a given node
4
+ # Can only have one scenario-node_id combination
5
+ validates_uniqueness_of :node_id
6
+
7
+ # Gets a given node
5
8
  # => Parameter: node ID (string)
6
- # => Parameter: scenario (optional)
7
- # Can either by a string or an object with
8
- # a scenario method, for example a user.
9
+ # => Parameter: Options (hash), containing
10
+ # - scenario (optional)
11
+ # Can either be a string or an object with
12
+ # a scenario method, for example a user.
13
+ #
14
+ # If no specific scenario returned, then will
15
+ # default back to a nil scenario.
16
+ #
17
+ # => Throws: NotFound if node not found
18
+ # => Returns: A content object
19
+ def self.get_node(node_id, options = {})
20
+ options = defaults.merge(options)
21
+ find_specific(node_id, options[:scenario]) || find_main(node_id)
22
+ end
23
+
24
+ def self.defaults
25
+ { scenario: nil,
26
+ parameters: {} }
27
+ end
28
+
29
+ # Convenience method to get the content of a given node
30
+ # => Paramter: node ID (string)
31
+ # => Paramter: Options (hash)
32
+ # - scenario
33
+ # Can either be a string or an object with
34
+ # a scenario method, for example a user
9
35
  #
10
- # If no specific scenario returned, then will
11
- # default back to a nil scenario.
12
- # Throws: NotFound if node not found
13
- # => Returns: content for this node
14
- def self.get_node_content(node, scenario = nil)
36
+ # - Also can be any arbitary key to be injected into the
37
+ # returned content. For example
38
+ # When called get_node_content(node_id, foo: 'hhh)
39
+ # "This is content for #{foo} and bar"
40
+ # will return 'This is content for hhh and bar'
41
+ #
42
+ # Note: If second parameter is a string, it will be treated as
43
+ # scenario.
44
+ # Also see: get_node
45
+ def self.get_node_content(node_id, options = {})
46
+ options = { scenario: options } unless options.is_a? Hash
47
+ options = defaults.merge(options)
48
+ inject_content(get_node(node_id, options).content, options)
49
+ end
50
+
51
+ # Interpolation of content
52
+ def self.inject_content(content, options)
53
+ options.each do |key, replacement|
54
+ content.gsub!("\#\{#{key}\}", replacement.to_s)
55
+ end
56
+ content
57
+ end
58
+
59
+ def self.find_specific(node, scenario)
15
60
  scenario = scenario.scenario if scenario.respond_to? :scenario
16
- specific = where(node_id: "#{node}.#{scenario}").first
17
- return specific.content if specific
18
- find_main(node)
61
+ find_by(node_id: "#{node}.#{scenario}")
19
62
  end
20
63
 
21
64
  def self.find_main(node)
22
65
  main = find_by(node_id: "#{node}.main")
23
- return main.content if main
24
- find_by!(node_id: "#{node}").content
66
+ return main if main
67
+ find_by!(node_id: "#{node}")
25
68
  end
26
-
27
- # Can only have one scenario-node_id combination
28
- validates_uniqueness_of :node_id
29
69
  end
30
70
  end
data/lib/plant/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # Version file
2
2
  module Plant
3
- VERSION = '0.4.0'
3
+ VERSION = '0.5.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yule
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-21 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails