Class: OfflineLookup::Builder
- Inherits:
-
Object
- Object
- OfflineLookup::Builder
- Defined in:
- lib/offline_lookup/builder.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#add_identity_method(key, value) ⇒ Object
Return true iff instance is of named type (e.g. FooType.first.bar?).
-
#add_key_for_name_method ⇒ Object
e.g.
-
#add_key_lookup_method(key, value) ⇒ Object
Get key value (e.g. FooType.bar_id).
- #add_lookup(key, value) ⇒ Object
-
#add_lookup_method(key, value) ⇒ Object
Get instance by named method (e.g. FooType.bar) (Not offline, but syntactic sugar).
-
#add_name_for_key_method ⇒ Object
e.g.
-
#add_static_lookup_method ⇒ Object
e.g.
- #build ⇒ Object
- #create_module ⇒ Object
- #get_module ⇒ Object
-
#initialize(model, options) ⇒ Builder
constructor
A new instance of Builder.
- #sanitize(string) ⇒ Object
Constructor Details
#initialize(model, options) ⇒ Builder
Returns a new instance of Builder.
4 5 6 7 8 9 10 11 12 |
# File 'lib/offline_lookup/builder.rb', line 4 def initialize(model, ) @model = model @fields = [:fields] @key = [:key] @name = [:name] @build_identity_methods = [:identity_methods] @build_lookup_methods = [:lookup_methods] @modyule = get_module || create_module end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
3 4 5 |
# File 'lib/offline_lookup/builder.rb', line 3 def fields @fields end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
3 4 5 |
# File 'lib/offline_lookup/builder.rb', line 3 def key @key end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/offline_lookup/builder.rb', line 3 def name @name end |
Instance Method Details
#add_identity_method(key, value) ⇒ Object
Return true iff instance is of named type (e.g. FooType.first.bar?)
64 65 66 67 68 69 70 |
# File 'lib/offline_lookup/builder.rb', line 64 def add_identity_method(key, value) @modyule.instance_exec(self, key, value) do |builder, key, value| define_method builder.sanitize(value) + "?" do self.attributes[builder.key] == key end end end |
#add_key_for_name_method ⇒ Object
e.g. FooType.id_for_name(“Bar”)
91 92 93 94 95 96 97 |
# File 'lib/offline_lookup/builder.rb', line 91 def add_key_for_name_method @modyule::ClassMethods.instance_exec(self) do |builder| define_method builder.sanitize("#{builder.key}_for_#{builder.name}") do |value| self.offline_lookup_values.key(value.to_s) end end end |
#add_key_lookup_method(key, value) ⇒ Object
Get key value (e.g. FooType.bar_id)
55 56 57 58 59 60 61 |
# File 'lib/offline_lookup/builder.rb', line 55 def add_key_lookup_method(key, value) @modyule::ClassMethods.instance_exec(self, key, value) do |builder, key, value| define_method builder.sanitize("#{value}_#{builder.key}") do key end end end |
#add_lookup(key, value) ⇒ Object
35 36 37 38 39 |
# File 'lib/offline_lookup/builder.rb', line 35 def add_lookup(key, value) add_key_lookup_method(key, value) add_identity_method(key, value) if @build_identity_methods add_lookup_method(key, value) if @build_lookup_methods end |
#add_lookup_method(key, value) ⇒ Object
Get instance by named method (e.g. FooType.bar) (Not offline, but syntactic sugar)
73 74 75 76 77 78 79 |
# File 'lib/offline_lookup/builder.rb', line 73 def add_lookup_method(key, value) @modyule::ClassMethods.instance_exec(self, key, value) do |builder, key, value| define_method builder.sanitize(value) do find_by(builder.key => self.offline_lookup_values.key(value.to_s)) end end end |
#add_name_for_key_method ⇒ Object
e.g. FooType.name_for_id(1)
82 83 84 85 86 87 88 |
# File 'lib/offline_lookup/builder.rb', line 82 def add_name_for_key_method @modyule::ClassMethods.instance_exec(self) do |builder| define_method builder.sanitize("#{builder.name}_for_#{builder.key}") do |key| self.offline_lookup_values[key] end end end |
#add_static_lookup_method ⇒ Object
e.g. FooType.lookup(“Bar”)
100 101 102 103 104 105 106 |
# File 'lib/offline_lookup/builder.rb', line 100 def add_static_lookup_method @modyule::ClassMethods.instance_exec(self) do |builder| define_method "lookup" do |value| find_by(builder.key => self.offline_lookup_values.key(value.to_s)) end end end |
#build ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/offline_lookup/builder.rb', line 25 def build @model.offline_lookup_values.each do |key, value| add_lookup(key, value) end add_name_for_key_method add_key_for_name_method add_static_lookup_method @model.include @modyule end |
#create_module ⇒ Object
18 19 20 21 22 23 |
# File 'lib/offline_lookup/builder.rb', line 18 def create_module @modyule = Module.new @modyule.extend ActiveSupport::Concern @modyule.const_set("ClassMethods", Module.new) @model.const_set("OfflineLookupMethods", @modyule) end |
#get_module ⇒ Object
14 15 16 |
# File 'lib/offline_lookup/builder.rb', line 14 def get_module @model.const_defined?("OfflineLookupMethods") && @model::OfflineLookupMethods end |
#sanitize(string) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/offline_lookup/builder.rb', line 41 def sanitize(string) #1. Replace illegal chars and _ boundaries with " " boundary string = string.to_s.gsub(/[^a-zA-Z\d]+/," ").strip #2. Insert " " boundary at snake-case boundaries string.gsub!(/([a-z])([A-Z])/){|s| "#{$1} #{$2}"} #3. underscore string.gsub!(/\s+/, "_") string.downcase! #4. Append underscore if name begins with digit string = "_#{string}" if string.length == 0 || string[0] =~ /\d/ return string end |