Class: Structr::FieldDefinition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, regexp, &block) ⇒ FieldDefinition

Returns a new instance of FieldDefinition.



84
85
86
# File 'lib/structr.rb', line 84

def initialize(name, regexp, &block)
  @name, @regexp, @block = name.to_s, regexp, block
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



82
83
84
# File 'lib/structr.rb', line 82

def block
  @block
end

#nameObject

Returns the value of attribute name.



82
83
84
# File 'lib/structr.rb', line 82

def name
  @name
end

Instance Method Details

#getterObject



92
93
94
# File 'lib/structr.rb', line 92

def getter
  :"#{name}"
end

#ivarObject



96
97
98
# File 'lib/structr.rb', line 96

def ivar
  :"@#{name}"
end

#match(string) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/structr.rb', line 100

def match(string)
  string.scan(@regexp).map do |matched|
    matched = matched.first  if matched.size == 1
    if @block
      @block.call(*matched)
    else
      matched
    end
  end
end

#setterObject



88
89
90
# File 'lib/structr.rb', line 88

def setter
  :"#{name}="
end