constantinopolis 0.0.1

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: 6239ddbd0930e8bff6be617d4f307220d58e0668
4
+ data.tar.gz: 82e80460a44b89b893fe66eb4913f21c4f9c38bc
5
+ SHA512:
6
+ metadata.gz: 68c7e8d39bbdb11f77b14dfda21286c5a7eb462ab3ff945c29a37607ea16b1dc828239f5875b942030c4e3f1e28c6162f75422718afe99b274d38ed64f96c868
7
+ data.tar.gz: 140bbf8fc10a101de88fb5bde9b9eff73f1fecd332c0db306cc17a8ff7a52c3629cdbbd8d642ed99e7f8e4e5fff94641bceace6e051a98b364b0f7f62cb3b38a
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ .DS_Store
5
+ vendor/bundle
6
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in constantinopolis.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 itmammoth
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # Constantinopolis
2
+
3
+ Constantinopolis allows you to set constants from your ERBed YAML file.
4
+ Remarkably, your constants are available not only in ruby context, but in javascript's.
5
+ It works with Rails, Sinatra, or any Ruby projects.
6
+ It's inspired by [settingslogic](https://github.com/binarylogic/settingslogic).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'constantinopolis'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install constantinopolis
21
+
22
+ ## Usage
23
+
24
+ ### Setup your Fort class
25
+
26
+ You need to create a class in your application like below.
27
+
28
+ ```ruby
29
+ class Istanbul < Constantinopolis::Fort
30
+ yml "#{Rails.root}/config/istanbul.yml" # Indicate your yaml file path.
31
+ namespace Rails.env # Indicate top level namespace (If you want).
32
+ end
33
+ Istanbul.build! # Don't forget this line!
34
+ ```
35
+
36
+ If you are setting for rails application, you'd better to create this file in ```config/initializer```.
37
+
38
+ ### Define constants
39
+
40
+ Secondly, define constant YAML file.
41
+ Of course, you can use ERB expressions in it.
42
+ Top level keys are used for 'namespace' if you indicated it in your Fort class.
43
+
44
+ ```yaml
45
+ defaults: &defaults
46
+ common: Common value
47
+
48
+ development:
49
+ <<: *defaults
50
+ greeting: Hello, development!
51
+ number: 1
52
+ memorable_date: <%= Date.today %>
53
+ is:
54
+ located: Turkey
55
+
56
+ test:
57
+ <<: *defaults
58
+ greeting: Hello, test!
59
+ number: 2
60
+ memorable_date: <%= Date.today + 1 %>
61
+ is:
62
+ located: Turkey
63
+
64
+ production:
65
+ <<: *defaults
66
+ greeting: Hello, production!
67
+ number: 3
68
+ memorable_date: <%= Date.today + 2 %>
69
+ is:
70
+ located: Turkey
71
+ ```
72
+
73
+ ### Access from Ruby
74
+
75
+ Congratulation!
76
+ you've been already able to access these constants anywhere like below.
77
+
78
+ ```ruby
79
+ Istanbul.common #=> "Common value"
80
+ Istanbul.greeting #=> "Hello, development!"
81
+ Istanbul.number #=> 1
82
+ Istanbul.memorable_date #=> Wed, 23 Apr 2014
83
+ Istanbul.is.located #=> Turkey
84
+ ```
85
+
86
+ ### Access from Javascript
87
+
88
+ You can access your constants in javascript the same way as in ruby context.
89
+ Constantinopolis provides you a simple helper method to define javascript's constants.
90
+
91
+ ```html
92
+ <%= javascript_tag Istanbul.js_code %><!-- You need to call this line before using constants. -->
93
+
94
+ <h1>Javascript context</h1>
95
+ <p id="common"></p>
96
+ <p id="greeting"></p>
97
+ <p id="number"></p>
98
+ <p id="memorable_date"></p>
99
+ <p id="is-located"></p>
100
+
101
+ <%= javascript_tag do %>
102
+ document.getElementById("common").innerText = Istanbul.common;
103
+ document.getElementById("greeting").innerText = Istanbul.greeting;
104
+ document.getElementById("number").innerText = Istanbul.number;
105
+ document.getElementById("memorable_date").innerText = Istanbul.memorable_date;
106
+ document.getElementById("is-located").innerText = Istanbul.is.located;
107
+ <% end %>
108
+ ```
109
+
110
+ It's can be a nice to define ```<%= javascript_tag Istanbul.js_code %>``` in ```layouts/application.html.erb``` if you are developing a rails application.
111
+
112
+ ## Contributing
113
+
114
+ 1. Fork it ( http://github.com/itmammoth/constantinopolis/fork )
115
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
116
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
117
+ 4. Push to the branch (`git push origin my-new-feature`)
118
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'constantinopolis/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "constantinopolis"
8
+ spec.version = Constantinopolis::VERSION
9
+ spec.authors = ["itmammoth"]
10
+ spec.email = ["itmammoth@gmail.com"]
11
+ spec.summary = %q{Setting constants solution for ruby applications.}
12
+ spec.description = %q{Constantinopolis allows you to set constants from your ERBed YAML file. Remarkably, your constants are available not only in ruby context, but in javascript's. It works with Rails, Sinatra, or any Ruby projects.}
13
+ spec.homepage = "https://github.com/itmammoth/constantinopolis"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "rspec", "~> 2.14"
22
+ spec.add_development_dependency "pry-byebug", "~> 1.3"
23
+ spec.add_development_dependency "capybara", "~> 2.2"
24
+ spec.add_development_dependency "launchy", "~> 2.4"
25
+ spec.add_development_dependency "poltergeist", "~> 1.5"
26
+ spec.add_development_dependency "sinatra", "~> 1.4"
27
+ end
@@ -0,0 +1,3 @@
1
+ module Constantinopolis
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require "constantinopolis/version"
2
+ require "yaml"
3
+ require "erb"
4
+ require "json"
5
+
6
+ module Constantinopolis
7
+
8
+ class Fort
9
+ private_class_method :new
10
+
11
+ class << self
12
+
13
+ def yml(path = nil)
14
+ @yml ||= path
15
+ end
16
+
17
+ def namespace(namespace = nil)
18
+ @namespace ||= namespace
19
+ end
20
+
21
+ def build!
22
+ instance.build_methods!
23
+ instance.build_js!
24
+ end
25
+
26
+ def js_code
27
+ instance.js_code
28
+ end
29
+
30
+ private
31
+
32
+ def instance
33
+ return @instance if @instance
34
+ @instance = new
35
+ end
36
+ end
37
+
38
+ def build_methods!
39
+ @constants.each do |key, value|
40
+ self.class.module_eval do
41
+ sig = class << self; self; end
42
+ sig.send :define_method, key, ->() { value }
43
+ end
44
+ end
45
+ end
46
+
47
+ def build_js!
48
+ @js_code = "#{self.class.name}=#{JSON.generate(@constants)};"
49
+ end
50
+
51
+ def js_code
52
+ @js_code
53
+ end
54
+
55
+ private
56
+
57
+ def initialize
58
+ raise "Must locate yaml file!" unless self.class.yml
59
+ file = open(self.class.yml).read
60
+ hash = set_accessor(YAML.load(ERB.new(file).result))
61
+ @constants = self.class.namespace ? hash[self.class.namespace.to_s] : hash
62
+ end
63
+
64
+ def set_accessor(hash)
65
+ hash.each do |key, value|
66
+ if value.is_a? Hash
67
+ value.extend AccessableHash
68
+ set_accessor value
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ module AccessableHash
75
+ def method_missing(name, *args, &block)
76
+ key = name.to_s
77
+ self.has_key?(key) ? self[key] : super
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ require 'capybara'
4
+ require 'capybara/rspec'
5
+ require 'capybara/poltergeist'
6
+ require 'sinatra'
7
+
8
+ Capybara.default_driver = :poltergeist
9
+ Capybara.javascript_driver = :poltergeist
10
+ Capybara.save_and_open_page_path = File.dirname(__FILE__) + '/../tmp'
11
+
12
+ class SettingRack < Constantinopolis::Fort
13
+ yml File.expand_path('../setting_rack.yml', __FILE__)
14
+ end
15
+ SettingRack.build!
16
+
17
+ get '/' do
18
+ file = open("spec/js/test.html.erb").read
19
+ erb file
20
+ end
21
+
22
+ describe 'the dummy app', js: true do
23
+ include Capybara::DSL
24
+
25
+ before do
26
+ Capybara.app = Sinatra::Application.new
27
+ end
28
+
29
+ it "renders javascript objects" do
30
+ visit "/"
31
+ page.should have_content "Fort of javascript"
32
+ page.should have_content "Hello, world!"
33
+ page.should have_content "20"
34
+ page.should have_content "2014-04-23"
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ greeting: Hello, world!
2
+ number: 20
3
+ deadline:
4
+ projects:
5
+ a: <%= Date.new(2014, 4, 23) %>
@@ -0,0 +1,17 @@
1
+ <html>
2
+ <head>
3
+ <title>spec</title>
4
+ <script type="text/javascript"><%= SettingRack.js_code %></script>
5
+ </head>
6
+ <body>
7
+ <h1 id="page-title">Fort of javascript</h1>
8
+ <p id="greeting"></p>
9
+ <p id="number"></p>
10
+ <p id="deadline-projects-a"></p>
11
+ <script type="text/javascript">
12
+ document.getElementById("greeting").innerText = SettingRack.greeting;
13
+ document.getElementById("number").innerText = SettingRack.number;
14
+ document.getElementById("deadline-projects-a").innerText = SettingRack.deadline.projects.a;
15
+ </script>
16
+ </body>
17
+ </html>
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ describe Constantinopolis::Fort do
4
+
5
+ class NoYaml < Constantinopolis::Fort
6
+ end
7
+
8
+ class WithYaml < Constantinopolis::Fort
9
+ yml File.expand_path('../setting.yml', __FILE__)
10
+ end
11
+ WithYaml.build!
12
+
13
+ class WithNamespace < Constantinopolis::Fort
14
+ yml File.expand_path('../setting_with_namespace.yml', __FILE__)
15
+ namespace :test
16
+ end
17
+ WithNamespace.build!
18
+
19
+ describe "::build!" do
20
+ context "when not located yaml file" do
21
+ it "raises error" do
22
+ expect { NoYaml.build! }.to raise_error(RuntimeError, "Must locate yaml file!")
23
+ end
24
+ end
25
+ end
26
+
27
+ describe "any keys" do
28
+ context "when located yaml file" do
29
+ it { expect(WithYaml.hello).to eq "Hello, CONSTANTINOPOLIS!" }
30
+ it { expect(WithYaml.number).to eq 10 }
31
+ it { expect(WithYaml.go.to.hospital).to eq "Go to hospital." }
32
+ end
33
+
34
+ context "when indicated namaespace" do
35
+ it { expect(WithNamespace.common).to eq "Common value" }
36
+ it { expect(WithNamespace.hello).to eq "Hello, test!" }
37
+ end
38
+ end
39
+
40
+ describe "::js_code" do
41
+ subject(:js_code) { WithYaml.js_code }
42
+ it { expect(js_code).to match /WithYaml={.*};/m }
43
+ it { expect(js_code).to match /"hello":"Hello, CONSTANTINOPOLIS!"/m }
44
+ it { expect(js_code).to match /"number":10/m }
45
+ it { expect(js_code).to match /"go":{"to":{"hospital":"Go to hospital."}}/m }
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ hello: Hello, <%= "constantinopolis!".upcase %>
2
+ number: 10
3
+ go:
4
+ to:
5
+ hospital: "Go to hospital."
@@ -0,0 +1,10 @@
1
+ defaults: &defaults
2
+ common: Common value
3
+
4
+ development:
5
+ <<: *defaults
6
+ hello: "Hello, development!"
7
+
8
+ test:
9
+ <<: *defaults
10
+ hello: "Hello, test!"
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'constantinopolis'
3
+ require 'pry-byebug'
4
+ require 'date'
metadata ADDED
@@ -0,0 +1,152 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: constantinopolis
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - itmammoth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: capybara
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: launchy
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: poltergeist
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sinatra
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.4'
97
+ description: Constantinopolis allows you to set constants from your ERBed YAML file.
98
+ Remarkably, your constants are available not only in ruby context, but in javascript's.
99
+ It works with Rails, Sinatra, or any Ruby projects.
100
+ email:
101
+ - itmammoth@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - constantinopolis.gemspec
112
+ - lib/constantinopolis.rb
113
+ - lib/constantinopolis/version.rb
114
+ - spec/js/constantinopolis_js_spec.rb
115
+ - spec/js/setting_rack.yml
116
+ - spec/js/test.html.erb
117
+ - spec/lib/constantinopolis_spec.rb
118
+ - spec/lib/setting.yml
119
+ - spec/lib/setting_with_namespace.yml
120
+ - spec/spec_helper.rb
121
+ homepage: https://github.com/itmammoth/constantinopolis
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.2.2
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Setting constants solution for ruby applications.
145
+ test_files:
146
+ - spec/js/constantinopolis_js_spec.rb
147
+ - spec/js/setting_rack.yml
148
+ - spec/js/test.html.erb
149
+ - spec/lib/constantinopolis_spec.rb
150
+ - spec/lib/setting.yml
151
+ - spec/lib/setting_with_namespace.yml
152
+ - spec/spec_helper.rb