Module: RRTF::SectionFormatting
- Included in:
- SectionStyle
- Defined in:
- lib/rrtf/formatting.rb
Overview
Section formatting attributes and methods.
Constant Summary
- SECTION_ATTRIBUTES =
Formatting attributes that can be applied to an RTF document section.
{ "columns" => { "default" => nil, "to_rtf" => lambda{ |value| "\\cols#{value}" unless value.nil? } }, "column_spacing" => { "default" => nil, "from_user" => lambda{ |value| RRTF::Utilities.value2twips(value) }, "to_rtf" => lambda{ |value| "\\colsx#{value}" unless value.nil? } }, "mirror_margins" => { "default" => nil, "to_rtf" => lambda{ |value| "\\margmirrorsxn" if value } } }.freeze
Class Method Summary collapse
-
.included(base) ⇒ Object
Generates attribute accessors for all section attributes when the module is included in another module or class.
Instance Method Summary collapse
-
#initialize_section_formatting(options = {}) ⇒ Object
Initializes section formatting attributes.
-
#section_formatting_to_rtf ⇒ String
Generates an RTF string representing all applied section formatting.
-
#set_section_formatting_from_hashmap(hash) ⇒ Object
Sets section formatting attributes according to the supplied hashmap.
Class Method Details
.included(base) ⇒ Object
Generates attribute accessors for all section attributes when the module is included in another module or class.
928 929 930 931 932 933 934 935 |
# File 'lib/rrtf/formatting.rb', line 928 def self.included(base) # define accessors in base for document attributes base.class_eval do SECTION_ATTRIBUTES.each do |key, | attr_accessor :#{key}" end # each end # class_eval end |
Instance Method Details
#initialize_section_formatting(options = {}) ⇒ Object
Initializes section formatting attributes.
943 944 945 946 947 948 949 950 |
# File 'lib/rrtf/formatting.rb', line 943 def initialize_section_formatting( = {}) # load default attribute values SECTION_ATTRIBUTES.each do |key, | send("#{key}=", ["default"]) end # each # overwrite default attribute values with given values set_section_formatting_from_hashmap() end |
#section_formatting_to_rtf ⇒ String
Generates an RTF string representing all applied section formatting.
972 973 974 975 976 977 978 979 980 981 982 983 984 |
# File 'lib/rrtf/formatting.rb', line 972 def section_formatting_to_rtf text = StringIO.new # accumulate RTF representations of section attributes SECTION_ATTRIBUTES.each do |key, | if .has_key?("to_rtf") rtf = ["to_rtf"].call(send(key)) text << rtf unless rtf.nil? end # if end # each text.string end |
#set_section_formatting_from_hashmap(hash) ⇒ Object
Sets section formatting attributes according to the supplied hashmap.
954 955 956 957 958 959 960 961 962 963 964 965 966 967 |
# File 'lib/rrtf/formatting.rb', line 954 def set_section_formatting_from_hashmap(hash) hash.each do |attribute, value| # skip unreconized attributes next unless(SECTION_ATTRIBUTES.keys.include?(attribute)) # preprocess value if nessesary if SECTION_ATTRIBUTES[attribute].has_key?("from_user") value = SECTION_ATTRIBUTES[attribute]["from_user"].call(value) elsif SECTION_ATTRIBUTES[attribute].has_key?("dictionary") && value.is_a?(String) value = SECTION_ATTRIBUTES[attribute]["dictionary"][value] end # if # set attribute value send("#{attribute}=", value) end # each end |