Class: Specifind::MethodBuilder
- Inherits:
-
Object
- Object
- Specifind::MethodBuilder
- Defined in:
- lib/specifind/method_builder.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.matchers ⇒ Object
readonly
Returns the value of attribute matchers.
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .match(model, name) ⇒ Object
- .parse_to_attributes(s) ⇒ Object
- .pattern ⇒ Object
- .prefix ⇒ Object
- .suffix ⇒ Object
Instance Method Summary collapse
- #body ⇒ Object
- #define ⇒ Object
-
#initialize(model, name) ⇒ MethodBuilder
constructor
A new instance of MethodBuilder.
- #merge_attribute_types(columns) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(model, name) ⇒ MethodBuilder
Returns a new instance of MethodBuilder.
40 41 42 43 44 45 46 |
# File 'lib/specifind/method_builder.rb', line 40 def initialize(model, name) @model = model @name = name.to_s match = @name.match(self.class.pattern) @attributes = MethodBuilder.parse_to_attributes match[1] @attribute_names = @attributes.map{ |a| a.name } end |
Class Attribute Details
.matchers ⇒ Object (readonly)
Returns the value of attribute matchers.
8 9 10 |
# File 'lib/specifind/method_builder.rb', line 8 def matchers @matchers end |
Instance Attribute Details
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
38 39 40 |
# File 'lib/specifind/method_builder.rb', line 38 def attribute_names @attribute_names end |
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
38 39 40 |
# File 'lib/specifind/method_builder.rb', line 38 def attributes @attributes end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
38 39 40 |
# File 'lib/specifind/method_builder.rb', line 38 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
38 39 40 |
# File 'lib/specifind/method_builder.rb', line 38 def name @name end |
Class Method Details
.match(model, name) ⇒ Object
10 11 12 13 |
# File 'lib/specifind/method_builder.rb', line 10 def match(model, name) klass = matchers.find { |k| name =~ k.pattern } klass.new(model, name) if klass end |
.parse_to_attributes(s) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/specifind/method_builder.rb', line 15 def parse_to_attributes(s) operator_regexp = Regexp.new Operator.patterns.map{|s| s = "(#{s})"}.join '|' hybrid_list = s.split(operator_regexp) attribute_list = [] # it is now split into [field_name (+comparator)] + operator + [field_name (+comparator)] ... attribute_list << AttributePhrase.from_string_and_operator(:string => hybrid_list.shift, :operator => hybrid_list.shift) while hybrid_list.length > 0 attribute_list end |
.pattern ⇒ Object
25 26 27 |
# File 'lib/specifind/method_builder.rb', line 25 def pattern /^#{prefix}_([_a-zA-Z]\w*)#{suffix}$/ end |
.prefix ⇒ Object
29 30 31 |
# File 'lib/specifind/method_builder.rb', line 29 def prefix raise NotImplementedError end |
.suffix ⇒ Object
33 34 35 |
# File 'lib/specifind/method_builder.rb', line 33 def suffix '' end |
Instance Method Details
#body ⇒ Object
80 81 82 |
# File 'lib/specifind/method_builder.rb', line 80 def body raise NotImplementedError end |
#define ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/specifind/method_builder.rb', line 62 def define assertions = "" @attributes.each do |a| assertions += a.to_assertion end rearrangements = "" @attributes.each do |a| rearrangements += a.to_rearrangement end model.class_eval <<-CODE, __FILE__, __LINE__ + 1 def self.#{name}(#{signature}) #{assertions} #{rearrangements} #{body} end CODE end |
#merge_attribute_types(columns) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/specifind/method_builder.rb', line 48 def merge_attribute_types(columns) @attributes.each do |a| columns.each do |c| if a.name == c[:name] a.type = c[:type] end end end end |
#valid? ⇒ Boolean
58 59 60 |
# File 'lib/specifind/method_builder.rb', line 58 def valid? attribute_names.all? { |name| model.columns_hash[name] || model.reflect_on_aggregation(name.to_sym) } end |