Class: Florrick::Builder
- Inherits:
-
Object
- Object
- Florrick::Builder
- Defined in:
- lib/florrick/builder.rb
Constant Summary collapse
- VALID_EXPORT_TYPES =
[String, Numeric, Date, Time]
Instance Attribute Summary collapse
-
#objects ⇒ Object
readonly
Returns the value of attribute objects.
-
#original_string ⇒ Object
readonly
Returns the value of attribute original_string.
Instance Method Summary collapse
-
#initialize(string, objects = {}) ⇒ Builder
constructor
A new instance of Builder.
- #output ⇒ Object
Constructor Details
#initialize(string, objects = {}) ⇒ Builder
Returns a new instance of Builder.
8 9 10 11 |
# File 'lib/florrick/builder.rb', line 8 def initialize(string, objects = {}) @original_string = string @objects = objects end |
Instance Attribute Details
#objects ⇒ Object (readonly)
Returns the value of attribute objects.
6 7 8 |
# File 'lib/florrick/builder.rb', line 6 def objects @objects end |
#original_string ⇒ Object (readonly)
Returns the value of attribute original_string.
6 7 8 |
# File 'lib/florrick/builder.rb', line 6 def original_string @original_string end |
Instance Method Details
#output ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/florrick/builder.rb', line 13 def output string = @original_string.dup string.gsub(/(\{\{([(\w+)\.]+)(?>\ ?\|\ ?([\w\-\+\ \!\?\[\]\(\)]+))?\}\})/) do original_string = $1 fallback_string = $3 parts = $2.split('.') final_string = nil previous_object = @objects[parts.shift.to_sym] if previous_object.nil? final_string = fallback_string || original_string end until final_string # get the latest part if var = parts.shift # if the previous object is suitable for string interpolation, we can # pass the var to it and get out a string or an integer. if previous_object.respond_to?(:string_interpolation_value_for) if previous_object.class.string_interpolation_for?(var) # we can do this easily previous_object = previous_object.string_interpolation_value_for(var) # if the previous object was nil, just set to an empty string final_string = "" if previous_object.nil? elsif previous_object.class.string_interpolation_relationship_for?(var) # maybe we can do this on another object previous_object = previous_object.send(var) else # can't do this :( final_string = fallback_string || original_string end elsif VALID_EXPORT_TYPES.any? { |t| previous_object.is_a?(t)} previous_object = Florrick::Formatter.convert(var, previous_object) else # does not respond to string_interpolation_value_for and isn't a valid object, # we can't do anything here just return the passed value. final_string = fallback_string || original_string end else # we're at the end now, if we can return the previous object, lets do it otherwise # we'll just return the string as we can't do anything. if VALID_EXPORT_TYPES.any? { |t| previous_object.is_a?(t)} final_string = previous_object.to_s else final_string = fallback_string || original_string end end end final_string end end |