Class: Fit::TypeAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/fit/type_adapter.rb

Direct Known Subclasses

GenericAdapter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, name = '', is_output = false) ⇒ TypeAdapter

Returns a new instance of TypeAdapter.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fit/type_adapter.rb', line 12

def initialize target, name = '', is_output = false
  @is_output = is_output
  @target = @fixture = target
  
  @name=name
  ends_in_qm = is_output && name=~/\?$/
  if @name.index(/[A-Z]/)
    @name = @name.split(/([A-Z][^A-Z]+)/).delete_if {|e| e.empty?}.collect {|e| e.downcase}.join('_')
  end
  @name = @name.gsub(/[^0-9a-zA-Z_]+$/,'').gsub(/[^0-9a-zA-Z_]+/,'_').gsub(/_+/,'_')
  @name << '?' if ends_in_qm
end

Instance Attribute Details

#fixtureObject

Returns the value of attribute fixture.



10
11
12
# File 'lib/fit/type_adapter.rb', line 10

def fixture
  @fixture
end

#targetObject

Returns the value of attribute target.



10
11
12
# File 'lib/fit/type_adapter.rb', line 10

def target
  @target
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/fit/type_adapter.rb', line 10

def type
  @type
end

Class Method Details

.for(fixture, name, is_output = (name=~/(\(\))|(\?)$/)) ⇒ Object

Factories



27
28
29
# File 'lib/fit/type_adapter.rb', line 27

def TypeAdapter.for fixture, name, is_output=(name=~/(\(\))|(\?)$/)
  GenericAdapter.new fixture, name, is_output
end

.on(fixture, type) ⇒ Object



31
32
33
34
35
# File 'lib/fit/type_adapter.rb', line 31

def TypeAdapter.on fixture, type
  adapter = GenericAdapter.new fixture
  adapter.type = type
  adapter
end

Instance Method Details

#equals(a, b) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/fit/type_adapter.rb', line 53

def equals a, b
  if ((a.kind_of? Float) and (b.kind_of? Numeric)) or ((a.kind_of? Numeric) and (b.kind_of? Float))
    (a - b).abs < 1.0e-5 # use a delta to test equality between a float and a number
  else
    a == b
  end
end

#getObject



37
38
39
40
41
42
# File 'lib/fit/type_adapter.rb', line 37

def get
  return nil if @name.nil?
  getter = @name
  getter.chop! if getter =~ /\?$/ && ! @target.respond_to?(getter.to_sym)
  @target.send(getter)
end

#is_output?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/fit/type_adapter.rb', line 49

def is_output?
  @is_output
end

#set(value) ⇒ Object



44
45
46
47
# File 'lib/fit/type_adapter.rb', line 44

def set value
  raise 'Output fields cannot be set' if @is_output
  @target.send("#{@name}=", value)
end

#to_s(target) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/fit/type_adapter.rb', line 61

def to_s target
  case target
  when Array
    target.join(", ")
  else
    target.to_s
  end
end