Class: Clevic::Sampler
Overview
This is used as part of the process of calculating the width of a field in the UI. Since the font is important, this computes a string value for a field that can be given to the font metrics for a framework. Uses various heuristics to compute the string values for different kinds of fields.
Defined Under Namespace
Classes: VirtualField
Instance Attribute Summary collapse
-
#field ⇒ Object
readonly
Returns the value of attribute field.
Instance Method Summary collapse
-
#compute ⇒ Object
return a string which is representative of the width of the field.
- #date_time_sample ⇒ Object
- #display ⇒ Object
- #do_format(value) ⇒ Object
- #entity_class ⇒ Object
- #field_name ⇒ Object
-
#initialize(field, &format_block) ⇒ Sampler
constructor
display is only used for relational fields.
- #meta ⇒ Object
- #numeric_sample ⇒ Object
- #related_sample ⇒ Object
- #sample_date_time ⇒ Object
-
#string_sample ⇒ Object
default to max length of 20.
Constructor Details
#initialize(field, &format_block) ⇒ Sampler
display is only used for relational fields
13 14 15 16 |
# File 'lib/clevic/sampler.rb', line 13 def initialize( field, &format_block ) @field = field @format_block = format_block end |
Instance Attribute Details
#field ⇒ Object (readonly)
Returns the value of attribute field.
17 18 19 |
# File 'lib/clevic/sampler.rb', line 17 def field @field end |
Instance Method Details
#compute ⇒ Object
return a string which is representative of the width of the field
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 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/clevic/sampler.rb', line 36 def compute if field.set # choose the longest value in the set set = field.set_for( entity_class.first ) if set.is_a?( Hash ) set.values else set end. \ max{|a,b| a.to_s.length <=> b.to_s.length }.to_s.upcase else # choose samples based on the type of the field case .type when :boolean field.label when :string, :text string_sample when :date, :time, :datetime, :timestamp date_time_sample when :numeric, :decimal, :integer, :float numeric_sample # TODO return a width, or something like that when :boolean; 'W' when :many_to_one else unless .type.nil? raise "Sampler#compute can't figure out sample for #{entity_class.name}##{field_name} because it's a #{.type.inspect}" end end end end |
#date_time_sample ⇒ Object
94 95 96 97 98 |
# File 'lib/clevic/sampler.rb', line 94 def date_time_sample # replace all letters with 'N' # and numbers with 8 do_format( sample_date_time || Date.today ).andand.gsub( /[[:alpha:]]/, 'N' ).gsub( /\d/, '8' ) end |
#display ⇒ Object
27 28 29 |
# File 'lib/clevic/sampler.rb', line 27 def display field.display end |
#do_format(value) ⇒ Object
75 76 77 |
# File 'lib/clevic/sampler.rb', line 75 def do_format( value ) @format_block.call( value ) end |
#entity_class ⇒ Object
19 20 21 |
# File 'lib/clevic/sampler.rb', line 19 def entity_class field.entity_class end |
#field_name ⇒ Object
23 24 25 |
# File 'lib/clevic/sampler.rb', line 23 def field_name field.attribute end |
#meta ⇒ Object
31 32 33 |
# File 'lib/clevic/sampler.rb', line 31 def field. end |
#numeric_sample ⇒ Object
100 101 102 103 104 105 |
# File 'lib/clevic/sampler.rb', line 100 def numeric_sample max = entity_class.max( field_name ) min = entity_class.min( field_name ) max_length = [ do_format( min ).to_s, do_format( max ).to_s ].map( &:length ).max '9' * ( max_length || 5 ) end |
#related_sample ⇒ Object
120 121 122 123 124 |
# File 'lib/clevic/sampler.rb', line 120 def if display.respond_to?( :to_sym ) Sampler.new( VirtualField.new( eval( .class_name ), display.to_sym ), &@format_block ).compute end end |
#sample_date_time ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/clevic/sampler.rb', line 84 def sample_date_time ds = entity_class \ .filter( ~{ field_name => nil } ) \ .select( field_name ) \ .limit(1) # can't use single-value here because the typecast_on_load # isn't called unless we access the value via the entity object ds.first.send( field_name ) end |
#string_sample ⇒ Object
default to max length of 20
80 81 82 |
# File 'lib/clevic/sampler.rb', line 80 def string_sample 'N' * ( entity_class.max( :length.sql_function( field_name ) ).andand.to_i || 20 ) end |