florrick 1.0.0

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: 20ab91439eaf4ce4d05675e501a0667f48138633
4
+ data.tar.gz: 0df8702cce5af859de517f3e34a645608aa9b993
5
+ SHA512:
6
+ metadata.gz: df6f287a92029579b87b8589e140792ed7f747464666718ab2e177fa7a40b91eff1a7dc03aa5b13799a6a668394f89fef60ec8f79cf3f4489901b82adefca782
7
+ data.tar.gz: 64a594f1d4b80a821f09117b340c11ac4ce91d72c85d0014cc7193898be33e7cb57c13991f68ed62e9c43271d2d279e2c7001246870e7710e2cab3c1149aed54
@@ -0,0 +1,55 @@
1
+ module Florrick
2
+ module ActiveRecordExtension
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ def string_interpolation_value_for(var)
9
+ if self.class.florrick_fields[:strings].keys.include?(var.to_sym)
10
+ block = self.class.florrick_fields[:strings][var.to_sym]
11
+ if block
12
+ self.instance_eval(&block)
13
+ else
14
+ self.send(var)
15
+ end
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ module ClassMethods
22
+
23
+ #
24
+ # Return a hash for all florrick fields which have been defined for this model
25
+ #
26
+ def florrick_fields
27
+ @florrick_fields ||= {:strings => {}, :relationships =>{}}
28
+ end
29
+
30
+ #
31
+ # Accept a new set of configuration for this model
32
+ #
33
+ def florrick(&block)
34
+ dsl = Florrick::DSL.new(self)
35
+ dsl.instance_eval(&block)
36
+ dsl
37
+ end
38
+
39
+ #
40
+ # Return whether or not a given key can be replaced
41
+ #
42
+ def string_interpolation_for?(var)
43
+ florrick_fields[:strings].keys.include?(var.to_sym)
44
+ end
45
+
46
+ #
47
+ # Return whether or not a given relationship key can be replaced
48
+ #
49
+ def string_interpolation_relationship_for?(var)
50
+ florrick_fields[:relationships].keys.include?(var.to_sym)
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,60 @@
1
+ module Florrick
2
+ class Builder
3
+
4
+ VALID_EXPORT_TYPES = [String, Numeric, Date, Time]
5
+
6
+ attr_reader :original_string, :objects
7
+
8
+ def initialize(string, objects = {})
9
+ @original_string = string
10
+ @objects = objects
11
+ end
12
+
13
+ def output
14
+ string = @original_string.dup
15
+ string.gsub(/(\{\{([(\w+)\.]+)\}\})/) do
16
+ parts = $2.split('.')
17
+ final_string = nil
18
+ previous_object = @objects[parts.shift.to_sym]
19
+ final_string = $1 if previous_object.nil?
20
+ until final_string
21
+ # get the latest part
22
+ if var = parts.shift
23
+ # if the previous object is suitable for string interpolation, we can
24
+ # pass the var to it and get out a string or an integer.
25
+ if previous_object.respond_to?(:string_interpolation_value_for)
26
+ if previous_object.class.string_interpolation_for?(var)
27
+ # we can do this easily
28
+ previous_object = previous_object.string_interpolation_value_for(var)
29
+ # if the previous object was nil, just set to an empty string
30
+ final_string = "" if previous_object.nil?
31
+ elsif previous_object.class.string_interpolation_relationship_for?(var)
32
+ # maybe we can do this on another object
33
+ previous_object = previous_object.send(var)
34
+ else
35
+ # can't do this :(
36
+ final_string = $1
37
+ end
38
+ elsif VALID_EXPORT_TYPES.any? { |t| previous_object.is_a?(t)}
39
+ previous_object = Florrick::Formatter.convert(var, previous_object)
40
+ else
41
+ # does not respond to string_interpolation_value_for and isn't a valid object,
42
+ # we can't do anything here just return the passed value.
43
+ final_string = $1
44
+ end
45
+ else
46
+ # we're at the end now, if we can return the previous object, lets do it otherwise
47
+ # we'll just return the string as we can't do anything.
48
+ if VALID_EXPORT_TYPES.any? { |t| previous_object.is_a?(t)}
49
+ final_string = previous_object.to_s
50
+ else
51
+ final_string = $1
52
+ end
53
+ end
54
+ end
55
+ final_string
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,39 @@
1
+ # This file contains all formatters which are built-in. Some of these built-in formatters
2
+ # require ActiveSupport but seeing as that's a dependency of ActiveRecord, it won't be a problem
3
+ # for most users.
4
+ require 'active_support/core_ext/integer/inflections'
5
+ require 'active_support/inflector/transliterate'
6
+ require 'digest/sha1'
7
+ require 'digest/md5'
8
+
9
+ #
10
+ # String formatters
11
+ #
12
+ Florrick::Formatter.add('downcase', [String]) { |s| s.downcase }
13
+ Florrick::Formatter.add('upcase', [String]) { |s| s.upcase }
14
+ Florrick::Formatter.add('humanize', [String]) { |s| s.humanize }
15
+ Florrick::Formatter.add('strip', [String]) { |s| s.strip }
16
+ Florrick::Formatter.add('sha1', [String]) { |s| Digest::SHA1.hexdigest(s) }
17
+ Florrick::Formatter.add('md5', [String]) { |s| Digest::MD5.hexdigest(s) }
18
+
19
+ #
20
+ # Mathmatical formatters
21
+ #
22
+ Florrick::Formatter.add('double', [Numeric]) { |s| s * 2 }
23
+ Florrick::Formatter.add('triple', [Numeric]) { |s| s * 3 }
24
+
25
+ #
26
+ # DateTime formatters
27
+ #
28
+
29
+ Florrick::Formatter.add('long_date', [Date, Time]) { |s| s.strftime("%A #{s.day.ordinalize} %B %Y") }
30
+ Florrick::Formatter.add('long_date_without_day_name', [Date, Time]) { |s| s.strftime("#{s.day.ordinalize} %B %Y") }
31
+
32
+ Florrick::Formatter.add('short_date', [Date, Time]) { |s| s.strftime("%a %e %b %Y") }
33
+ Florrick::Formatter.add('short_date_without_day_name', [Date, Time]) { |s| s.strftime("%e %b %Y") }
34
+
35
+ Florrick::Formatter.add('ddmmyyyy', [Date, Time]) { |s| s.strftime("%d/%m/%Y") }
36
+ Florrick::Formatter.add('hhmm', [Date, Time]) { |s| s.strftime("%H:%M") }
37
+ Florrick::Formatter.add('hhmmss', [Date, Time]) { |s| s.strftime("%H:%M:%S") }
38
+ Florrick::Formatter.add('hhmm12', [Date, Time]) { |s| s.strftime("%I:%M%P") }
39
+ Florrick::Formatter.add('hhmmss12', [Date, Time]) { |s| s.strftime("%I:%M:%S%P") }
@@ -0,0 +1,20 @@
1
+ module Florrick
2
+ class DSL
3
+
4
+ def initialize(model)
5
+ @model = model
6
+ end
7
+
8
+ def string(*vars, &block)
9
+ vars.each do |var|
10
+ @model.florrick_fields[:strings][var.to_sym] = block
11
+ end
12
+ end
13
+
14
+ def relationship(*vars, &block)
15
+ vars.each do |var|
16
+ @model.florrick_fields[:relationships][var.to_sym] = block
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,40 @@
1
+ module Florrick
2
+ class Formatter
3
+
4
+ class << self
5
+ # Return all formatters available
6
+ def formatters
7
+ @formatters ||= {}
8
+ end
9
+
10
+ # Add a new global formatter
11
+ def add(name, types = [], &block)
12
+ formatters[name.to_sym] = self.new(name, types, &block)
13
+ end
14
+
15
+ # Run a string through a given formatter and return the result. If not possible, return false.
16
+ def convert(name, value)
17
+ if formatter = formatters[name.to_sym]
18
+ formatter.convert(value)
19
+ else
20
+ false
21
+ end
22
+ end
23
+ end
24
+
25
+ def initialize(name, types, &block)
26
+ @name, @types, @block = name, types, block
27
+ end
28
+
29
+ def convert(value)
30
+ if @types.empty? || @types.any? { |t| value.is_a?(t)}
31
+ @block.call(value)
32
+ else
33
+ false
34
+ end
35
+ rescue
36
+ '???'
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,12 @@
1
+ module Florrick
2
+ class Railtie < Rails::Railtie
3
+
4
+ initializer 'florrick.initialize' do
5
+ ActiveSupport.on_load(:active_record) do
6
+ require 'florrick/active_record_extension'
7
+ ActiveRecord::Base.send :include, Florrick::ActiveRecordExtension
8
+ end
9
+ end
10
+
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Florrick
2
+ VERSION = '1.0.0'
3
+ end
data/lib/florrick.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'florrick/version'
2
+ require 'florrick/builder'
3
+ require 'florrick/dsl'
4
+ require 'florrick/formatter'
5
+ require 'florrick/builtin_formatters'
6
+ require 'florrick/railtie' if defined?(Rails)
7
+
8
+ module Florrick
9
+
10
+ # A quick helper method for quickly running conversion
11
+ def self.convert(string, objects = {})
12
+ Builder.new(string, objects).output
13
+ end
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: florrick
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Rails extension for providing awesome user-initiated string interpolation
14
+ email:
15
+ - me@adamcooke.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/florrick.rb
21
+ - lib/florrick/active_record_extension.rb
22
+ - lib/florrick/builder.rb
23
+ - lib/florrick/builtin_formatters.rb
24
+ - lib/florrick/dsl.rb
25
+ - lib/florrick/formatter.rb
26
+ - lib/florrick/railtie.rb
27
+ - lib/florrick/version.rb
28
+ homepage: https://github.com/adamcooke/florrick
29
+ licenses: []
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.2.0
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: A Rails extension for providing awesome user-initiated string interpolation
51
+ test_files: []
52
+ has_rdoc: