Class: JSONCop::Analyzer
- Inherits:
-
Object
- Object
- JSONCop::Analyzer
- Defined in:
- lib/jsoncop/analyzer.rb
Constant Summary collapse
- JSON_COP_ENABLED =
/@jsoncop/
- NEED_PARSING_CONTENT =
/(\/\/\s*@jsoncop\s+(?:struct|class)(?:.|\s)*?\n})/
- MODEL_NAME_REGEX =
/(struct|class)\s+(.+)\s*{/
- ATTRIBUTE_REGEX =
/^\s+(let|var)\s(.+):(.+)/
- JSON_TRANSFORMER_REGEX =
/^\s+static\s+func\s+(.+)JSONTransformer\(.+?\:(.+)\).+->.+/
- JSON_BY_PROPERTY_HASH_REGEX =
/static\s+func\s+JSONKeyPathByPropertyKey\(\)\s*->\s*\[String\s*:\s*String\]\s*{\s*return\s*(\[[\s\."a-z0-9A-Z_\-:\[\],]*)}/
Instance Attribute Summary collapse
-
#current_model ⇒ Object
Returns the value of attribute current_model.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
Instance Method Summary collapse
- #analyze! ⇒ Object
-
#initialize(file_path) ⇒ Analyzer
constructor
A new instance of Analyzer.
Constructor Details
#initialize(file_path) ⇒ Analyzer
Returns a new instance of Analyzer.
19 20 21 |
# File 'lib/jsoncop/analyzer.rb', line 19 def initialize(file_path) @file_path = file_path end |
Instance Attribute Details
#current_model ⇒ Object
Returns the value of attribute current_model.
17 18 19 |
# File 'lib/jsoncop/analyzer.rb', line 17 def current_model @current_model end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
16 17 18 |
# File 'lib/jsoncop/analyzer.rb', line 16 def file_path @file_path end |
Instance Method Details
#analyze! ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jsoncop/analyzer.rb', line 23 def analyze! content = File.read file_path return unless content =~ JSON_COP_ENABLED models = [] content.scan(NEED_PARSING_CONTENT).flatten.each do |content| content.each_line do |line| if line =~ MODEL_NAME_REGEX model_name = line.scan(MODEL_NAME_REGEX).flatten.last @current_model = Model::Model.new model_name models << @current_model elsif line =~ ATTRIBUTE_REGEX result = line.scan(ATTRIBUTE_REGEX).flatten @current_model.attributes << Model::Attribute.new(result[1], result[2]) elsif line =~ JSON_TRANSFORMER_REGEX result = line.scan(JSON_TRANSFORMER_REGEX).flatten @current_model.transformers << Model::Transformer.new(result[0], result[1]) end end json_attr_list = content.scan(JSON_BY_PROPERTY_HASH_REGEX).flatten.first if json_attr_list json_attr_list.gsub!(/[(\[\]"\s)]*/, '') @current_model.attr_json_hash = Hash[json_attr_list.split(",").map { |attr_json_pair| attr_json_pair.split(":") }] end end models end |