Module: Artworker::Dimensions

Defined in:
lib/artworker/dimensions.rb

Class Method Summary collapse

Class Method Details

.format_individual_dimensions(dimensions) ⇒ Object



12
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
# File 'lib/artworker/dimensions.rb', line 12

def self.format_individual_dimensions(dimensions)
  dimensions.each do |dimension_collection|
    dimension_collection[1].each do |dimension|

      define_method "#{dimension}" do
        Artworker::Dimensions.read_dimension(read_attribute(dimension),fractions?) unless read_attribute(dimension).nil?
      end

      define_method "#{dimension}_in_centimeters" do
        if read_attribute(dimension) && read_attribute(dimension) > 0
          metric? ? eval("self.#{dimension.to_s}") : convert_to_centimeters(eval("self.#{dimension.to_s}"))
        end
      end

      define_method "#{dimension}_in_inches" do
        if read_attribute(dimension) && read_attribute(dimension) > 0
          metric? ? Artworker::Fractions.round_to_nearest_fraction(convert_to_inches(eval("self.#{dimension.to_s}")).to_s, "1/16") : eval("self.#{dimension.to_s}")
        end
      end

      define_method "#{dimension}=" do |d|
        new_depth = Artworker::Dimensions.write_dimension(d)
        write_attribute(dimension, new_depth)
      end
      
    end
  end
end

.format_total_dimensions(dimensions) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/artworker/dimensions.rb', line 41

def self.format_total_dimensions(dimensions)
  dimensions.each do |dimension_type|
    
    define_method "#{dimension_type[0]}" do
      dim_array = []
      dimension_type[1].each do |dimension_entry|
        dimension_entry = eval "self.#{dimension_entry}"
        dim = dimension_entry unless dimension_entry.blank?
        dim_array << dim
      end
      if metric?
        suffix = " centimeters"
      else
        suffix = " inches"
      end
      total_dimensions = dim_array.compact.join(' x ') + suffix unless dim_array.compact.join(' x ') == ''
    end
    
    define_method "#{dimension_type[0]}_in_inches" do
      dim_array = []
      dimension_type[1].each do |dimension_entry|
        dimension_entry = eval "self.#{dimension_entry}_in_inches"
        dim = dimension_entry unless dimension_entry.blank?
        dim_array << dim
      end
      suffix = " inches"
      total_dimensions = dim_array.compact.join(' x ') + suffix unless dim_array.compact.join(' x ') == ''
    end

    define_method "#{dimension_type[0]}_in_centimeters" do
      dim_array = []
      dimension_type[1].each do |dimension_entry|
        dimension_entry = eval "self.#{dimension_entry}_in_centimeters"
        dim = dimension_entry unless dimension_entry.blank?
        dim_array << dim
      end
      suffix = " centimeters"
      total_dimensions = dim_array.compact.join(' x ') + suffix unless dim_array.compact.join(' x ') == ''
    end
    
  end
end

.read_dimension(dimension, use_fractions) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/artworker/dimensions.rb', line 84

def self.read_dimension(dimension,use_fractions)
  if dimension > 0
    returned_dimension = dimension && (use_fractions ? Artworker::Fractions.round_to_nearest_fraction(dimension.to_s, "1/16") : dimension.to_s)
  else
    returned_dimension = ""
  end
  returned_dimension = returned_dimension.to_i unless returned_dimension != returned_dimension.to_i
  return returned_dimension
end

.set_dimensions_functions(dimensions, use_fractions, use_metric) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/artworker/dimensions.rb', line 4

def self.set_dimensions_functions(dimensions, use_fractions, use_metric)
  @@use_fractions = "self.#{use_fractions}"
  @@use_metric = "self.#{use_metric}"

  format_total_dimensions(dimensions)
  format_individual_dimensions(dimensions)
end

.write_dimension(dimension) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/artworker/dimensions.rb', line 94

def self.write_dimension(dimension)
  if dimension.blank?
    dimension = 0
  end
  new_dimension = Artworker::Fractions.to_f(dimension)
  return new_dimension
end